如何将 onClickListener 实现到 recyclerView 以在单击时打开新片段

Edw*_*win 1 android android-fragments kotlin android-recyclerview

我正在尝试实现 onClickListener 来recyclerView打开一个新的片段,我知道它们在 stackoverflow 上已经很多了,但作为初学者,我发现它们有些复杂。

就像我说的,我想在点击时打开一个片段recyclerView,我有一些问题;

  • 主要是我该怎么做??

  • 如何控制我导航到的屏幕的视图(即文本视图、图像视图等)?

这是具有 recyclerView 的片段

class NewScreenFragment : Fragment() {

    private var _binding: FragmentNewScreenBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    // for the recyclerView
    private lateinit var newArrayList: ArrayList<DataClass>

    // variables to the recyclerView views
    lateinit var imageId: Array<Int>
    lateinit var title: Array<String>

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentNewScreenBinding.inflate(inflater, container, false)
        val root: View = binding.root

        imageId = arrayOf(
            R.drawable.ways_start_cpa_marketing,
            R.drawable.ways_start_gigging,
            R.drawable.ways_teach_english_online,
            R.drawable.ways_test_websites,
            R.drawable.ways_tutor_on_your_own_time,
            R.drawable.ways_use_your_voice,
            R.drawable.ways_write_a_list
        )

        title = arrayOf(
            getString(R.string.Become_A_Dog_Walker),
            getString(R.string.Become_A_Proofreader),
            getString(R.string.Become_A_Virtual_Assistant),
            getString(R.string.Make_Things_to_Sell),
            getString(R.string.Give_your_opinion),
            getString(R.string.Play_Games),
            getString(R.string.Start_A_Blog),
        )

        // this creates a vertical layout Manager
        binding.recyclerview.layoutManager = LinearLayoutManager(context)
        /** I change LinearLayoutManager(this)
         * to LinearLayoutManager(context)
         *
         * remember this in case of any issue
        **/

        binding.recyclerview.setHasFixedSize(true)

        newArrayList = arrayListOf<DataClass>()
        getUserData()


        return root
    }

    private fun getUserData() {

        for (i in imageId.indices){

            val dataClass = DataClass(imageId[i],title[i])
            newArrayList.add(dataClass)

        }

        // ArrayList of class ItemsViewModel
        // val data = ArrayList<DataClass>()

        // This loop will create Views containing
        // the image with the count of view

        // This will pass the ArrayList to our Adapter
        // val adapter = RecyclerAdapter(data)

        // Setting the Adapter with the recyclerview
        binding.recyclerview.adapter = RecyclerAdapter(newArrayList)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}
Run Code Online (Sandbox Code Playgroud)

这是 Adapter.kt

class RecyclerAdapter(private val mList: ArrayList<DataClass>) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {

    // create new views
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        // inflates the card_view_design view
        // that is used to hold list item
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.new_screen_recyclerview_look, parent, false)

        return ViewHolder(view)
    }

    // binds the list items to a view
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val dataClass = mList[position]

        // sets the image to the imageview from our itemHolder class
        holder.imageView.setImageResource(dataClass.cardImage)

        // sets the text to the textview from our itemHolder class
        holder.textView.text = dataClass.cardTitle

    }

    // return the number of the items in the list
    override fun getItemCount(): Int {
        return mList.size
    }

    // Holds the views for adding it to image and text
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.card_image)
        val textView: TextView = itemView.findViewById(R.id.card_title)

         // implement the onClickListener event here
        init {
            itemView.setOnClickListener {

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然不需要,但为了更清楚。这是我的数据类;

data class DataClass(var cardImage: Int, var cardTitle: String) {
}
Run Code Online (Sandbox Code Playgroud)

我真的很感谢你的帮助,提前感谢你..

Fil*_*ira 8

在 Adapter 构造函数上创建一个新参数 Change

class RecyclerAdapter(private val mList: ArrayList<DataClass>)
Run Code Online (Sandbox Code Playgroud)

class RecyclerAdapter(private val mList: ArrayList<DataClass>, private val onItemClicked: (DataClass) -> Unit)
Run Code Online (Sandbox Code Playgroud)

为您的项目设置 clickListener onBindViewHolder。如果你想让整个项目可点击,请设置:

holder.root.setOnClickListener{ 
   onItemClicked(dataClass)
}
Run Code Online (Sandbox Code Playgroud)

在您的片段上,创建适配器时,传递新参数

binding.recyclerview.adapter = RecyclerAdapter(newArrayList){ dataClass ->
   //Here, 'dataClass' will be the clicked item on recyclerView. Here you can do any logic that's supposed to happen after the click, like opening a new fragment passing 'dataClass' as parameter. 
}
Run Code Online (Sandbox Code Playgroud)

要打开一个传递 dataClass 作为参数的新片段,请选中“newInstance”模式。 了解 Fragment.newInstance 方法