项目中使用到了仿iphone那样的城市分组列表,网上找到了一个demo但是问题来了,他的数据是直接放在里面的,而我的需求是动态获取数据,再更新UI
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* 右侧字母表,快速定位
*
* @author lmj
*
*/
public class RightCharacterListView extends View {
private String[] c =null;
public void setC(String[] c)
{ this.c = c;
}
String[] b = { "#", "A", "B", "C", "F", "G", "H", "J", "K",
"L", "M", "N", "P", "Q", "S", "T", "W", "X",
"Y", "Z" ,"O"};
// String[] b =new CitySelectActivity().rightchar;
OnTouchingLetterChangedListener onTouchingLetterChangedListener;
int choose = -1;
Paint paint = new Paint();
boolean showBkg = false;
public RightCharacterListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public RightCharacterListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RightCharacterListView(Context context,String [] b) {
super(context);
this.b= b;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (showBkg) {
// 右侧文本点击时候的颜色
// canvas.drawColor(Color.parseColor("#ffffff"));
}
if (c!=null&&c.length>0) {
b =c;
}
int height = this.getHeight();
int width = this.getWidth();
int singleHeight = height / b.length;
for (int i = 0; i < b.length; i++) {
// 设置文本的颜色
paint.setColor(Color.BLACK);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
if (i == choose) {
// 修改成点击的时候应该textview显示
// 右侧字母点击时候的颜色
// paint.setColor(Color.parseColor("#000000"));
// paint.setFakeBoldText(true);
}
float xPos = width / 2 - paint.measureText(b[i]) / 2;
float yPos = singleHeight * i + singleHeight;
canvas.drawText(b[i], xPos, yPos, paint);
paint.reset();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * (b.length));
switch (action) {
case MotionEvent.ACTION_DOWN:
showBkg = true;
if (oldChoose != c && listener != null) {
if (c > 0 && c < b.length) { // 如果第一个字母是#,无效点击的话,条件变为c>0
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_MOVE:
if (oldChoose != c && listener != null) {
if (c > 0 && c < b.length) { // 如果第一个字母是#,无效点击的话,条件变为c>0
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
showBkg = false;
choose = -1;
invalidate();
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
public interface OnTouchingLetterChangedListener {
public void onTouchingLetterChanged(String s);
}
}
<RelativeLayout
android:id="@+id/relaytivelayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/relativeLayout1"
android:background="#ffffff" >
<!-- 列表 -->
<ListView
android:id="@+id/listInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ffffff"
android:cacheColorHint="#ffffff"
android:fastScrollEnabled="true"
android:focusable="true" >
</ListView>
<com.。。。。.selectcity.RightCharacterListView
android:id="@+id/rightCharacterListView"
android:background="@null"
android:layout_width="30dip"
android:layout_height="fill_parent"
android:layout_alignParentRight="true" />
</RelativeLayout>
rightchar=letter.toArray(new String[] {});
RightCharacterListView letterListView = (RightCharacterListView) findViewById(R.id.rightCharacterListView);
letterListView.setC(rightchar);
letterListView.invalidate();
先随便写下,项目完成再修改