textview摺疊和顯示多行

先上需求圖:

網上見的跟多的是把向下圖標換成“更多”字樣,然後都可以點擊。而我這個是點擊展開後不需要顯示收縮按鈕,其實要做還是可以的。

我主要是通過佈局來實現的,屬於取巧。

佈局代碼:用相對佈局包裹內容和圖標

<RelativeLayout
            android:id="@+id/booke_detail_resume_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
    >
        <TextView
                android:id="@+id/book_comment_item_comment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/color_1b1b1b"
                android:ellipsize="end"
                android:gravity=""
                android:padding="32,0,32,35"
                android:lineSpacingMultiplier="1.2"
                app:x_text_size="28"
        />
        <XRelativeLayout
                android:id="@+id/book_detail_open"
                android:layout_width="126px"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:padding="70,20,32,40"
        >
            <ImageView
                    android:id="@+id/book_more_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentBottom="true"
                    android:src="@mipmap/open"
                    app:x_layout_size="24,14"
            />
        </RelativeLayout>
    </RelativeLayout>

佈局不用直接拷貝,因爲會報錯,理解即可。

代碼包括兩部分:初始化和監聽

初始化代碼:

if (!TextUtils.isEmpty(commentBean.getContent())) {
                tvContent.setText(commentBean.getContent());
                moreLayout.setTag(commentBean.getContent());
                if (handler != null) {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            tvContent.setVisibility(View.VISIBLE);
                            if (tvContent.getLineCount() > 3) {
                                tvContent.setMaxLines(3);
                                moreLayout.setVisibility(View.VISIBLE);

                                int lineEndIndex = tvContent.getLayout().getLineEnd(2);
                                String text = tvContent.getText().subSequence(0, lineEndIndex - 4) + "...";
                                tvContent.setText(text);
                            } else {
                                moreLayout.setVisibility(View.GONE);
                            }
                        }
                    }, 100);
                }
            }

監聽:

moreLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (tvContent.getLineCount() > 3) {
                        tvContent.setMaxLines(3);
                        int lineEndIndex = tvContent.getLayout().getLineEnd(2);
                        String text = tvContent.getText().subSequence(0, lineEndIndex - 5) + "...";
                        tvContent.setText(text);
                    } else {
                        tvContent.setMaxLines(Integer.MAX_VALUE / 2);
                        tvContent.setText((String)v.getTag());
                    }
                    moreLayout.setVisibility(View.GONE); // 我們的需求不需要收縮,就直徑隱藏了
                }
            });

具體在使用的過程中,需要自己調整圖標的上下位置,調整好就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章