1. 程式人生 > >Android中可展開收起內容的TextView

Android中可展開收起內容的TextView

android開發中,我們常常會有一些需求,也不得不去實現它,一方面是提升自己的技術水平,再者是實現自己的價值,對一個階段性的自己有個評估,自己是否又進步了多少,又掌握了多少,如果遇到想跳槽時,面對面試官,是否有貨拿出來.............

今天我們就來說說,在android開發中,實現一個可以摺疊,展開收起內容的Textview,這個效果,程式碼呢也相對比較簡單,相信大家一定在QQ空間或者社交,資訊平臺看過,我們先來看一下展示效果:            

                                                                                  

      

如圖,我們看見當我們點選箭頭,textView會展開或者收起,並且附帶動畫效果渲染,使展開收起效果不單調,好了,廢話不說,直接上程式碼和實現原理

1. 新建一個MoreTextViwe 繼承 LinearLayout,實現CheckBox的checked事件 onCheckedChangeListener, view觀察器 OnPreDrawListener

public class MoreTextView extends LinearLayout implements CompoundButton.OnCheckedChangeListener,ViewTreeObserver.OnPreDrawListener{


    private int mMoreSwitchTextSize = 12;

    private int mMoreTextSize = 12;

    private int mMoreTextColor = Color.parseColor("#3c3c40");

    private int mMoreSwitchTextColor = Color.parseColor("#fc9400");

    private int mMaxHeight,mMinHeight,mMaxLine;

    private int mMinLine = 3;

    private int mLineHeight = -1;


    private TextView mTextView;
    private CheckBox mCheckBox;
}

2 , 第二步,計算TextView的高度,當然我們得給出一個TextView內容超出時預設的高度,列入超出三列我們的高度是多少,這個時候我們就需要計算,我們通過onPreDraw方法在view繪製之前獲取內容並且設定高度,minHeight,和maxHeight

    @Override
    public boolean onPreDraw() {
        mTextView.getViewTreeObserver().removeOnPreDrawListener(this);
        mMaxLine = mTextView.getLineCount(); //獲取當前最大的line

        if (mMaxLine != 0) { // view初始化時  這個值肯定為0
            //計算最大的高度
            //得到行高
            int tempLineHeight = mTextView.getHeight() / mMaxLine;

            if (mLineHeight == -1 || tempLineHeight > mLineHeight) {
                mLineHeight = tempLineHeight;
            }
        }

        // 求出最大的高度
        mMaxHeight = mLineHeight * mMaxLine;

        //如果點選了展開  此時更新資料時 我們不想自動關閉它

        if (mCheckBox.isChecked()) {
            mTextView.setHeight(mMaxHeight);
            return false;
        }


        if (mMaxLine > mMinLine) {

            mTextView.setLines(mMinLine); //設定為最小的行數

            //獲取最小行的高度
            mTextView.post(new Runnable() {
                @Override
                public void run() {
                    mMinHeight = mTextView.getHeight();
                }
            });

            if (noDrawable()) {
                mCheckBox.setText(mMoreSwitchHints[0]);
            }

            mCheckBox.setVisibility(VISIBLE);
            return false;
        }else {
            if (noDrawable()) {
                mCheckBox.setText(mMoreSwitchHints[1]);
            }
            mCheckBox.setVisibility(GONE);
        }

        return true;
    }

3, 我們設定好了高度,那麼我們要如何觸發這個展開收起呢,還記得我們實現了onCheckedChangeListener 的回撥嗎,當然我們肯定要去實現方法裡面填充內容

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

        mTextView.clearAnimation();

        final int deltaValue;
        final int startValue = mTextView.getHeight();
        if (b) {
            //展開
            deltaValue = mMaxHeight - startValue;
        } else {
            //縮排
            deltaValue = mMinHeight - startValue;
        }
        setMoreSwichHints();
        Animation animation = new Animation() {
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                mTextView.setHeight((int) (startValue + deltaValue * interpolatedTime));
                if (interpolatedTime == 0) {
                    t.clear();
                }
            }
        };
        animation.setDuration(350);
        mTextView.startAnimation(animation);
    }

好了,以上就是MoreTextView的核心程式碼了

MoreTextView : 原始碼

如果有幫助到你,記得在github上面點選star一下,作者便有更多動力創造更好的輪子啦

歡迎android愛好者加群    420221427