TextView text collapse

Custom CollapsibleTextView tool class

//text collapse
public class CollapsibleTextView extends LinearLayout implements View.OnClickListener {

    // display a few lines by default
    private static final int DEFAULT_MAX_LINE_COUNT = 3;

    private static final int COLLAPSIBLE_STATE_NONE = 0;
    private static final int COLLAPSIBLE_STATE_SHRINKUP = 1;
    private static final int COLLAPSIBLE_STATE_SPREAD = 2;

    private TextView desc;
    private TextView descOp;

    private String shrinkup;
    private String spread;
    private int mState;
    private boolean flag;

    public CollapsibleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // two definitions
//        shrinkup = context.getString(R.string.desc_shrinkup);
//        spread = context.getString(R.string.desc_spread);
        shrinkup="收起";
        spread="Expand";
        View view = inflate(context, R.layout.collapsible_textview, this);
        view.setPadding(0, -1, 0, 0);
        desc = (TextView) view.findViewById(R.id.desc_tv);
        descOp = (TextView) view.findViewById (R.id.desc_op_tv);
        descOp.setOnClickListener(this);
    }

    public CollapsibleTextView(Context context) {
        this(context, null);
    }
    /**
     1.
     2. Provide external exposure methods and provide data for text
     3. What is the text content of @param charSequence
     4. @param bufferType
     */
    public final void setDesc(CharSequence charSequence, TextView.BufferType bufferType) {
        desc.setText(charSequence, bufferType);
        mState = COLLAPSIBLE_STATE_SPREAD;
        requestLayout();
    }

    @Override
    public void onClick(View v) {
        flag = false;
        requestLayout();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!flag) {
            flag = true;
            if (desc.getLineCount() <= DEFAULT_MAX_LINE_COUNT) {
                mState = COLLAPSIBLE_STATE_NONE;
                descOp.setVisibility(View.GONE);
                desc.setMaxLines(DEFAULT_MAX_LINE_COUNT + 1);
            } else {
                post(new InnerRunnable());
            }
        }
    }

    class InnerRunnable implements Runnable {
        @Override
        public void run() {
            if (mState == COLLAPSIBLE_STATE_SPREAD) {
                desc.setMaxLines(DEFAULT_MAX_LINE_COUNT);
                descOp.setVisibility(View.VISIBLE);
                descOp.setText(spread);
                mState = COLLAPSIBLE_STATE_SHRINKUP;
            } else if (mState == COLLAPSIBLE_STATE_SHRINKUP) {
                desc.setMaxLines(Integer.MAX_VALUE);
                descOp.setVisibility(View.VISIBLE);
                descOp.setText(shrinkup);
                mState = COLLAPSIBLE_STATE_SPREAD;
            }
        }
    }
}

collapsible_textview.xml definition

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/desc_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4.0dip"
        android:gravity="center_vertical"
        android:textColor="#ff000000"
        android:textSize="14.0dip" />

    <!--You can flexibly center the favorite buttons from right to left-->
    <TextView
        android:id="@+id/desc_op_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4.0dip"
        android:gravity="right"

        android:singleLine="true"
        android:textColor="#ff576b95"
        android:textSize="14.0dip"
        android:visibility="gone" />

</LinearLayout>

Use: Replace the TextView that needs to be collapsed with CollapsibleTextView

Then in the code call

CollapsibleTextView tv = (CollapsibleTextView) findViewById(R.id.desc_collapse_tv);

tv.setDesc("Text", BufferType.NORMAL);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325965392&siteId=291194637