简体   繁体   中英

How can I load more items when scrolling down in listview?

First I want to say: This question is no duplicate!

I already read a lot of questions about loading more items when scrolling down .

This was most helpful for me.

But all questions I read aren't explaining the basic principle.

So, what I mean is:

My app gets data from json and displays it in a ListView , but it's not possible to load all items from database with one request . The app crashes…

The solution is to load only 10 items and on scrolling down 10 items again.

But what is the basic principle to do this?

I thought about these 2 different options:

  1. Set a LIMIT in my PHP file and send for each 10 items a new request from android and set LIMIT +10.
  2. Send one request from android and getting all data from json, but only displaying 10 items.

Code isn't necessary, because I want to know the principle of doing this.

Usually you would load 10 new items from the server each time. Use a page-parameter to identify which 10 items you need and where to place them.

loading all items at once could be way too expensive: The delay could be long and the user's data-plan won't be happy either. Obviously depending on how many items there are and what contents they have.

You will have to find the trade-off. Based on your data size, sometimes it makes sense to parse and save it all in local database.

Just for 200-300 records, you don't want to make another api call after every 50 records in list. Remember with mobile app user scrolls up and down very often. You might be unnecessarily sending multiple requests to your server, which might be an overload(depending on user count).

If you go with option 2, you can make use of something like JobIntentService to silently fetch data and save locally.

This approach will also let your user interact with no internet(offline mode) scenarios.

The approach I use in my apps is:

Step # 1: I load first set of data with limit from server and then store the last record data id in a variable.

Step # 2: Implement the on scroll end listener to your listView or RecyclerView.

Step # 3: Start another request inside on scroll end listener and load new records.(Here I set again data id in a variable)

For Example:

Start the request when the activity starts and do what I explained earlier.

GetBusinesses("&isOpen=2&cat="+Prefrences.getSelectedCategoryId());

Then inside your on Scroll End Listener

GetBusinesses("&isOpen=2&cat="+Prefrences.getSelectedCategoryId()+"&limit=10&lastDataId="+BusinessItems[index].mBusinessId)

Edit To avoid duplicate api call inside GetBusinesses() check if the request was started previously, well the idea is create a boolean initially false and then in your GetBusinesses() function make it true before starting the request and once the data is loaded and request is finish make it false again. I hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM