recyclerview中的单选按钮,带有kotlin MVVM和数据绑定

问题描述 投票:0回答:2

使用kotlin,dagger2,retrofit,livedata,MVVM和数据绑定的recyclerview中的Redio按钮,我需要来自videModel的更新列表,并且还设置选定的位置以找到它被选中而取消选择其他位置。请建议如何在List中的RadioButton中只选择一个。这是我的适配器: -

class DocTypeListAdapter : RecyclerView.Adapter<DocTypeListAdapter.ViewHolder>() {
    private lateinit var postList: List<DriverType>

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DocTypeListAdapter.ViewHolder {
        val binding: ItemDocumentTypeBinding =
            DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_document_type, parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: DocTypeListAdapter.ViewHolder, position: Int) {
        holder.bind(postList[position], position, mSelectedItem)
    }

    override fun getItemCount(): Int {
        return if (::postList.isInitialized) postList.size else 0
    }

    fun updatePostList(postList: List<DriverType>) {
        this.postList = postList
        notifyDataSetChanged()
    }

    class ViewHolder(private val binding: ItemDocumentTypeBinding) : RecyclerView.ViewHolder(binding.root) {
        private val viewModel = DocTypeListViewModel()

        fun bind(post: DriverType, position: Int, selectedPosition: Int) {
//            viewModel.bind(post,true)
            viewModel.bind(post,position,selectedPosition)

//             mRadioButton : RadioButton = binding.radioButton;



            viewModel.readioButtonClicked(getAdapterPosition(),position)
            binding.viewModel = viewModel
        }
    }
}

viewModel喜欢: -

class DocTypeListViewModel : BaseViewModel() {
    private val postTitle = MutableLiveData<String>()
    private val postBody = MutableLiveData<String>()
     val isChecked = MutableLiveData<Boolean>()
    private var mSelectedItem = -1


    fun bind(post: DriverType, position: Int, selectedPosition: Int) {
        postTitle.value = post.driver_type
        postBody.value = post.description

        if ((selectedPosition == -1 && position == 0))
            isChecked.value = true
        else
            if (selectedPosition == position)
                isChecked.value = true
            else
                isChecked.value = false

    }

    fun getPostTitle(): MutableLiveData<String> {
        return postTitle
    }

    fun getPostBody(): MutableLiveData<String> {
        return postBody
    }

    fun getIsChecked(): MutableLiveData<Boolean> {
        return isChecked
    }

    fun readioButtonClicked(selectedPosition: Int, pos:Int) {
        mSelectedItem = selectedPosition

        //            mSelectedItem=getAdapterPosition()
//            notifyDataSetChanged()

    }
}

我的布局: -

<android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:adapter="@{viewModel.getDocTypeListAdapter()}"

                android:layout_marginTop="8dp"
                app:layout_constraintTop_toBottomOf="@+id/textView9"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_marginStart="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="8dp"
                android:id="@+id/recyclerView"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toTopOf="@+id/button"/>

绑定适配器: -

@BindingAdapter("adapter")
fun setAdapter(view: RecyclerView, adapter: RecyclerView.Adapter<*>) {
    view.adapter = adapter
}
kotlin recycler-adapter android-databinding 2-way-object-databinding android-mvvm
2个回答
0
投票

我通常喜欢这样:

class YourAdapter(val selected : (YourClassType) -> Unit) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    // override fun onCreateViewHolder

    // override fun getItemCount()

    // override fun onBindViewHolder

    inner class YourViewHolder(val binding: YourClassItemBinding) : RecyclerView.ViewHolder(binding.root) {
        fun setData(item : YourItem) {
            binding.radioButton.setOnClickListener {
                binding.radioButton.checked = true
                selected.invoke(item) // use this if you need the item in your activity
            }
            // binding.viewModel.setData
        }
    }

}

但是,当您选择一个时,您可能需要创建自己的函数来取消选中列表中的其他radioButtons项。

编辑

我不认为将适配器放在xml文件中是一个好习惯。

我通常喜欢这个,在“拥有”RecyclerView的活动中:

class YourActivity : AppCompatActivity() {

    var listRv : RecyclerView? = null

    var viewModel = YourViewModel()

    var adapter = YourAdapter {
        // Put here the actions you want to achieve when a user click on an item and the adapter selected val is "invoked"
    }

    override fun onCreate(savedInstanceState : Bundle?) {
        super.onCreate()

        val binding = DataBindingUtil.setContentView<ActivityYourBinding>(this, R.layout.activity_your_layout)
        binding.viewModel = viewModel

        listRv = binding.yourListId
        listRv?.adapter = adapter // Set your list adapter here
        listRv?.layoutManager = LinearLayoutManager(this)


        // etc.
    }
}

0
投票

如果您使用rxjava / rxkotlin,那么map运算符将帮助您实现上述目标

val item=YourModel()   
val observable:Observable<YourModel>
observable=Observable.fromIterable(list)
observable.map { model->
    model.isChecked = filter.modelType===item.modelType
    }
}.toList().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe()

在地图操作使用更新列表后,它将生成您想要的结果。

© www.soinside.com 2019 - 2024. All rights reserved.