如何扩展recyclerview?

enxuqcxy  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(319)

我试图用SelectionEnable元素制作一个列表。我可以点击按钮,它选择元素,但它也会定期选择列表中的其他元素。我注意到,例如,当我选择第一个元素时,所有十个元素都被选中。这对我来说显然是个问题。。。你能帮我吗?这是我的活动

val transactionChooser = supportFragmentManager.beginTransaction()
        transactionChooser.replace(R.id.container_lowerLegs, ChooserLowerLegs(this))
        transactionChooser.addToBackStack(null)
        transactionChooser.commit()

这是我的适配器

class SelectionAdapter(
        val context: MyActivity1,
        private val chooserList: ArrayList<ChooserModel>)
    : RecyclerView.Adapter<SelectionAdapter.ViewHolder>(){

    //boite pour ranger les composants à contrôler
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val exerciceImage: ImageView = view.findViewById(R.id.image_selection)
        val likedIcon: ImageView = view.findViewById(R.id.button_liked)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_selection, parent, false)

        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // récupérer les infos du muscle group
        val currentItem = chooserList[position]

        //vérifier si item liked
        fun updateStar() {
            if (currentItem.liked) {
                holder.likedIcon.setImageResource(R.drawable.ic_check_foreground)
            } else {
                holder.likedIcon.setImageResource(R.drawable.ic_add_foreground)
            }
        }

        //mettre à jour les infos du muscle group
        holder.exerciceImage.setImageResource(currentItem.drawable)
        holder.likedIcon.setOnClickListener {
            currentItem.liked = !currentItem.liked
            updateStar()
        }
    }
    override fun getItemCount(): Int = chooserList.size
}

最后是我的片段:

open class ChooserLowerLegs(
            private val context: MyActivity1
    ) : Fragment() {
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                                  savedInstanceState: Bundle?): View? {
            val view = inflater.inflate(R.layout.grid_list_item, container, false)

            val chooserList = arrayListOf<ChooserModel>()
            chooserList.add(
                    ChooserModel(
                            R.string.home_page_first_button,
                            R.string.first_picture,
                            R.drawable.bas_des_jambes_1,
                            false
                    )
            )
            chooserList.add(
                    ChooserModel(
                            R.string.home_page_first_button,
                            R.string.second_picture,
                            R.drawable.bas_des_jambes_3,
                            false
                    )
            )
            chooserList.add(
                    ChooserModel(
                            R.string.home_page_first_button,
                            R.string.third_picture,
                            R.drawable.bas_des_jambes_5,
                            false
                    )
            )

            // récupérer le recyclerview

            val verticalRecyclerView = view.findViewById<RecyclerView>(R.id.Vertical_recycler_view_grid)
            verticalRecyclerView.adapter = SelectionAdapter(context, chooserList)
            verticalRecyclerView.addItemDecoration(MuscleItemDecoration())
            return view
        }
}

此外,我想在网格中显示我的项目,而不是在线性视图中,但我不知道如何做到这一点?

hwazgwia

hwazgwia1#

您必须将布局管理器设置为回收视图,
要设置具有2列的gridlayout,请执行以下操作:

val layoutManager = GridLayoutManager(mContext, 2)
val mAdapter = YourAdapter(context = activity!!,list = list)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = mAdapter

现在,从列表中的10条记录中选择一条。在模型类中获取一个布尔变量,并将其传递到适配器类中。单击recycleview的项时,使用for循环将另一个项布尔变量设置为false,并将要显示的项设置为选中项,在for循环完成后将其设置为true。最后,使用您在模型类中使用的布尔变量在适配器类中显示一些标记为选定的写代码:
例如:recycleview项的侦听器

mAdapter.clickListener{ pos, model ->
   var entity = model as YourModel

   list.forEach {
      model.isSelected = false
   }
   list[i].isSelected = true   
   mAdapter.notifyDataSetChanged()
}

相关问题