0

我现在面临着一种奇怪的行为。我正在使用视图绑定来引用 id。当 RecyclerView 高度为 wrap_content 时,项目显示完美,但是当我设置 recyclerview 高度 match_parent 时,项目宽度会在运行时自动包装(wrap_content),但在 xml 中我已将项目宽度设置为 match_parent。

请帮我解决这个问题。

请查看我的 xml 和 kt 文件-:

<LinearLayou ------
 <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/idRvTrainerReview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

行项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/_1sdp"
    android:background="@color/colorWhite"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="@dimen/_10sdp">


    <TextView
        android:id="@+id/idTvDate"
        style="@style/MediumText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="14 feb"
        android:includeFontPadding="false"
        android:textColor="@color/colorBlack"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/idTvStartTime"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center"
        android:includeFontPadding="false"
        android:text="19:30" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_right_arrow" />

</LinearLayout>


适配器文件-:


class FeedbackDataAdapter(
    private var mcontext: Context,
    private var mItemList: MutableList<ResponseTrainerFeedback.Result.Training>,
    var mClickListener: OnClickListener?
) : RecyclerView.Adapter<TrainingDataViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrainingDataViewHolder {
       var layoutInflater =   mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as (LayoutInflater)
        val rowEventMemberBinding =
            RowEventDataBinding.inflate(layoutInflater, parent, false)
        return TrainingDataViewHolder(rowEventMemberBinding)
    }

    override fun getItemCount(): Int =  mItemList.size

    override fun onBindViewHolder(holder: TrainingDataViewHolder, position: Int) {
        holder.mBinding.idTvDate.text = mItemList[holder.adapterPosition].trainingName
        holder.mBinding.idTvStartTime.invisible()

        holder.itemView.setOnClickListener {
            if(mItemList.size > 0){
                mClickListener?.onItemClick(holder.adapterPosition)
            }
        }

    }

    fun refreshList(mFeedbackList: MutableList<ResponseTrainerFeedback.Result.Training>) {
        this.mItemList = mFeedbackList
        notifyDataSetChanged()
    }

}

class TrainingDataViewHolder(var mBinding: RowEventDataBinding) :
    RecyclerView.ViewHolder(mBinding.root) {

}

在此处输入图像描述

4

1 回答 1

0

我已经尝试了您的代码并且它正在工作。

我只根据Android 培训重构了您的适配器,如果您想做代码实验室:https ://codelabs.developers.google.com/codelabs/kotlin-android-training-diffutil-databinding

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TrainingDataViewHolder {
       return TrainingDataViewHolder.from(parent)
    }

    override fun getItemCount(): Int =  mItemList.size

    override fun onBindViewHolder(holder: TrainingDataViewHolder, position: Int) {
        holder.bind(mItemList[position])
        holder.itemView.setOnClickListener {
            if(mItemList.size > 0){
                mClickListener?.onItemClick(holder.adapterPosition)
            }
        }
    }

    fun refreshList(mFeedbackList: MutableList<ResponseTrainerFeedback.Result.Training>) {
        this.mItemList = mFeedbackList
        notifyDataSetChanged()
    }

}

class TrainingDataViewHolder(var mBinding: RowEventDataBinding) :
    RecyclerView.ViewHolder(mBinding.root) {

    fun bind(data: YOURDATA) {
        mBinding.idTv = data // it'll map automatically all the property, replace idTv by the name of your variable in your layout
        mBinding.idTvStartTime.invisible()
    }


    companion object {
        fun from(parent: ViewGroup): TrainingDataViewHolder {
            return TrainingDataViewHolder(RowEventDataBinding.inflate(LayoutInflater.from(parent.context), parent, false))
        }
    }

}

行项目

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>
        <variable
            name="idTv"
            type="package.name.to.data" />
    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="10dp">

        <TextView
            android:id="@+id/idTvDate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:text="@{idTv.idTvDate}"
            android:includeFontPadding="false"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            tools:text="14 feb"/>

        <TextView
            android:id="@+id/idTvStartTime"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:gravity="center"
            android:includeFontPadding="false"
            android:text="19:30" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/arrow_up_float" />

    </LinearLayout>
</layout>

<layout>PS:我在您的中没有看到,row_item您确定您的绑定正确吗?

在此处输入图像描述

于 2020-05-29T07:57:09.040 回答