列表按字母顺序排序,右侧实现根据首字母快速查找,输入框输入首字母或元素第一个字也可以实现快速查找。
页面布局如下:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.demo.view.ClearEditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:background="@drawable/arrow_down"
android:hint="快速查找">
</com.example.demo.view.ClearEditText>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:divider="@null"
android:scrollbars="none"/>
<TextView
android:id="@+id/tv"
android:layout_width="80.0dip"
android:layout_height="80.0dip"
android:layout_gravity="center"
android:background="@drawable/text_shape_bg2"
android:gravity="center"
android:textColor="#ffffffff"
android:textSize="30.0dip"
android:visibility="invisible"/>
<com.example.demo.view.SideBar
android:id="@+id/sidrbar"
android:layout_width="30.0dip"
android:layout_height="fill_parent"
android:layout_gravity="right|center"/>
</FrameLayout>
</LinearLayout>
在布局文件中,输入框ClearEditText和快速查找的SideBar需要自定义实现。
ClearEditText:
publicclass ClearEditTextextends EditTextimplements OnFocusChangeListener,
TextWatcher {
/**
*删除按钮的引用
*/
private DrawablemClearDrawable;
public ClearEditText(Context context) {
this(context,null);
}
//这个构造方法也很重要,不加这个很多属性不能再XML里面定义
public ClearEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.editTextStyle);
}
public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
privatevoid init() {
//获取EditText的删除文字图片,假如没有设置我们就使用默认的图片
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable ==null) {
mClearDrawable = getResources().getDrawable(
R.drawable.emotionstore_progresscancelbtn);
}
//图片被绘制的区域
mClearDrawable.setBounds(0, 0,
mClearDrawable.getIntrinsicWidth() / 3 * 2,
mClearDrawable.getIntrinsicHeight() / 3 * 2);
setClearIconVisible(false);
setOnFocusChangeListener(this);
addTextChangedListener(this);
}
/**
*因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件当我们按下的位置横坐标在( EditText的宽-
*图标到控件右边的间距-图标的宽)和(EditText的宽-图标到控件右边的间距)之间我们就算点击了图标,竖直方向没有考虑
*/
@Override
publicbooleanonTouchEvent(MotionEvent event) {
if (getCompoundDrawables()[2] !=null) {
if (event.getAction() == MotionEvent.ACTION_UP) {
boolean touchable = event.getX() > (getWidth()
- getPaddingRight() -mClearDrawable
.getIntrinsicWidth())
&&(event.getX() < ((getWidth() - getPaddingRight())));
if (touchable) {
this.setText("");
}
}
}
returnsuper.onTouchEvent(event);
}
/**
*当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
*/
@Override
publicvoid onFocusChange(View v,boolean hasFocus) {
if (hasFocus) {
setClearIconVisible(getText().length()> 0);
}else {
setClearIconVisible(false);
}
}
/**
*设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
*
*@param visible
*/
protectedvoid setClearIconVisible(boolean visible) {
Drawable right = visible ?mClearDrawable :null;
setCompoundDrawables(getCompoundDrawables()[0],
getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
/**
*当输入框里面内容发生变化的时候回调的方法
*/
@Override
publicvoid onTextChanged(CharSequence s,int start,int count,int after) {
setClearIconVisible(s.length()> 0);
}
@Override
publicvoid beforeTextChanged(CharSequence s,int start,int count,
int after) {
}
@Override
publicvoid afterTextChanged(Editable s) {
}
/**
*设置晃动动画控件晃动(根据需求)
*/
publicvoid setShakeAnimation() {
this.setAnimation(shakeAnimation(5));
}
/**
*晃动动画
*
*@param counts
* 1秒钟晃动多少次
*@return
*/
publicstatic Animation shakeAnimation(int counts) {
AnimationtranslateAnimation =new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateAnimation;
}
SideBar:
publicclass SideBarextends View {
//触摸事件
private OnTouchingLetterChangedListeneronTouchingLetterChangedListener;
// 26个字符
publicstatic String[]b = { "A","B","C","D","E","F","G","H","I",
"J","K","L","M","N","O","P","Q","R","S","T"