首页 > 解决方案 > 自定义视图出现问题,如果在同一布局中添加多个自定义视图

问题描述



在我的项目中,我有一个自定义视图ExpandableTextView,它是通过膨胀以下布局创建的widget_expandable_textview

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:background="@color/appDesignDarkTwo"
        style="@style/widgetContainer"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvHeader"
        style="@style/textLatoRegularWhite14sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/ivDropDown"
        app:srcCompat="@drawable/ic_down_arrow_blue"
       android:padding="@dimen/_5sdp"
        app:layout_constraintBottom_toBottomOf="@+id/tvHeader"
        app:layout_constraintTop_toTopOf="@+id/tvHeader"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvContent"
        style="@style/textLatoRegularWhite2_10sp"
        android:layout_marginTop="@dimen/_15sdp"
        android:layout_marginRight="@dimen/_11sdp"
        app:layout_constraintTop_toBottomOf="@+id/tvHeader"
        app:layout_constraintLeft_toLeftOf="@+id/tvHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:visibility="gone"
            android:layout_marginTop="@dimen/_15sdp"
            android:layout_marginRight="@dimen/_11sdp"
            app:layout_constraintTop_toBottomOf="@+id/tvHeader"
            app:layout_constraintLeft_toLeftOf="@+id/tvHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>



ExpandableTextView我使用方法将适配器设置为recyclerview

fun setBulletedList(texts: List<String>?){
        mIsRecylcerViewContent = true
        mView.recyclerView.visibility = View.VISIBLE
        var adapter = ExpandableTextViewBulletedTextAdapter()
        var layoutManager = LinearLayoutManager(context)
        mView.recyclerView.layoutManager = layoutManager
        texts?.let {
            adapter.setList(texts)
        }
        mView.recyclerView.adapter = adapter
        adapter.notifyDataSetChanged()
    }


ExpandableTextView在布局中添加了这个

            <ExpandableTextView
                android:id="@+id/ddOfferDetails"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimen_10dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cvHeaderLayout" />

            <ExpandableTextView
                android:id="@+id/ddTermsAndConditions"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimen_10dp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/ddOfferDetails" /> 



从我的片段中,当我从 API 获取报价和条款和条件列表时,我通过调用将其传递到各自的视图

ddOfferDetails.setBulletedList(LIST_OF_OFFER)
ddTermsAndConditions.setBulletedList(LIST_OF_TERMS_AND_CONDITIONS)

现在问题是,在视图中我可以看到条款和条件列表设置为,ddOfferDetails并且没有任何列表设置为ddTermsAndConditions
我观察到ddOfferDetails首先显示报价详细信息列表,然后在几秒钟后显示条款和条件列表。

如何解决这个问题呢 ?
我想ddOfferDetails显示报价详情列表并ddTermsAndConditions显示条款和条件列表。
请帮助
提前谢谢

干杯

标签: androidkotlinandroid-custom-view

解决方案


推荐阅读