0

Ive been looking all over the internet and cant find the answer that works for me.

I am trying to create a fragment with an infinite scroller (just like on social network apps like twitter, facebook, instagram, tumblr etc ) and research suggests that a recycler view is the best way. This image show you how i have done it. File -> new -> Fragment -> Fragment (List) enter image description here

If I'm correct, the next step now is to create an on scroll listener so that i know when the user has scrolled to the bottom as that would be when i load more data.

If i have understood things so far, i think its inside the file MyContentRecyclerViewAdapter here enter image description here that i have to figure out how where to put my next bit of code for the scrollListener

` list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)

            if (dy > 0) {

                Toast.makeText(list.context,"Scrolling up", Toast.LENGTH_SHORT).show()
                // Scrolling up
            } else {
                Toast.makeText(list.context,"Scrolling down", Toast.LENGTH_SHORT).show()
                // Scrolling down
            }
        }

    })`

Replying to Lalit Fauazdar. This is what I've got right now however for some reason this causes my app to crash. list appears to have a null value for some reason even though my androidx.recyclerview.widget.RecyclerView has android:id="@+id/list".

enter image description here

Jevon
  • 295
  • 2
  • 13
  • The `onScrollListener()` will come inside `onCreateView()` of your Fragment. You can't place it in your adapter class. Also, [this](https://android.jlelse.eu/keddit-part-7-infinite-scroll-higher-order-functions-lambdas-3a11fbd5090e) is a decent article which can help, I was reading it just today and I feel it's worth reading, – Lalit Fauzdar Jun 29 '20 at 20:09
  • Could you show me the code? I don't think i can use `list` inside of the `onCreateView`. When i've been trying this it would appear as red. This leads me to believe that inside of the `onCreateView()` i cant access the `androidx.recyclerview.widget.RecyclerView` via its `id` – Jevon Jun 29 '20 at 20:44
  • You don't and can't access the views directly by their IDs even in Kotlin in Fragments. You've to use the rootView for that. See your first line of `onCreateView` where you assign the layout which is generally as `val root = inflater.inflate()` so what you have to do is your have to use `root.yourRecyclerView.addScrollListener()`. – Lalit Fauzdar Jun 29 '20 at 20:48
  • Share your `onCreateView()` and I'll show you. – Lalit Fauzdar Jun 29 '20 at 20:50
  • Ive reedited to show my code at the bottom of the answer by the way. Im making this comment because i think thats what makes the website notify you. – Jevon Jun 29 '20 at 21:20
  • Read my first comment, I told you to put the `onScrollListener()` inside `onCreateView()` of your fragment and not `onCreateViewHolder()` of your adapter class. Put it inside `onCreateView()` of your `ContentFragment`. And posting pictures of code is not encouraged on StackOverFlow, remember this. – Lalit Fauzdar Jun 29 '20 at 21:42
  • Thanks for helping me by the way, if you post an answer i would accept it. Why isn't posting pictures encouraged on stack overflow? – Jevon Jul 09 '20 at 03:48
  • Here, read [Why not upload pictures of code?](https://meta.stackoverflow.com/a/285557/8244632). Tl;dr - We can't copy it to test it or help you for the answer. – Lalit Fauzdar Jul 09 '20 at 06:06
  • I have another question on how to implement infinite scroll here https://stackoverflow.com/questions/62846910/how-to-implement-endless-list-with-a-recyclerview-inside-a-fragment if you don't mind helping me on this question too. – Jevon Jul 11 '20 at 08:29

1 Answers1

0

For infinite scrolling I would recommend you to use this library provided by Google.

Filip Sollár
  • 91
  • 1
  • 5