我的效果圖:

1.gif
第一步:布局,非常簡單一個TextView和一個checkBox復選框
<LinearLayout
android:id="@+id/content_lin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
>
<TextView
android:id="@+id/zhuanfa_head"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="5"
android:textColor="#4c5264"
android:textSize="13sp" />
<CheckBox
android:id="@+id/check_zhankai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null" //去掉框圖,只顯示文字
android:text="全文"
android:textSize="13sp"
android:visibility="gone" />
</LinearLayout>
第二步:在Adapter適配器中寫個邏輯。headTv(文字的TextView)/ch是展開和收起按鈕
//展開文字
final CheckBox ch = baseViewHolder.getView(R.id.check_zhankai);
headTv.post(new Runnable() { //用post方法是因為當view繪制完獲取信息,不然可能第一次獲取不到正確的行數(shù)。
@Override
public void run() {
dataBean.setMaxLines(headTv.getLineCount()); //保存一個最大行數(shù),在集合對應(yīng)的對象中
if (headTv.getLineCount() > 5) { //如果大于5行
ch.setVisibility(View.VISIBLE); //展開按鈕顯示
} else {
ch.setVisibility(View.GONE); //否則,展開按鈕隱藏
}
}
});
//當點擊按鈕發(fā)生改變
ch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
dataBean.setCheckZK(isChecked); //這里在你的對象集合中記錄一個狀態(tài)值,防止滑動checkBox選中狀態(tài)錯亂
if (isChecked) {
ch.setText("收起");
headTv.setMaxLines(dataBean.getMaxLines());
headTv.postInvalidate();
} else {
headTv.setMaxLines(5);
headTv.postInvalidate(); //刷新控件,不加的話,如果下拉刷新列表notifyDataSetChanged()的時候TextView會自動重繪
ch.setText("全文");
}
}
});
ch.setChecked(dataBean.isCheckZK()); //隨時釋放選中狀態(tài)