首页 > 解决方案 > recyclerView (kotlin) 的一项中的 4 个可点击视图

问题描述

我想在 recyclerView 项目中设置许多按钮(我的意思是可点击的视图),就像每个项目都有下载、播放、停止、删除我该怎么做?如果我 setOnlickListener like holder.download_image.setOnClickListenr(){}它适用于所有项目我希望每个项目都有不同的下载链接

class RecyclerAdapter(private val soundList: List<SoundItem>,private val mListener: AdapterView.OnItemClickListener?) :
    RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>() {



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {


        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.recycler_view, parent, false)
        return RecyclerViewHolder(itemView)
    }



    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        val currnetItem = soundList[position]
        holder.soundNmae.text = currnetItem.soundName
        holder.play_image.setImageResource(currnetItem.playImage)
        holder.stop_image.setImageResource(currnetItem.stopImage)
        holder.download_image.setImageResource(currnetItem.donloadImage)
        holder.delete_image.setImageResource(currnetItem.deleteImage)



    }

    override fun getItemCount() = soundList.size
    class RecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val soundNmae = itemView.soundName
        val play_image = itemView.play_image
        val stop_image = itemView.stop_image
        val download_image = itemView.download_image
        val delete_image = itemView.delete_image



    }     

}

这是 recyclerView 的活动,我在 kotlin 中是新的,对不起,如果我的问题可能很简单,或者我无法说出我的意思

class RelaxSoundActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_relax_sound)
        val exampleList=generatorSoundList(500)
        recyceler_view.adapter=RecyclerAdapter(exampleList)
        recyceler_view.layoutManager=LinearLayoutManager(this)
        recyceler_view.setHasFixedSize(true)
    }


    fun generatorSoundList(size:Int ):List<SoundItem>{
         val list=ArrayList<SoundItem>()
        for(i in 0 until size){
            val drawable=when(i % 5){
                0->R.drawable.ic_play_arrow_black_24dp
                1->R.drawable.ic_play_arrow_black_24dp
                2->R.drawable.ic_play_arrow_black_24dp
                3->R.drawable.ic_play_arrow_black_24dp
                else->R.drawable.ic_play_arrow_black_24dp
            }
            val drawable2=when(i % 5){
                0->R.drawable.ic_stop_black_24dp
                1->R.drawable.ic_stop_black_24dp
                2->R.drawable.ic_stop_black_24dp
                3->R.drawable.ic_stop_black_24dp
                else->R.drawable.ic_stop_black_24dp
            }
            val drawable3=when(i % 5){
                0->R.drawable.ic_file_download_black_24dp
                1->R.drawable.ic_file_download_black_24dp
                2->R.drawable.ic_file_download_black_24dp
                3->R.drawable.ic_file_download_black_24dp
                else->R.drawable.ic_file_download_black_24dp
            }
            val drawable4=when(i % 5){
                0->R.drawable.ic_delete_black_24dp
                1->R.drawable.ic_delete_black_24dp
                2->R.drawable.ic_delete_black_24dp
                3->R.drawable.ic_delete_black_24dp
                else->R.drawable.ic_delete_black_24dp
            }
            val item=SoundItem("item $i" ,drawable,drawable2,drawable3,drawable4)
            list+=item

        }
        return list
    }
}

recycler_view.xml 文件在这里

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="wrap_content"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="8dp"
       >


        <TextView
            android:id="@+id/soundName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"
            android:textSize="20dp"/>

        <ImageView
            android:id="@+id/play_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_play_arrow_black_24dp" />

        <ImageView
            android:id="@+id/stop_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_stop_black_24dp" />

        <ImageView
            android:id="@+id/download_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_file_download_black_24dp" />

        <ImageView
            android:id="@+id/delete_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_delete_black_24dp" />

    </LinearLayout>
</androidx.cardview.widget.CardView>

标签: androidkotlinandroid-recyclerview

解决方案


你可以通过多种方式做到这一点。您可以在视图上添加点击侦听器并直接在适配器中执行操作,或者您可以在回收器视图持有者类中传递函数。

所以对于捷径,我可以给你一个你可以理解这个过程的方式。

在您的适配器onBindViewHolder中,您可以添加

holder.download_image.setOnCLickListener{
   val downloadLink = currnetItem.getDownloadLink()
   // Download task start by this download link

}

因此,下载图像上的这个单击监听器将对每个项目产生影响并单独执行。


推荐阅读