Android recyclerview-从第二个适配器(Kotlin)调用方法/函数 [英] Android recyclerview - call method / function from second adapter (Kotlin)

查看:340
本文介绍了Android recyclerview-从第二个适配器(Kotlin)调用方法/函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using two adapters for the same list but each is differently sorted.

这是adapterONE(我删除了此问题几乎不需要的所有内容):

This is the adapterONE (I removed almost everything non necessary for this question):

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函数之后,我希望在第二个适配器中删除同一项目,所以我尝试了:

Now afer calling the hide function I want the same item to be removed in the second adapter so I tried:

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

并得到错误:

lateinit property adapterTWO has not been initialized

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

But this isn't true because adapterTWO has loaded the content

请事先提供帮助和感谢!

Please help and thanks in advance!

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

This is how I create the adapters in 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)
}

推荐答案

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

I think you're unwillingly creating new MainActivity (in the added code you're calling its constructor). It'd also help if you posted some of MainActivity code

这篇关于Android recyclerview-从第二个适配器(Kotlin)调用方法/函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆