先上需求图:

网上见的跟多的是把向下图标换成“更多”字样,然后都可以点击。而我这个是点击展开后不需要显示收缩按钮,其实要做还是可以的。

我主要是通过布局来实现的,属于取巧。

布局代码:用相对布局包裹内容和图标

<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); // 我们的需求不需要收缩,就直径隐藏了
                }
            });

具体在使用的过程中,需要自己调整图标的上下位置,调整好就可以了。


本文转载:CSDN博客