Load More Items On Scroll Android (Endless Scrolling)

Load More Items On Scroll Android (Endless Scrolling)

We pretty much all know the nice technique apps like Gmail, Facebook, etc. use to dynamically load more items to the ListView then you scroll to the bottom. In this article i am going to show you the same thing over ListView and RecyclerView.

If you are unaware of ListView and RecyclerView that how to use both views then you can check my tutorials for ListView and RecyclerView.

Example

In this tutorial, we are going to learn how to implement endless scroll listener by loading more items when user reaches end of the view i.e. bottom view.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. Open res ⇒ values ⇒ strings.xml and add below string values. This are the strings that i am going to use in this tutorial and there are String Array also that i’ll use for loading data over views.

3. Open res ⇒ values and create new resource file naming colors.xml for your colors and put these colors inside this xml.

4. Now create a layout naming activity_main.xml that contains a ViewPager. Since I am using ListView and RecyclerView so i will show both views in same activity using view pager tab.

If you don’t know how to make swipeable tabs then you can check my tutorial about Tabs.

5. Now after creating viewpager we have to create two more designs for our two fragments i.e ListView and RecyclerView. So create two more xml files under layout directory.

listview_fragment.xml

linearlayout_fragment.xml

In above both layouts we have seen there is a include tag. This include tag including a progress_layout below ListView and RecyclerView. The progress_layout is a layout that will display then user reaches at the end of the view while scrolling.

6. In above point we had included progress_layout so now we have to create progress_layout.xml file under layout directory. This layout contains a progress bar a textview for display message title.

7. We are using a custom ListView and RecyclerView. So for this we have to create a getter and setter class and custom xml files and custom adapter. If you don’t know about custom views then you can check this tutorial.

I am not going in details for these custom part.

  • Create a custom view xml file naming list_customview.xml for custom designing and we can use same design for both ListView and RecyclerView.
  • Now create a getter and setter class naming Data_Model.java.
  •  After getter and setter class create custom adapter for ListView.
  • Now for RecyclerView we have to create a separate ViewHolder class and separate Adapter class.

ListView_Holder.java

  • Create a Interface for implementing click listener over recyclerview items.

RecyclerView_OnClickListener.java

  • Finally create a custom adapter for RecyclerView.

ListView_Recycler_Adapter.java

8. Come to your MainActivity.java and add the following code for implementing swipeable tabs over viewpager.

9. Finally we are going to implement scroll listener over both listview and recyclerview. So lets see how to implement this :

  • ListView : We use setOnScrollListener method for ListView and use certain variables to read the end of ListView scroll and update the ListView when end is reached.
  • RecyclerView : For RecyclerView we will use addOnScrollListener and certain  variables to detect end of recyclerview.
We will use this both methods in their respected classes.

10. Since i am using static items to be load on linear list that i had declared in strings.xml inside string array. For that i had created a random value generator method that will generate a random value between 0-9 and update the items accordingly. So create a new class RandomNumberGenerator.java and add the following code.

11. Finally create two more java files for ListView and RecyclerView fragments naming ListViewLayout_Fragment.java and LinearLayout_Fragment.java respectively. In both these classes we first load data manually and when user scrolls and reached at end we will load new data and update items.

ListViewLayout_Fragment .java

LinearLayout_Fragment .java

12. Now, you are all done, run your app and you will get the output as shown in video.

Note: For gridview you have to just replace refrence variable of listview with that of gridview because same listener is used for both ListView and GridView.

Thanks. :)

Download Source Code – >

COMMENTS (3)
Reply

Hi, finally found what i wanted thanks mate you saved the day.

PS: on thing is you site did’t come on search result just a advice.

Add RecyclerView or Listview in url so that if anybody search it will come in search result and take the domain name around android if you are going to build around android tutorials.

Thanks

Reply

how to import downloaded source code into android studio

    Hi Pradeep,

    Follow the below steps:

    1. Open your Android Studio.
    2. Go to File and Select Open
    3. Now Go to project location and select build.gradle file and open it. Don’t open direct Project always open build.gradle file.

    Thanks

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)