自定義View:04-字母索引

1诅病、效果圖如下:

字母索引.gif

2戚宦、實(shí)現(xiàn)

2.1自定義IndexesView_06繼承View
public class IndexesView_06 extends View {
    private Paint paint;
    private String[] letterList = 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 static final String TAG = "IndexesView_06";
    private String mCurrentText;


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

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

    public IndexesView_06(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setTextSize(pxToSp(12));
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int textWidth = (int) paint.measureText("A");//A字母的寬度
        int width = textWidth + getPaddingLeft() + getPaddingRight();
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
    }

    private float pxToSp(int sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //每個(gè)字母Item的高度
        int itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / letterList.length;
        for (int i = 0; i < letterList.length; i++) {
            //求文字的x 軸中心 :item 的寬度/2 -字母的寬度/2
            int textWidth = (int) paint.measureText(letterList[i]);//A字母的寬度
            int letterCenterX = getWidth() / 2 - textWidth / 2;

            //文字的Y軸中心 :第幾個(gè)字母*字母Item的高度 + 字母一半的高度 和頂部的padding高度
            int letterCenterY = i * itemHeight + itemHeight / 2;
            //字體的基線
            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            int dy = (int) ((fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);
            int baseLine = letterCenterY + dy;


            if (letterList[i].equals(mCurrentText)) {
                paint.setColor(Color.BLUE);
            } else {
                paint.setColor(Color.BLACK);
            }
            canvas.drawText(letterList[i], letterCenterX, baseLine, paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:

                float eventY = event.getY();
                int itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / letterList.length;
                int currentCode = (int) eventY / itemHeight;

                if (currentCode < 0)
                    currentCode = 0;

                if (currentCode > letterList.length - 1)
                    currentCode = letterList.length - 1;

                mCurrentText = letterList[currentCode];

                if (listener != null) {
                    listener.touch(mCurrentText);
                }
                break;

            case MotionEvent.ACTION_UP:
                if (listener != null) {
                    listener.touch("");
                }
                mCurrentText="";

                break;
        }
        invalidate();
        return true;
    }

    private currentListener listener;

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

    public interface currentListener {
        void touch(String text);
    }
}

2.2、使用

xml:

public class IndexeFragment_06 extends Fragment {
   private IndexesView_06 view_06;
   private TextView textView;
   private static final String TAG = "IndexeFragment_06";

   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.view_06, container, false);
       view_06 = view.findViewById(R.id.letter_view);
       textView = view.findViewById(R.id.letter_text);
       view_06.setListener(new IndexesView_06.currentListener() {
           @Override
           public void touch(String text) {
               textView.setText(text);
           }
       });
       return view;
   }

}

fragment:

public class IndexeFragment_06 extends Fragment {
    private IndexesView_06 view_06;
    private TextView textView;
    private static final String TAG = "IndexeFragment_06";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_06, container, false);
        view_06 = view.findViewById(R.id.letter_view);
        textView = view.findViewById(R.id.letter_text);
        view_06.setListener(new IndexesView_06.currentListener() {
            @Override
            public void touch(String text) {
                textView.setText(text);
            }
        });
        return view;
    }

}
```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瑞信,一起剝皮案震驚了整個(gè)濱河市顷帖,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鼎文,老刑警劉巖渔肩,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異拇惋,居然都是意外死亡周偎,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門撑帖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蓉坎,“玉大人,你說我怎么就攤上這事胡嘿◎劝” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵衷敌,是天一觀的道長(zhǎng)勿侯。 經(jīng)常有香客問我,道長(zhǎng)缴罗,這世上最難降的妖魔是什么助琐? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮瞒爬,結(jié)果婚禮上弓柱,老公的妹妹穿的比我還像新娘。我一直安慰自己侧但,他們只是感情好矢空,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著禀横,像睡著了一般屁药。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上柏锄,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天酿箭,我揣著相機(jī)與錄音,去河邊找鬼趾娃。 笑死缭嫡,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的抬闷。 我是一名探鬼主播妇蛀,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼耕突,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了评架?” 一聲冷哼從身側(cè)響起眷茁,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎纵诞,沒想到半個(gè)月后上祈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡浙芙,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年登刺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片茁裙。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡塘砸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出晤锥,到底是詐尸還是另有隱情,我是刑警寧澤廊宪,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布矾瘾,位于F島的核電站,受9級(jí)特大地震影響箭启,放射性物質(zhì)發(fā)生泄漏壕翩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一傅寡、第九天 我趴在偏房一處隱蔽的房頂上張望放妈。 院中可真熱鬧,春花似錦荐操、人聲如沸芜抒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宅倒。三九已至,卻和暖如春屯耸,著一層夾襖步出監(jiān)牢的瞬間拐迁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工疗绣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留线召,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓多矮,卻偏偏與公主長(zhǎng)得像缓淹,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354