动态更新UI之组件更新

项目中使用到了仿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();

先随便写下,项目完成再修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值