0

The RecyclerView have to load n number of items initially, when the user scroll to the bottom of it I want a button or any other View on the bottom so that the user can click on it and a method to add n more items is triggered.

I'm new to android app development and I really need help, Thanks.

Ashique
  • 25
  • 2
  • 5
  • 3
    Welcome to SO! You should post what you searched, what you tried and which are your problems, check Android guide for `RecyclerView`: https://developer.android.com/guide/topics/ui/layout/recyclerview; check here to see how to ask a good question: [ask] – Luca Murra Jan 29 '20 at 17:00
  • Does this answer your question? [android recyclerview load more button](https://stackoverflow.com/questions/30313209/android-recyclerview-load-more-button) – Ryan M Jan 29 '20 at 20:34

2 Answers2

0

Android recyclerview is used to display large amount of items on screen, but with improved performance and other benefits. RecyclerView in Android uses an adapter to represent the information we want to show as list. The data to be displayed is sent to adapter, which handles the view.


Coming to your custom approach of adding a button. You can control the amount of data being sent to adapter at beginning. Considering you have a server that will send data when requested with a tweak to send data in set of row. Add a floating button which is displayed when user reaches end of recyclerview, this button requests further data from server and send a signal to adapter that your variable containing the data has been updated. So, this will add data to your view and you can scroll further until that data ends.
  • So you require to design your server for custom data range request.
  • Variable to keep track of current data range present.
  • Code to check end of RecyclerView data feed.
  • function to request more data on button click and signal adapter for update.

You can search endless scrolling using RecyclerView to get tutorial related to this.

skWyz
  • 154
  • 1
  • 10
0

what you are asking is basically an endless scrolling recyclerview with a loaderview at the bottom that accepts click to load additional data.

You have to make use of multiple viewType in the RecyclerView.Adapter

I encourage you to search more before asking a new question as there are countless tutorials for this and similar questions on stackOverflow. Here's a link to an example on stack on how to implement multiple viewTypes.

I'll share some points on one of the ways to achieve what you are looking for.

First in your adapter you need to declare two constants

private final int VIEW_ITEM = 0;  
private final int VIEW_LOADER = 1;

and a boolean showLoadMore

create a public method

public void showLoader(boolean status) {
    this.showLoadMore = status;
}

this can be used to show/hide loader from your activity

You need to override getItemCount() method to return the correct number of rows as it keeps changing base on your loader

@Override
public int getItemCount() {
    if (yourList.isEmpty()) {
        return 0;
    } else {
        if (showLoadMore)
            return yourList.size() + 1;
        else
            return yourList.size();
    }
}

Override getItemViewType(int position), since your loader needs to be at the bottom , it should

return (position == yourList.size() && showLoadMore) ? VIEW_LOAD : VIEW_ITEM;

Next as shown in the link i shared above, you need to inflate your layout in onCreateViewHoldermethod and then bind your layout in onBindViewHolder method based on your viewTypes. you can simply use if else condition as this case only has 2 viewtypes.

Now in your activity after you setAdapter on the recyclerview and have fetched the n data set adapter.showLoader(true) to show the loader view and notifyDataSetChanged(). You can customize the loaderview to show progress bar or however you like , on click fetch the next set of data, if new count is less than required count count <= n then make set adapter's showLoader(false) and adapter.notifyDataSetChanged() this will hide your loader view after adding the data.

Hope this answer helps you in some way,

Meanwhile here's a tutorial from web implementing endless scrolling recyclerview in another way

ljk
  • 1,496
  • 1
  • 11
  • 14