在网上搜索关于ListView 的时候发现一篇写的很好的例子关于ListView 按首字母查询的案例
效果图
原文链接
http://blog.csdn.net/chenfeng0104/article/details/7067763
在用的时候发现了个小问题,楼主在定位字母的时候定位不准,所以在此进行了点修改
修改以后的Java代码
package com.inlee.demo.view;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* 右边带有字母查询的ListView
* @author Davee
*/
public class AlphabetListView extends FrameLayout {
private Context mContext;
private ListView mListView;
private LinearLayout alphabetLayout;
private TextView mTextView;
private AlphabetPositionListener positionListener;
private float screenDensity;
private Handler mHandler;
private HideIndicator mHideIndicator = new HideIndicator();
private int indicatorDuration = 1000;
public void setIndicatorDuration(int duration) {
this.indicatorDuration = duration;
}
private final class HideIndicator implements Runnable {
@Override
public void run() {
mTextView.setVisibility(View.INVISIBLE);
}
}
public AlphabetListView(Context context) {
super(context);
init(context);
}
public AlphabetListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
mContext = context;
screenDensity = context.getResources().getDisplayMetrics().density;
mHandler = new Handler();
mListView = new ListView(mContext);
initAlphabetLayout(mContext);
mTextView = new TextView(mContext);
mTextView.setTextSize(convertDIP2PX(50));
mTextView.setTextColor(Color.argb(150, 255, 255, 255));
mTextView.setBackgroundColor(Color.argb(200, 0, 0, 0));
mTextView.setMinWidth(convertDIP2PX(70));
mTextView.setMinHeight(convertDIP2PX(70));
int pixels = convertDIP2PX(10);
mTextView.setPadding(pixels, pixels, pixels, pixels);
mTextView.setGravity(Gravity.CENTER);
mTextView.setVisibility(View.INVISIBLE);
FrameLayout.LayoutParams textLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
textLayoutParams.gravity = Gravity.CENTER;
mTextView.setLayoutParams(textLayoutParams);
}
public void setAdapter(ListAdapter adapter, AlphabetPositionListener positionListener) {
if (positionListener == null)
throw new IllegalArgumentException("AlphabetPositionListener is required");
mListView.setAdapter(adapter);
this.positionListener = positionListener;
this.addView(mListView);
this.addView(alphabetLayout);
this.addView(mTextView);
}
private void initAlphabetLayout(Context context) {
alphabetLayout = new LinearLayout(context);
alphabetLayout.setOrientation(LinearLayout.VERTICAL);
FrameLayout.LayoutParams alphabetLayoutParams = new FrameLayout.LayoutParams(40,FrameLayout.LayoutParams.FILL_PARENT);
alphabetLayoutParams.gravity = Gravity.RIGHT;
alphabetLayout.setLayoutParams(alphabetLayoutParams);
final String[] alphabet = new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1;
params.gravity = Gravity.CENTER_HORIZONTAL;
for (int i=0, count=alphabet.length; i < count; i++) {
TextView textView = new TextView(context);
textView.setTextColor(Color.argb(150, 150, 150, 150));
textView.setBackgroundColor(Color.argb(0, 255, 255, 0));
textView.setTextSize(15);
textView.setText(alphabet[i]);
textView.setGravity(Gravity.CENTER);
textView.setLayoutParams(params);
textView.setTag(i);
alphabetLayout.addView(textView);
}
alphabetLayout.setOnTouchListener(new OnTouchListener() {
int unit=0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
TextView tv=(TextView)alphabetLayout.findViewWithTag(0);
unit=tv.getHeight();
alphabetLayout.setBackgroundColor(Color.argb(50, 100, 200, 100));
int l = (int)(event.getY()/unit);
int pos = positionListener.getPosition(alphabet[l]);
if (pos != -1) {
mTextView.setText(alphabet[l]);
mTextView.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(mHideIndicator);
mHandler.postDelayed(mHideIndicator, indicatorDuration);
mListView.setSelection(pos);
}
break;
case MotionEvent.ACTION_MOVE:
l=(int)(event.getY()/unit);
pos = positionListener.getPosition(alphabet[l]);
if (pos != -1) {
mTextView.setText(alphabet[l]);
mTextView.setVisibility(View.VISIBLE);
mHandler.removeCallbacks(mHideIndicator);
mHandler.postDelayed(mHideIndicator, indicatorDuration);
mListView.setSelection(pos);
}
break;
case MotionEvent.ACTION_UP:
alphabetLayout.setBackgroundResource(0);
break;
}
return true;
}
});
}
public int convertDIP2PX(float dip) {
return (int)(dip*screenDensity + 0.5f*(dip>=0?1:-1));
}
public static interface AlphabetPositionListener {
public static final int UNKNOW = -1;
public int getPosition(String letter);
}
}
完整的工程 下载地址 http://download.csdn.net/detail/aiyowodetian/4423264