RecyclerView交换两个适配器

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

我想创建一个应用程序,显示最后15场比赛和接下来的15场比赛使用API​​。我用2个不同的列表视图创建了一个RecyclerView。一个用于最后一场比赛,另一个用于下一场比赛。我还创建了2个不同的适配器,所以当我单击不同的导航栏时,它会将视图(例如最后一个匹配)更改为另一个视图(下一个匹配)。但是,当我单击第二个按钮(下一个匹配)时,它强制停止。

我认为问题出在适配器集中,但我不知道在哪里

这是我的代码:

我激活了。

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.navigation_home -> {
            //Prev Match
            recyclerView_main.layoutManager = LinearLayoutManager(this)
            fetchJsons()
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_dashboard -> {
            //Next Match
            recyclerView_main.layoutManager = LinearLayoutManager(this)
            fetchJson()
            return@OnNavigationItemSelectedListener true
        }
    }
    false
}
fun fetchJsons(){
    //Json for prev match
    val url = "https://www.thesportsdb.com/api/v1/json/1/eventspastleague.php?id=4328"
    val request = Request.Builder().url(url).build()
    val client = OkHttpClient()
    client.newCall(request).enqueue(object: Callback{
        override fun onResponse(call: Call?, response: Response?){
            println("sukses")
            val body = response?.body()?.string()
            println(body)

            val gson = GsonBuilder().create()

            val prevMatch = gson.fromJson(body, PrevMatch::class.java)

            runOnUiThread { recyclerView_main.adapter = MainAdapters(prevMatch) }
        }
        override fun onFailure(call: Call?, e: IOException?){
            println("failed request")
        }
    })
}

// Next Match JSON
fun fetchJson(){
    val url = "https://www.thesportsdb.com/api/v1/json/1/eventsnextleague.php?id=4328"
    val request = Request.Builder().url(url).build()
    val client = OkHttpClient()
    client.newCall(request).enqueue(object: Callback{
        override fun onResponse(call: Call?, response: Response?){
            println("sukses")
            val body = response?.body()?.string()
            println(body)

            val gson = GsonBuilder().create()

            val nextMatch = gson.fromJson(body, NextMatch::class.java)

            runOnUiThread { recyclerView_main.adapter = MainAdapter(nextMatch) }
        }
        override fun onFailure(call: Call?, e: IOException?){
            println("failed request")
        }
    })
}
android api android-recyclerview kotlin adapter
1个回答
1
投票

这就是我要做的,我将创建两个recyclerView并在它们之间切换。并在它们之间划分API的数据。

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                //Prev Match
                recyclerView_Next_Match.visibility = View.GONE
                recyclerView_Prev_Match.visibility = View.VISIBLE
                fetchJsons()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                //Next Match
                recyclerView_Prev_Match.visibility = View.GONE
                recyclerView_Next_Match.visibility = View.VISIBLE
                fetchJson()
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.