Android 开发技术分享新的开始UI

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

2021-07-12  本文已影响0人  咚咚_Coding

实际效果展示

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

自定义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<String> des_ary = new ArrayList<>();
    public boolean isUnfold;
}
}
跟布局容器--item_coupon_mutil_text
<?xml version="1.0" encoding="utf-8"?>
<merge tools:parentTag="android.widget.LinearLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_instructions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools" />
xml 子Item容器--item_coupon_child_text_view
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/tv_instructions"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:textColor="#777777"
    android:textSize="12dp"
    android:gravity="top|center_vertical"

    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_goneMarginRight="15dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/tv_expanderview"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintWidth_default="wrap"
    tools:text="均可使均可使用均可使用均可使用均可使用均可使用" />

<ImageView
    android:id="@+id/tv_expanderview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="15dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:padding="4dp"
    android:src="@drawable/tv_arrow_down"
    android:textColor="#777777"
    android:textSize="12dp"
    android:visibility="invisible"
    app:layout_constraintLeft_toRightOf="@id/tv_instructions"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
  </androidx.constraintlayout.widget.ConstraintLayout>
外部How Use
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/tv_center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp"
    android:layout_marginRight="15dp"
    android:gravity="center"
    android:padding="2dp"
    android:text="Hello World!"
    android:textColor="@color/white"
    android:textSize="15dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<com.soyoung.syapp.MutilTextView
    android:id="@+id/mutil_tv"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="5dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/tv_center"
    app:layout_constraintTop_toTopOf="@id/tv_center" />
</androidx.constraintlayout.widget.ConstraintLayout>

End Thanks

上一篇 下一篇

猜你喜欢

热点阅读