带有kotlin MVVM和数据绑定的recyclerview中的单选按钮
我需要更新videModel中的列表,并设置所选位置以查找它 选择,取消选择其他打开。请建议如何在列表中的单选按钮中仅选择一个。 这是我的适配器:-带有kotlin MVVM和数据绑定的recyclerview中的单选按钮,kotlin,android-recyclerview,android-databinding,2-way-object-databinding,android-mvvm,Kotlin,Android Recyclerview,Android Databinding,2 Way Object Databinding,Android Mvvm,我需要更新videModel中的列表,并设置所选位置以查找它 选择,取消选择其他打开。请建议如何在列表中的单选按钮中仅选择一个。 这是我的适配器:- class DocTypeListAdapter : RecyclerView.Adapter<DocTypeListAdapter.ViewHolder>() { private lateinit var postList: List<DriverType> override fun onCreateVi
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
}
}
}
类DocTypeListAdapter:RecyclerView.Adapter(){
私有lateinit var postList:List
重写CreateViewHolder(父级:ViewGroup,viewType:Int):DocTypeListAdapter.ViewHolder{
val绑定:ItemDocumentTypeBinding=
DataBindingUtil.inflate(LayoutFlater.from(parent.context),R.layout.item\u document\u type,parent,false)
返回视图保持器(绑定)
}
覆盖BindViewHolder(holder:DocTypeListAdapter.ViewHolder,位置:Int){
绑定(postList[position],position,mSelectedItem)
}
重写getItemCount():Int{
如果(::postList.isInitialized)postList.size否则为0,则返回
}
乐趣更新列表(postList:List){
this.postList=postList
notifyDataSetChanged()
}
类ViewHolder(私有val绑定:ItemDocumentTypeBinding):RecyclerView.ViewHolder(binding.root){
private val viewModel=DocTypeListViewModel()
趣味绑定(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
}
}
}
视图模型如下:-
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()
}
}
类DocTypeListViewModel:BaseViewModel(){
private val postTitle=MutableLiveData()
private val postBody=MutableLiveData()
val isChecked=MutableLiveData()
私有变量mSelectedItem=-1
趣味绑定(post:DriverType,position:Int,selectedPosition:Int){
postTitle.value=post.driver\u类型
postBody.value=post.description
如果((selectedPosition==1&&position==0))
isChecked.value=true
其他的
如果(selectedPosition==位置)
isChecked.value=true
其他的
isChecked.value=false
}
fun getPostTitle():MutableLiveData{
回帖
}
fun getPostBody():MutableLiveData{
回邮体
}
fun getIsChecked():MutableLiveData{
返回已检查
}
趣味阅读按钮点击(选择位置:Int,位置: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
}
@BindingAdapter(“适配器”)
fun setAdapter(视图:RecyclerView,适配器:RecyclerView.adapter){
view.adapter=适配器
}
我通常这样做:
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
}
}
}
classyourAdapter(val selected:(YourClassType)->Unit):RecyclerView.Adapter(){
//覆盖视图保持器
//重写getItemCount()
//覆盖BindViewHolder
内部类YourViewHolder(val绑定:YourClassItemBinding):RecyclerView.ViewHolder(binding.root){
乐趣设置数据(项目:YourItem){
binding.radioButton.setOnClickListener{
binding.radioButton.checked=true
selected.invoke(item)//如果在活动中需要该项,请使用此选项
}
//binding.viewModel.setData
}
}
}
但您可能需要创建自己的函数,以便在选中某个单选按钮时取消选中列表中的其他单选按钮项
编辑
我认为将适配器放在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.
}
}
class YourActivity:AppCompatActivity(){
变量listRv:RecyclerView?=null
var viewModel=YourViewModel()
var adapter=YourAdapter{
//在此处列出当用户单击某个项目并且“调用”适配器所选val时要执行的操作
}
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate()
val binding=DataBindingUtil.setContentView(this,R.layout.activity\u your\u布局)
binding.viewModel=viewModel
listRv=binding.yourListId
listRv?.adapter=adapter//在此处设置列表适配器
listRv?.layoutManager=LinearLayoutManager(此)
//等等。
}
}
如果您使用的是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()
val item=YourModel()
可观察的,可观察的
可观测=可观测。可观测(列表)
observable.map{model->
model.isChecked=filter.modelType===item.modelType
}
}.toList().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe()
映射操作使用更新的列表后,它将产生您想要的结果。布局(RecyFlerView)或适配器中的更改是什么?@UdayNayak您的意思是什么?哪些变化?