0
I’m having trouble requisitioning and putting the results in Recycler View. Whenever I make a new request Recycler View has the elements replaced instead of preserving what has already been called and just add more elements in the list.
My requisition code is like this:
class Api (val recyclerView: RecyclerView) : MainActivity(){
private lateinit var adapter: Adapter
private val remote3 = Connection.createService(Repository::class.java)
fun getResponsesFromRepository(page: Int) {
val callPostRepository: Call<Domain.Items> = remote3.list("language:Java", "stars", page)
callPostRepository.enqueue(object : Callback<Domain.Items> {
override fun onFailure(call: Call<Domain.Items>, t: Throwable) {
Toast.makeText(baseContext, t.message, Toast.LENGTH_LONG).show()
println(t.message)
}
override fun onResponse(
call: Call<Domain.Items>,
response: Response<Domain.Items>
) {
if (response.isSuccessful) {
var repo: Domain.Items = response.body()!!
recyclerView.layoutManager = LinearLayoutManager(instance)
adapter = Adapter(repo)
recyclerView.adapter = adapter
}
}
})
}
The Main Activity code looks like this:
open class MainActivity : AppCompatActivity() {
private var count = -1
private lateinit var recycler: RecyclerView
private lateinit var api: Api
private lateinit var loading: View
companion object {
lateinit var instance: MainActivity
private set
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
instance = this
recycler = findViewById(R.id.rec)
api = Api(recycler)
loading = findViewById(R.id.frame_loading)
recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (!recyclerView.canScrollVertically(1) && newState == RecyclerView.SCROLL_STATE_IDLE) {
fetchNewData(api, loading)
}
}
})
fun fetchNewData(api: Api, view: View) {
api.getResponsesFromRepository(count)
count++
}
}
Whenever I go to the end of the page it carries more elements, but it does not add these elements in the view Recycler and rather replace those that have already been loaded, if someone give me a light on how to solve this I thank kkkkkkk