如何将基本适配器更改为Recycler View适配器?

时间:2020-10-17 08:15:38

标签: android kotlin android-recyclerview

我是编程的新手,试图弄清楚如何在recyclerview中流式传输.mp3文件

如何将我的基本适配器转换为Recycler View适配器?有简单的方法吗?

我需要这样做,因为最终在回收站的每个视图中我都需要一个搜索栏

主要活动中的我的基本适配器:

class Adapter(context: Context, private val List: ArrayList<Model>) : RecyclerView.Adapter<Adapter.ViewHodler>() {

    private val mContext: Context

    init {
        mContext = context
    }
    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): Adapter.ViewHodler {
        val row = LayoutInflater.from(mContext).inflate(R.layout.list_item,parent,false)
        return ViewHodler(row)
    }

    override fun getItemCount(): Int {
        return List.size
    }


    inner class ViewHodler(v : View) : RecyclerView.ViewHolder(v) {
        val viewTitle = v.title
    }

    override fun onBindViewHolder(holder: ViewHodler, position: Int) {
        val model = List[position]
        holder.viewTitle!!.text = model.Title
    }
}

我的空Recycler View适配器:

Notice: Trying to access array offset on value of type bool in /homepages/30/d834969130/htdocs/clickandbuilds/Freiraum4ImmobilienundFinanzierung/wp-content/plugins/onoffice-for-wp-websites/plugin/EstateList.php on line 375

Notice: Trying to access array offset on value of type bool in /homepages/30/d834969130/htdocs/clickandbuilds/Freiraum4ImmobilienundFinanzierung/wp-content/plugins/onoffice-for-wp-websites/plugin/EstateList.php on line 376

1 个答案:

答案 0 :(得分:1)

`
inner class ViewHodler(v : View) : RecyclerView.ViewHolder(v) {
    val viewTitle = v.findViewById(R.id.title) as TextView
    val button = v.findViewById(R.id.button) as Button
}

override fun onBindViewHolder(holder: ViewHodler, position: Int) {
    val model = List[position]
    holder.viewTitle?.text = model.Title
    holder.button.setOnClickListener {
        if(holder.button.text == "STOP") {
           mp?.stop()
           myview.button.text = "PLAY"
        }else{
           mp = MediaPlayer()
           try {
              mp?.setDataSource(Song.SongURL)
              mp?.prepare()
              mp?.start()
              myview.button.text = "STOP"
           }catch (e:Exception){}
         }
     }
 }`

您还可以避免使用onBindViewHolder()中的代码,并创建类似于ViewHolder的方法onBind(file : SongFile)并将对象传递给该方法。