Android recyclerview - 来自第二个适配器(Kotlin)的调用方法/函数

问题描述 投票:0回答:1

我在同一个列表中使用了两个适配器,但每个都有不同的排序。

这是adapterONE(我删除了几乎所有这个问题不必要的东西):

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val video = videolist[position]

    holder.title.text = video.id.toString()

    holder.title.setOnClickListener {
        hide(video.id)
    }
}

override fun getItemCount() = videolist.size

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

class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!){
    val title = itemView!!.videoviewTitle!!
}


fun hide(id: Int){

    var ppp = 0

    for (i in 0 until videolist.size) {

        if(videolist[i].id == id){
            ppp = i
            break
        }
    }

    videolist.removeAt(ppp)
    notifyItemRemoved(ppp)
}

现在调用hide函数我希望在第二个适配器中删除相同的项目,所以我尝试:

videolist.removeAt(ppp)
notifyItemRemoved(ppp)
MainActivity().adapterTWO.hide(id) // this is what I added

并得到错误:

lateinit property adapterTWO has not been initialized

但这不是真的,因为adapterTWO已经加载了内容

请提前帮助和感谢!

编辑:

这就是我在MainActivity中创建适配器的方法

lateinit var adapter: RecentAdapter
lateinit var adapterTrending: TrendingAdapter

fun loadVids(endvids: MutableList<Videos>){

    adapter = RecentAdapter(this@MainActivity, endvids, isfavorites)

    recyclerViewRecent.adapter = adapter

    recyclerViewRecent.layoutManager = LinearLayoutManager(this@MainActivity)
    recyclerViewRecent.setHasFixedSize(true)

}


fun loadVidsRecent(endvids: MutableList<Videos>){

    adapterTrending = TrendingAdapter(this@MainActivity, endvids, isfavorites)

    recyclerViewTrending.adapter = adapterTrending

    recyclerViewTrending.layoutManager = LinearLayoutManager(this@MainActivity)
    recyclerViewTrending.setHasFixedSize(true)
}
android kotlin android-recyclerview adapter
1个回答
0
投票

我认为你不情愿地创建新的MainActivity(在添加的代码中你正在调用它的构造函数)。如果您发布了一些MainActivity代码,它也会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.