• 在ListView的右边添加字母列表


    在ListView的右边添加字母列表,点击某个字母时,列表就滚动到预期位置。

    <!-- 数字和字母栏在标题栏下边并且停靠在右边 -->
    <com.txrj.sms.component.QuickAlphaBar
        android:id="@+id/fast_scroller"
        android:layout_width="22dp"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/title_bar"
        android:layout_gravity="top|right|center"
        android:layout_marginTop="0dip"
        android:background="@null"
        android:scaleType="centerInside"
        android:src="@drawable/dic_background" />

    <!-- 当前滚动到哪个字母那里 -->
    <TextView
        android:id="@+id/fast_position"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal|top"
        android:layout_margin="34dip"
        android:background="@drawable/sort_icon_bg_click"
        android:gravity="center"
        android:padding="2dip"
        android:textColor="#404040"
        android:textSize="48dip"
        android:visibility="invisible" />

    /**
    * @ClassName QuickAlphaBar
    * @description
    * @author Txrj
    * @date 2013-7-9 上午10:56:16
    */
    public class QuickAlphaBar extends ImageButton {
       
        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 Paint mPaint = new Paint();
        private int select = 0;
        private int singleHeight;
        private ListView mListView;
        private TextView mAlphsTextView;
        private HashMap<String, Integer> alphaIndexer;
        private Handler mHandler = new Handler();

        public QuickAlphaBar(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
       
        /* (non-Javadoc)
         * @see android.view.View#onTouchEvent(android.view.MotionEvent)
         */
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int lastSelect = select;       
            int index = (int) (event.getY() / singleHeight);
            if (index >= 0 && index < letters.length) {
                String alpha = letters[index];
                if (alphaIndexer.containsKey(alpha)) {
                    int position = alphaIndexer.get(alpha);
                    if (mListView.getHeaderViewsCount() > 0) {
                        mListView.setSelectionFromTop(position
                                + mListView.getHeaderViewsCount(), 0);
                    } else {
                        mListView.setSelectionFromTop(position, 0);
                    }
                    mAlphsTextView.setText(letters[index]);
                }
            }
            switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if(lastSelect != index) {
                    if(index >= 0 && index < letters.length) {
                        select = index;
                        invalidate();
                    }
                }
                mAlphsTextView.setVisibility(VISIBLE);
                break;
            case MotionEvent.ACTION_MOVE:
                if(lastSelect != index) {
                    if(index >= 0 && index < letters.length) {
                        select = index;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                select = -1;
                mAlphsTextView.setVisibility(INVISIBLE);
                break;
            }
            return super.onTouchEvent(event);
        }
       
        /* (non-Javadoc)
         * @see android.widget.ImageView#onDraw(android.graphics.Canvas)
         */
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            singleHeight = getHeight() / letters.length;
            for(int i=0;i<letters.length;i++){
                mPaint.setTextSize(20);
                mPaint.setTypeface(Typeface.DEFAULT_BOLD);
                mPaint.setAntiAlias(true);
                mPaint.setColor(Color.GRAY);
                if(i == select) {
                    mPaint.setColor(Color.parseColor("#00BFFF"));
                    mPaint.setFakeBoldText(true);
                }
                float x = getWidth() / 2 - mPaint.measureText(letters[i]) / 2;
                float y = singleHeight * (i + 1);
                canvas.drawText(letters[i], x, y, mPaint);
                mPaint.reset();
            }
        }
       
        public void setListView(ListView listView) {
            mListView = listView;
        }
       
        public void setAlphaIndexer(HashMap<String, Integer> alphaIndexer) {
            this.alphaIndexer = alphaIndexer;
        }
       
        public void setTextView(TextView textView) {
            this.mAlphsTextView = textView;
        }
    }

  • 相关阅读:
    学习些新东西
    浏览器内的web开发工具
    基于oracle开发的初步接触
    LAMP3 PHP安装
    svn for windows
    PHP替换掉字符串中的非字符
    搭个邮件服务器
    centos下安装mysql
    安装tomcat
    c#线程
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/3179955.html
Copyright © 2020-2023  润新知