在看Oplayer的時候看見滑動字母來實現listView的內容搜索,所以就把裡面的核心的函數扣除來做了一個demo,分為兩部分一個是布局,另一個就是代碼了,具體的如下:
布局:
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@android:color/transparent"
android:divider="@drawable/ic_line"
android:drawSelectorOnTop="false"
android:fadingEdge="none"
android:listSelector="@android:color/transparent"
android:scrollbars="none" />
<TextView
android:id="@+id/first_letter_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="50.0dip"
android:layout_marginTop="49.0dip"
android:background="@drawable/fast_scroller_overlay"
android:gravity="center"
android:paddingBottom="5.0dip"
android:scaleType="center"
android:textSize="43.0dip"
android:visibility="gone" />
</FrameLayout>
<ImageView
android:id="@+id/alphabet_scroller"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingRight="0dip"
android:scaleType="fitXY"
android:src="@drawable/alphabet_scroller_bg" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@android:color/transparent"
android:divider="@drawable/ic_line"
android:drawSelectorOnTop="false"
android:fadingEdge="none"
android:listSelector="@android:color/transparent"
android:scrollbars="none" />
<TextView
android:id="@+id/first_letter_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="50.0dip"
android:layout_marginTop="49.0dip"
android:background="@drawable/fast_scroller_overlay"
android:gravity="center"
android:paddingBottom="5.0dip"
android:scaleType="center"
android:textSize="43.0dip"
android:visibility="gone" />
</FrameLayout>
<ImageView
android:id="@+id/alphabet_scroller"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingRight="0dip"
android:scaleType="fitXY"
android:src="@drawable/alphabet_scroller_bg" />
</LinearLayout>
裡面涉及到了一個ImageView的圖片
[html]
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/contact_list_scroll_pressed" />
<item android:drawable="@drawable/contact_list_scroll_normal" />
</selector>
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/contact_list_scroll_pressed" />
<item android:drawable="@drawable/contact_list_scroll_normal" />
</selector>
具體的代碼如下:
[java]
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Main6 extends Activity{
ListView iListView;
TextView showChar;
ImageView myChar;
List<String> iList;
MAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main5);
iListView = (ListView)findViewById(R.id.list);
showChar = (TextView)findViewById(R.id.first_letter_overlay);
myChar = (ImageView)findViewById(R.id.alphabet_scroller);
iList = initChar();
Log.e("t", " "+iList.size());
mAdapter = new MAdapter();
iListView.setAdapter(mAdapter);
myChar.setClickable(true);
myChar.setOnTouchListener(asOnTouch);
}
/**
* A-Z
*/
private OnTouchListener asOnTouch = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 0
myChar.setPressed(true);
showChar.setVisibility(View.VISIBLE);
mathScrollerPosition(event.getY());
break;
case MotionEvent.ACTION_UP:// 1
myChar.setPressed(false);
showChar.setVisibility(View.GONE);
break;
case MotionEvent.ACTION_MOVE:
mathScrollerPosition(event.getY());
break;
}
return false;
}
};
/**
* 顯示字符
*
* @param y
*/
private void mathScrollerPosition(float y) {
int height = myChar.getHeight();
float charHeight = height / 28.0f;
char c = 'A';
if (y < 0)
y = 0;
else if (y > height)
y = height;
int index = (int) (y / charHeight) - 1;
if (index < 0)
index = 0;
else if (index > 25)
index = 25;
String key = String.valueOf((char) (c + index));
showChar.setText(key);
int position = 0;
if (index == 0)
iListView.setSelection(0);
else if (index == 25)
iListView.setSelection(mAdapter.getCount() - 1);
else {
if (mAdapter != null) {
for (int i = 0; i < iList.size(); i++) {
if (iList.get(i).startsWith(key)) {
iListView.setSelection(position);
break;
}
position++;
}
}
}
}
/**
* ListView的數據
* @return
*/
public List<String> initChar(){
List<String> list = new ArrayList<String>();
char c = 'A';
for (int i = 0; i < 26; i++) {
list.add(String.valueOf((char) (c + i)));
list.add(String.valueOf((char) (c + i)));
list.add(String.valueOf((char) (c + i)));
}
return list;
}
class MAdapter extends BaseAdapter{
@Override
public int getCount() {
return iList.size();
}
@Override
public Object getItem(int position) {
return iList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = new TextView(getApplicationContext());
text.setText(iList.get(position));
return text;
}
}
}
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Main6 extends Activity{
ListView iListView;
TextView showChar;
ImageView myChar;
List<String> iList;
MAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main5);
iListView = (ListView)findViewById(R.id.list);
showChar = (TextView)findViewById(R.id.first_letter_overlay);
myChar = (ImageView)findViewById(R.id.alphabet_scroller);
iList = initChar();
Log.e("t", " "+iList.size());
mAdapter = new MAdapter();
iListView.setAdapter(mAdapter);
myChar.setClickable(true);
myChar.setOnTouchListener(asOnTouch);
}
/**
* A-Z
*/
private OnTouchListener asOnTouch = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 0
myChar.setPressed(true);
showChar.setVisibility(View.VISIBLE);
mathScrollerPosition(event.getY());
break;
case MotionEvent.ACTION_UP:// 1
myChar.setPressed(false);
showChar.setVisibility(View.GONE);
break;
case MotionEvent.ACTION_MOVE:
mathScrollerPosition(event.getY());
break;
}
return false;
}
};
/**
* 顯示字符
*
* @param y
*/
private void mathScrollerPosition(float y) {
int height = myChar.getHeight();
float charHeight = height / 28.0f;
char c = 'A';
if (y < 0)
y = 0;
else if (y > height)
y = height;
int index = (int) (y / charHeight) - 1;
if (index < 0)
index = 0;
else if (index > 25)
index = 25;
String key = String.valueOf((char) (c + index));
showChar.setText(key);
int position = 0;
if (index == 0)
iListView.setSelection(0);
else if (index == 25)
iListView.setSelection(mAdapter.getCount() - 1);
else {
if (mAdapter != null) {
for (int i = 0; i < iList.size(); i++) {
if (iList.get(i).startsWith(key)) {
iListView.setSelection(position);
break;
}
position++;
}
}
}
}
/**
* ListView的數據
* @return
*/
public List<String> initChar(){
List<String> list = new ArrayList<String>();
char c = 'A';
for (int i = 0; i < 26; i++) {
list.add(String.valueOf((char) (c + i)));
list.add(String.valueOf((char) (c + i)));
list.add(String.valueOf((char) (c + i)));
}
return list;
}
class MAdapter extends BaseAdapter{
@Override
public int getCount() {
return iList.size();
}
@Override
public Object getItem(int position) {
return iList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = new TextView(getApplicationContext());
text.setText(iList.get(position));
return text;
}
}
}