聯繫人快速索引

#聯繫人快速索引

效果圖:



###MainActivity主要代碼

public class MainActivity extends AppCompatActivity {

    @InjectView(R.id.lv_contacts)
    ListView lvContacts;
    @InjectView(R.id.quick_index_bar)
    QuickIndexBar quickIndexBar;
    @InjectView(R.id.tv_center)
    TextView tvCenter;
    private ArrayList<Person> persons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        quickIndexBar.setListener(new QuickIndexBar.OnLetterUpdateListener() {
            @Override
            public void onLetterUpdate(String letter) {
                showLetter(letter);
                // 根據字母定位ListView, 找到集合中第一個以letter爲拼音首字母的對象,得到索引
                for (int i = 0; i < persons.size(); i++) {
                    Person person = persons.get(i);
                    String str = person.getPinyin().charAt(0) + "";
                    if (TextUtils.equals(letter, str)) {
                        lvContacts.setSelection(i);
                        break;
                    }
                }
            }
        });
        persons = new ArrayList<Person>();
        fillAndSortData(persons);
        lvContacts.setAdapter(new HaoHanAdapter(MainActivity.this, persons));
    }

    /**
     * 填充數據並排序
     * @param persons
     */
    private void fillAndSortData(ArrayList<Person> persons) {
        //填充數據
        for (int i = 0; i < Cheeses.HAO_HAN_NAMES.length; i++) {
            String name = Cheeses.HAO_HAN_NAMES[i];
            persons.add(new Person(name));
        }
        //排序
        Collections.sort(persons);
    }

    private Handler handler = new Handler();

    /**
     * 展示聯繫人的首字母
     */
    private void showLetter(String letter) {
        tvCenter.setVisibility(View.VISIBLE);
        tvCenter.setText(letter);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                tvCenter.setVisibility(View.GONE);
            }
        }, 1000);
    }
}

###自定義快速索引控件

  
  public class QuickIndexBar extends View {
    private static final String TAG = "QuickIndexBar";
    private static final 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;

    private int cellWidth;

    private float cellHeight;

    //設置字母改變監聽
    public interface OnLetterUpdateListener {
        void onLetterUpdate(String letter);
    }

    private OnLetterUpdateListener listener;

    public OnLetterUpdateListener getListener() {
        return listener;
    }

    public void setListener(OnLetterUpdateListener listener) {
        this.listener = listener;
    }

    public QuickIndexBar(Context context) {
        this(context, null);
    }

    public QuickIndexBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QuickIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//添加抗鋸齒
        mPaint.setColor(Color.WHITE);
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);//使用默認的字體對象
    }

   
    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < LETTERS.length; i++) {
            String text = LETTERS[i];
            //計算座標
            int x = (int) (cellWidth / 2.0f - mPaint.measureText(text) / 2.0f);
            Rect bounds = new Rect();
            mPaint.getTextBounds(text, 0, text.length(), bounds);
            int textHeight = bounds.height();
            int y = (int) (cellHeight / 2.0f + textHeight / 2.0f + i * cellHeight);
            mPaint.setColor(touchIndex == i ? Color.GRAY : Color.BLACK);
            canvas.drawText(text, x, y, mPaint);
        }
    }

    private int touchIndex = -1;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = -1;
        switch (MotionEventCompat.getActionMasked(event)) {
            case MotionEvent.ACTION_DOWN:
                //獲取當前觸摸到字母索引
                index = (int) (event.getY() / cellHeight);
                if (index >= 0 && index < LETTERS.length) {
                    //判斷是否跟上次一樣
                    if (index != touchIndex) {
                        if (listener != null) {
                            listener.onLetterUpdate(LETTERS[index]);
                        }
                        Log.d(TAG, "onTouchEvent" + LETTERS[index]);
                        touchIndex = index;
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                index = (int) (event.getY() / cellHeight);
                if (index >= 0 && index < LETTERS.length) {
                    //判斷是否跟上次一樣
                    if (index != touchIndex) {
                        if (listener != null) {
                            listener.onLetterUpdate(LETTERS[index]);
                        }
                        Log.d(TAG, "onTouchEvent" + LETTERS[index]);
                        touchIndex = index;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                touchIndex = -1;
                break;
            default:
                break;
        }
        invalidate();//手觸摸後顯示滾輪
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //獲取單元格的高度
        cellWidth = getMeasuredWidth();
        int mHeight = getMeasuredHeight();
        cellHeight = mHeight * 1.0f / LETTERS.length;
    }
}

源碼

改進後的城市快速索引

發佈了21 篇原創文章 · 獲贊 19 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章