Список RecyclerView исчез при прокрутке вниз - PullRequest
0 голосов
/ 01 ноября 2018

Я пытаюсь сделать множественное представление в моем 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)
}

если у кого-то есть идеи, как ее решить, пожалуйста, помогите. Очень ценю это.

...