①监听TextView的绘制,当其未超过显示最大行数时,将其底部drawable动态设置为null。
②监听TextView的绘制超过显示最大行数时,记录最大行数的最后一个字的位置mLastEnd。
③监听TextView的点击事件,通过底部是否为null,为null,则没有折叠,不用进行其他操作。
不为null,则有折叠操作,v.setSelected(!v.isSelected());
,折叠,则展开,显示全文,未折叠,则折叠,显示从0到mLastEnd-1的字符串
IntroduceTv布局
<TextView
android:id="@+id/detail_introduce_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_30px_in720p"
android:drawableBottom="@drawable/detail_introduce_bottom_selector"
android:drawablePadding="@dimen/_14px_in720p"
android:paddingBottom="@dimen/_10px_in720p"
android:textColor="@color/color_white"
android:textSize="@dimen/_24px_in720p"
app:layout_constraintTop_toBottomOf="@id/episode_parT_cl"/>
detail_introduce_bottom_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/vod_more" android:state_selected="true"/>
<item android:drawable="@mipmap/vod_close"/>
</selector>
int maxline = 3;
boolean isFold;
int mLastEnd;
mIntroduceTv.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
mIntroduceTv.getViewTreeObserver().removeOnPreDrawListener(this);
if (mIntroduceTv.getLineCount() > maxline) {
mLastEnd = mIntroduceTv.getLayout().getLineEnd(maxline-1) - getResources().getString(R.string.detail_introduce).length();
mIntroduceTv.setText(getResources().getString(R.string.detail_introduce) + "\n" + mChannelBean.getDescription().substring(0, mLastEnd - 1));
mIntroduceTv.setSelected(true);
isFold = true;
} else {
mIntroduceTv.setCompoundDrawables(null, null, null, null);
}
return false;
}
});
mIntroduceTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mIntroduceTv.getCompoundDrawables()[3] == null) {
return;
}
v.setSelected(!v.isSelected());
if (isFold) {
mIntroduceTv.setText(getResources().getString(R.string.detail_introduce) + "\n" + mChannelBean.getDescription());
} else {
mIntroduceTv.setText(getResources().getString(R.string.detail_introduce) + "\n" + mChannelBean.getDescription().substring(0, mLastEnd - 1));
}
isFold = !isFold;
}
});