Android TextView支持多行展开收起(含Icon)

实际效果展示

默认收起,第一行放不下展示…
收起文本较少且有多个子文本
展开所有子文本

自定义View

  /**
 * 名称:多行文本
 * Created by niudong on 2021/7/12 8:03 PM
 * Tel:18811793194
 */
public class MutilTextView extends LinearLayout {

private boolean isHasMutilLine = false;
private LinearLayout llInstructions;
private CollarInfoBean mData;
private LayoutInflater from;
private final int MAX_LINE_COUNT = 20;
private final int MIN_LINE_COUNT = 1;

public MutilTextView(@NonNull Context context) {
    this(context, null);
}

public MutilTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
}

public MutilTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView(context);
}

private void initView(Context context) {
    from = LayoutInflater.from(context);
    llInstructions = (LinearLayout) from.inflate(R.layout.item_coupon_mutil_text, this, true);
    llInstructions.setOrientation(VERTICAL);
    llInstructions.setGravity(Gravity.TOP);
    setListener();
}

private void setListener() {
    CollarInfoBean collarInfoBean = new CollarInfoBean();
    collarInfoBean.isUnfold = false;
    collarInfoBean.des_ary.add("啊厉害哈哈礼盒啊厉害哈哈礼盒啊厉害哈哈礼盒啊厉害哈哈礼盒啊厉害哈哈礼盒");
    collarInfoBean.des_ary.add("哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊厉害哈哈礼盒");
    collarInfoBean.des_ary.add("heheheh哈哈哈哈哈哈哈哈heheheh哈哈哈哈哈哈哈哈heheheh哈哈哈哈哈哈哈哈heheheh哈哈哈哈哈哈哈哈");
    collarInfoBean.des_ary.add("8932u924e9787927982379");
    setData(collarInfoBean);
}

public void setData(CollarInfoBean mode) {
    this.mData = mode;
    if (null == mode || null == mode.des_ary || mode.des_ary.isEmpty()) return;
    showList(mode);
}

private void showList(CollarInfoBean mode) {
    int count = mode.des_ary.size();
    llInstructions.removeAllViews();
    for (String data : mode.des_ary) {
        ConstraintLayout mChildView = (ConstraintLayout) from.inflate(R.layout.item_coupon_child_text_view, null);
        TextView mChildTextView = mChildView.findViewById(R.id.tv_instructions);
        ImageView iv_expanderview = mChildView.findViewById(R.id.tv_expanderview);
        mChildTextView.setText(data);
        //count>1
        if (llInstructions.getChildCount() == 0) {
            this.setOnClickListener(v -> {
                if (null != mData && isHasMutilLine) {
                    mData.isUnfold = !mData.isUnfold;
                    clickArrows(mData);
                }
            });
            mChildTextView.post(() -> {
                int countLine = mChildTextView.getLineCount();
                if (countLine > MIN_LINE_COUNT || count > MIN_LINE_COUNT) {
                    mChildTextView.setMaxLines(!mode.isUnfold ? MIN_LINE_COUNT : MAX_LINE_COUNT);
                    mChildTextView.setEllipsize(TextUtils.TruncateAt.END);
                    iv_expanderview.setImageResource(mData.isUnfold ? R.drawable.tv_arrow_up : R.drawable.tv_arrow_down);
                    iv_expanderview.setVisibility(VISIBLE);
                    isHasMutilLine = true;
                }
            });
        } else {
            //移除箭头
            mChildView.removeView(iv_expanderview);
            mChildView.setVisibility(mode.isUnfold ? VISIBLE : GONE);
        }
        llInstructions.addView(mChildView);
    }
}


private void clickArrows(CollarInfoBean mData) {
    try {
        if (llInstructions.getChildCount() == 0) return;
        ConstraintLayout constraintLayout = (ConstraintLayout) llInstructions.getChildAt(0);
        ((TextView) constraintLayout.getChildAt(0)).setMaxLines(!mData.isUnfold ? MIN_LINE_COUNT : MAX_LINE_COUNT);
        ((ImageView) constraintLayout.getChildAt(1)).setImageResource(mData.isUnfold ? R.drawable.tv_arrow_up : R.drawable.tv_arrow_down);
        for (int i = 0; i < llInstructions.getChildCount(); i++) {
            View childAt = llInstructions.getChildAt(i);
            if (mData.isUnfold) {
                childAt.setVisibility(VISIBLE);
            } else {//收起
                if (i == 0) continue;
                childAt.setVisibility(GONE);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }


private static class CollarInfoBean {
    public List des_ary = new ArrayList<>();
    public boolean isUnfold;
}
}
跟布局容器--item_coupon_mutil_text


xml 子Item容器--item_coupon_child_text_view






  
外部How Use







End Thanks

你可能感兴趣的:(Android TextView支持多行展开收起(含Icon))