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

时间:2019-02-23 23:38:12

标签: android kotlin android-recyclerview adapter

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

这是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)
}

1 个答案:

答案 0 :(得分:0)

我认为您不愿意创建新的MainActivity(在添加的代码中,您将其称为构造函数)。 如果您发布了一些MainActivity代码