我正在尝试在recyclerview
中进行多重查看,但是当我滚动查看时,数据不见了,列表形式很混乱,即使数据已完成也不会停止。所以我不知道发生了什么。但是我认为适配器有问题,但是我不知道它是什么。
我的活动
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_last, container, false)
rootView.recyclerView_main.layoutManager = LinearLayoutManager(activity)
fetchJson()
return rootView
}
fun fetchJson(){
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, okhttp3.Callback {
override fun onResponse(call: okhttp3.Call?, response: okhttp3.Response?) {
val body = response?.body()?.string()
println(body)
val gson = GsonBuilder().create()
val teamFeed = gson.fromJson(body, TeamFeed::class.java)
runOnUiThread {
recyclerView_main.adapter = MainAdapter(teamFeed)
recyclerView_main.recycledViewPool.setMaxRecycledViews(0, 0)
}
}
override fun onFailure(call: okhttp3.Call?, e: IOException) {
println("failed")
}
})
}
class TeamFeed(val events: List<Results>)
class Results(val strHomeTeam: String, val strAwayTeam: String, val intHomeScore: String, val intAwayScore: String, val dateEvent: String)
我的适配器
class MainAdapter(val teamFeed: LastActivity.TeamFeed) :
RecyclerView.Adapter<MainAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_layout,
parent, false))
}
override fun getItemCount(): Int {
return teamFeed.events.count()
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val team = teamFeed.events.get(position)
holder.itemView.home_team?.text = team.strHomeTeam
holder.itemView.away_team?.text = team.strAwayTeam
holder.itemView.home_score?.text = team.intHomeScore
holder.itemView.away_score?.text = team.intAwayScore
holder.itemView.tanggal?.text = team.dateEvent
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
}
如果有人知道如何解决,请提供帮助。真的很感激。