RecyclerView 交换两个适配器
RecyclerView swap two adapters
我想制作一个应用程序,使用 API 显示最后 15 场比赛和接下来的 15 场比赛。
我创建了一个具有 2 个不同列表视图的 RecyclerView。一个用于上一场比赛,另一个用于下一场比赛。我还创建了 2 个不同的适配器,所以当我单击不同的导航栏时,它会将视图(例如上一场比赛)更改为另一个视图(下一场比赛)。但是,当我单击第二个按钮(下一场比赛)时,它会强制停止。
我认为问题出在适配器设置上,但我不知道在哪里
这是我的代码:
MainActivity.kt
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")
}
})
}
这是我要做的,我将创建两个 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
}
我想制作一个应用程序,使用 API 显示最后 15 场比赛和接下来的 15 场比赛。 我创建了一个具有 2 个不同列表视图的 RecyclerView。一个用于上一场比赛,另一个用于下一场比赛。我还创建了 2 个不同的适配器,所以当我单击不同的导航栏时,它会将视图(例如上一场比赛)更改为另一个视图(下一场比赛)。但是,当我单击第二个按钮(下一场比赛)时,它会强制停止。
我认为问题出在适配器设置上,但我不知道在哪里
这是我的代码:
MainActivity.kt
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")
}
})
}
这是我要做的,我将创建两个 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
}