11

I am desperately trying to implement endless scrolling on an android app using kotlin. All the tutorials are either useless since they dont explain things properly. So for example: https://github.com/chetdeva/recyclerview-bindings

it looks promising but the author uses phrases like "put this in your BindingAdapter" so i look what this BindingAdapter is, I found a java file but if you insert anything in there I get errors. Its like anything I try fails directly.

The other tutorials are written in java and even with "translate to kotlin" option its useless since the translated code throws 100 errors.

I tried things like :

setContentView(R.layout.activity_main)
    list.layoutManager = LinearLayoutManager(this)
    list.hasFixedSize()
    list.adapter = ListAdapter(this, getLists())
    val list_view: RecyclerView = findViewById(R.id.list)
    fun setRecyclerViewScrollListener() {
        list_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                val height = list_view.getHeight()

                val diff = height-dy
                if (diff < 1000){
                    /*load next list */
                }
            }
        })
    }
    setRecyclerViewScrollListener()
}

or this

val inflater = LayoutInflater.from(this@MainActivity)
val layout = inflater.inflate(R.layout.append_list, null, false)
button.setOnClickListener{screen.addView(layout)}

Is there a bullet proof method where you can simply append elemets like with html and js? I wrote this snippet in 2 min. Is there a similar "easy" way in Android/Kotlin?

$("#next").click(function(){
  $(".append_text").append("new text <img src='http://static.webshopapp.com/shops/015426/files/005031634/560x625x2/kek-amsterdam-wandtattoo-hase-forest-friends-braun.jpg'/>")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="next">Load</button>

<span class="append_text"> </span>

In general I recive a lot of errors for choosing the wrong Layout. I tried Listview and contrainlayout and recycling Layout and Vertical Scrolling layout and so on. Is there a simple body tag where you can simply append a xml file?

I think I go the wrong way the whole time because I see everything though the eyes of a Web. Dev. while android does not have the classical DOM. Can anybody explain it to me with an minimal example on how to append a xml file to the main xml file on button click/on scroll?

0

3 Answers 3

16

I use this method for adding endless scroll functionality to a recyclerview in Kotlin:

private fun setRecyclerViewScrollListener() {
    scrollListener = object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
            val totalItemCount = recyclerView!!.layoutManager.itemCount
            if (totalItemCount == lastVisibleItemPosition + 1) {
                Log.d("MyTAG", "Load new list")
                recycler.removeOnScrollListener(scrollListener)
            }
        }
    }
    recycler.addOnScrollListener(scrollListener)
}

the variable lastVisibleItemPosition is declared as follows:

private val lastVisibleItemPosition: Int get() = linearLayoutManager.findLastVisibleItemPosition()

private lateinit var scrollListener: RecyclerView.OnScrollListener

Just call the setRecyclerViewScrollListener() method every time you nedd to add this functionality to the recyclerView.

Hope it helps,

Leonardo

Sign up to request clarification or add additional context in comments.

4 Comments

where do you define these methods? If I add this into my MainAct. into the onCreate function, get, linearLayoutManager, LOG_TAG, chatRecycler, recycler and scrollListener are either undefined or cant be resolved. Where are the xml files?
@hansTheFranz I edited my code, sorry for the misprint, chatRecycler and recyler are the same RecyclerView, LOG_TAG is a string for logging purpose, I also added the declaration of scrollListener. linearLayoutManager is the layoutmanager you have to set to the recyclerview and get() is the getter for lastVisibleItemPosition variable.
Thanks for this piece of code, @LeonardoMedori. Can I ask you why you have this next line in your code? recycler.removeOnScrollListener(scrollListener)
@Rod I remove the scrollListener and I add it again after I finished updating the list. If I don't remove the listener the onScrollStateChanged method is called many times and (totalItemCount == lastVisibleItemPosition + 1) condition can be true more than once. So instead of having a boolean for checking if the list is updated I remove the listener, make a call, update the list and than add the listener again.
2

I don't know if this solves your problem but I use this to implement a RecyclerView that added new data to my RecyclerView when the scroll comes to the end:

productsListActivityBinding.recyclerViewProducts.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
            if (!recyclerView.canScrollVertically(1)){
                //function that add new elements to my recycler view
            }
        }

    })

Comments

0

set in recycler view in scroll listener

 recycler.addOnScrollListener(your listener)

1 Comment

See "Explaining entirely code-based answers". While this might be technically correct it doesn't explain why it solves the problem or should be the selected answer. We should educate in addition to help solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.