今天用到了这个功能,看了一下。在此记录一下。
一、首先记录一下本人今天所了解的新知识。
1、listview.getHeaderViewsCount (int position,int padding) 作用是设置ListView选中的位置,把当前position 的Item置顶,同时在Y轴设置一个偏移量(padding值)
2、invalidate(); 看一下源码
/**
* Invalidate the whole view. If the view is visible,
* {@link #onDraw(android.graphics.Canvas)} will be called at some point in
* the future.
* <p>
* This must be called from a UI thread. To call from a non-UI thread, call
* {@link #postInvalidate()}.
*/
public void invalidate() {
invalidate(true);
}
意思大概是:如果这个控件可获得,那就使这个View无效。这样接下来onDraw(Canvas canvas) 会执行
下面贴出 自定义控件的代码:
ublic class QuickAlphabeticBar extends ImageButton {
private static final String TAG = "QuickAlphabeticBar";
private TextView mDialogText;//用于显示选中的字幕
private Handler mHandler;
private ListView mList; //显示联系人的LIstView
private float mHight;
private String[] letters = 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" };
private HashMap<String, Integer> alphaIndexer;
public QuickAlphabeticBar(Context context) {
super(context);
}
public QuickAlphabeticBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public QuickAlphabeticBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void init(TextView mDialogText) {
this.mDialogText=mDialogText;
mDialogText.setVisibility(View.INVISIBLE);
mHandler = new Handler();
}
public void setListView(ListView mList) {
this.mList = mList;
}
public void setAlphaIndexer(HashMap<String, Integer> alphaIndexer) {
this.alphaIndexer = alphaIndexer;
}
public void setHight(float mHight) {
this.mHight = mHight;
}
public float getHight(){
return mHight;
}
float downX = 0, downY = 0, moveX = 0, moveY = 0;
int sumMove = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
int act = event.getAction();
float x = event.getX();
float y = event.getY();
final int oldChoose = choose;
//
int selectIndex = (int) (y / (mHight / letters.length));
if (selectIndex > -1 && selectIndex < letters.length) {
String key = letters[selectIndex];
if (alphaIndexer.containsKey(key)) {
int pos = alphaIndexer.get(key);
if (mList.getHeaderViewsCount() > 0) {
this.mList.setSelectionFromTop(
pos + mList.getHeaderViewsCount(), 0);
} else {
this.mList.setSelectionFromTop(pos, 0);
}
mDialogText.setText(letters[selectIndex]);
}
}
switch (act) {
case MotionEvent.ACTION_DOWN:
sumMove = 0;
showBkg = true;
downX = x;
downY = y;
if (oldChoose != selectIndex) {
if (selectIndex >= 0 && selectIndex < letters.length) {
choose = selectIndex;
invalidate();
}
}
showDialogText();
CLog.i(TAG, "=======================================");
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG,"Move==>y:"+y);
sumMove ++;
moveX = x;
moveY = y;
if(downX != 0 && downY != 0){
double moveAngle = getAngle(downX, moveX, downY, moveY);
CLog.i(TAG , " move angle = " + moveAngle + ",downX = " + downX + ",downY = " + downY + ",moveX = " + moveX + ",moveY = " + moveY);
if(sumMove < 10 && moveAngle < 0.5){
hideDialogText();
break;
}
}
if (oldChoose != selectIndex) {
if (selectIndex > 0 && selectIndex < letters.length) {
choose = selectIndex;
invalidate();//重新绘图
}
}
break;
case MotionEvent.ACTION_UP:
showBkg = false;
choose = -1;
hideDialogText();
break;
default:
break;
}
return super.onTouchEvent(event);
}
/**
* 获取当前左移的角度
* @param downX
* @param moveX
* @param downY
* @param moveY
*/
private double getAngle(float downX, float moveX, float downY, float moveY){
float disX = downX - moveX;
float disY = Math.abs(downY - moveY);
if(disX == 0)
return 10000;
return Math.atan(disY/disX);
}
/**
* 隐藏提示控件
*/
private void hideDialogText(){
if (mHandler != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (mDialogText != null
&& mDialogText.getVisibility() == View.VISIBLE) {
mDialogText.setVisibility(INVISIBLE);
}
}
});
}
}
/**
* 展示提示控件
*/
private void showDialogText(){
if (mHandler != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (mDialogText != null
&& mDialogText.getVisibility() == View.INVISIBLE) {
mDialogText.setVisibility(VISIBLE);
}
}
});
}
}
//TODO:
Paint paint = new Paint();
boolean showBkg = false;
int choose = -1;
protected void onDraw(Canvas canvas) {//绘制字母导航栏
super.onDraw(canvas);
Log.e(TAG,"Ondraw==>go");
int height = getHeight();
int width = getWidth();
int singleHeight = height / letters.length;
for (int i = 0; i < letters.length; i++) {
paint.setColor(getResources().getColor(R.color.text_title));//默认颜色
paint.setTextSize(30);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
if (i == choose) {//如果是当前选中的则设置颜色
paint.setColor(getResources().getColor(R.color.default_quick_alpha));//选中的颜色
paint.setFakeBoldText(true);
}
float xPos = width / 2 - paint.measureText(letters[i]) / 2;
float yPos = singleHeight * i + singleHeight;
canvas.drawText(letters[i], xPos, yPos, paint);
paint.reset();
}
}
public void setLetters(String[] letters){
this.letters = letters;
postInvalidate();
}
}
此时我们一般还需要获取系统通讯录...下次再说。
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦