安辉 2020-01-10
首先布局
因为我这个是列表所以这只是一个item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:background="@drawable/rounded_box">
<TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:layout_marginTop="8dp"
android:text="日期"
android:textColor="#ff333333"
android:textSize="13sp" />
<TextView
//最重要 设定显示几行
android:maxLines="3"
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_date"
android:layout_alignLeft="@id/tv_date"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="text"
android:textColor="#ff333333"
android:textSize="13sp" />
<LinearLayout
android:id="@+id/ll_img"
android:layout_marginBottom="10dp"
android:layout_marginLeft="17dp"
android:layout_below="@id/tv_text"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_marginRight="15dp"
android:id="@+id/iv_img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:layout_marginRight="15dp"
android:id="@+id/iv_img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv_img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<ImageView
android:id="@+id/iv_more"
android:visibility="gone"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:layout_below="@+id/ll_img"
android:layout_centerHorizontal="true"
android:src="@mipmap/more"
android:layout_width="17dp"
android:layout_height="12dp"/>
</RelativeLayout>
</LinearLayout>
//java实现逻辑
//判断文字超出三行后显示还是隐藏
private Boolean flag = true;
((ViewHolderTitle) holder).tv_text.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (((ViewHolderTitle) holder).tv_text.getLineCount() >= 3) {
((ViewHolderTitle) holder).iv_more.setVisibility(View.VISIBLE);
} else {
((ViewHolderTitle) holder).iv_more.setVisibility(View.GONE);
}
}
});
((ViewHolderTitle) holder).iv_more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag) {
flag = false;
//向上箭头图片
((ViewHolderTitle) holder).iv_more.setImageResource(R.mipmap.more);
((ViewHolderTitle) holder).tv_text.setEllipsize(null);//展开
((ViewHolderTitle) holder).tv_text.setMaxLines(Integer.MAX_VALUE);//把TextView行数显示取消掉
((ViewHolderTitle) holder).tv_text.setSingleLine(false);//这个方法是必须设置的,否则无法展开
} else {
flag = true;
//向下箭头图片
((ViewHolderTitle) holder).iv_more.setImageResource(R.mipmap.more_open);
((ViewHolderTitle) holder).tv_text.setEllipsize(TextUtils.TruncateAt.END); // 收缩
((ViewHolderTitle) holder).tv_text.setMaxLines(3);
}
}
});