【问题标题】:4 clickable views in one Iteme of recyclerView (kotlin)recyclerView (kotlin) 的一项中的 4 个可点击视图
【发布时间】:2020-04-30 11:11:13
【问题描述】:

我想在 recyclerView 的项目中设置许多按钮(我的意思是可点击的视图),就像每个项目都有下载、播放、停止、删除我该怎么做?如果我 setOnlickListener 像 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>

【问题讨论】:

  • 你好 safa,欢迎来到 StackOverflow!请考虑通过包含相关的适配器项目布局和其他相关代码来提供minimal reproducible example
  • 谢谢,现在好吗?
  • 能否包含recycler_view.xml 布局文件?
  • 是的,我添加了...我现在应该做什么?

标签: android kotlin android-recyclerview


【解决方案1】:

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

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

在你的适配器onBindViewHolder你可以添加

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

}

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

【讨论】:

    【解决方案2】:

    IMO 您必须为每个按钮附加一个点击侦听器,然后您可以对适配器/活动进行回调并从那里开始处理它。

    如果您是 View.OnClickListener,那么您始终拥有被点击的视图。从那里开始,只需编写一个 switch 语句,您可以在其中通过单击视图的 id 确定选择了哪个操作。

    如果您需要额外的数据,您可以在视图中添加一个标签,然后通过 clickListener 传递它。

    【讨论】:

      猜你喜欢
      • 2018-08-26
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多