Android 自訂滾動類Tab標籤,androidtab

來源:互聯網
上載者:User

Android 自訂滾動類Tab標籤,androidtab
要求

Tab 標籤可以橫向滾動,標籤可選擇,並且在選擇的時候有標線底線。

分析

可繼承HorizontalScrollView 實現,然后里面標籤ITem可可以是TextView,底線可以在Draw方法中繪製出。

實現
  • 添加Tab Item(這裡是TextView)
/**     * 向容器中添加標籤view     *      * @param position     * @param title     */    private void add****(final int position, String title)    {        TextView tab = new TextView(getContext());        tab.setText(title);        tab.setGravity(Gravity.CENTER);        tab.setSingleLine();        if (position == currentPosition)        {            tab.setTextColor(mTabPressTextColor);        } else        {            tab.setTextColor(mTabTextColor);        }        tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabTextSize);        tab.setBackgroundResource(tabBackgroundResId);        addTab(position, tab);    }
  • 繪製底線
protected void onDraw(Canvas canvas)    {        super.onDraw(canvas);        /** 繪製tab標籤底線 **/        View currentTab = tabsContainer.getChildAt(currentPosition);        if (currentTab != null)        {            float lineLeft = currentTab.getLeft();            canvas.drawRect(lineLeft - detalLeft,                    mViewHeight - underlineHeight,                    lineLeft + currentTab.getWidth() - detalRight, mViewHeight,                    mLinePaint)`        }    }
  • 實現Item選擇底線動畫
/**     * 類比動畫滾動底線     *      * @param fromPosition     * @param toPosition     */    private void *****(int fromPosition, int toPosition)    {        TextView lastTab = (TextView) tabsContainer.getChildAt(fromPosition);        lastTab.setTextColor(mTabTextColor);        TextView currentTab = (TextView) tabsContainer.getChildAt(toPosition);        currentTab.setTextColor(mTabPressTextColor);        currentPosition = toPosition;        float lineLeft = currentTab.getLeft();        float lineRight = currentTab.getRight();        detalLeft = lineLeft - lastTab.getLeft();        detalRight = lineRight - lastTab.getRight();        this.post(new Runnable()        {            @Override            public void run()            {                if (Math.abs(detalLeft) > minDetal                        || Math.abs(detalRight) > minDetal)                {                    if (Math.abs(detalLeft) > minDetal)                    {                        detalLeft = detalLeft / minDetal;                    }                    if (Math.abs(detalRight) > minDetal)                    {                        detalRight = detalRight / minDetal;                    }                    invalidate();                    TabHorizontalScrollView.this.post(this);                } else                {                    invalidate();                }            }        });    }
  • 綁定監聽
/**     * 標籤監聽事件     */    private OnTabItemClickListener mOnTabItemClickListener;    /**     * 綁定標籤切換監聽事件     *      * @param listener     */    public void setOnTabItemClickListener(OnTabItemClickListener listener)    {        mOnTabItemClickListener = listener;    }    /**     * 標籤監聽類     *      * @author jarlen     *      */    public interface OnTabItemClickListener    {        public void onClickTabItem(float value);    }在Item 的TextView的OnClick的方法中調用onClickTabItem()方法,然後在Activity中實現。

如果想要此效果Demo,

代碼 = money

請點擊臨時QQ交談,非誠勿擾!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.