A simple way to load more items in Robospice listview

in ipragmatech •  6 years ago 


ListView is very useful and powerful component to display the list of items in android mobile application. We were using Robospice framework for one of our client's mobile app and during the android custom development of the it, we found that there is no way to dynamically load items in the robospice listview (robospicelist). It allows to load all data at a same time means static loading of the elements which shall hit the performance of the app if the list is very long. So overcome this issue, we studied and implemented a way to dynamically add more items in the list.

In this article, we shall demonstrate how we have implemented this and others can take a help from it. Basically we listen to the scrolling event of the list and add a network call to get the next set of data.

Steps for dynamically load more items in robospice listview


1.Create a class in which pagination attributes has to be defined(for eg..see the code below)

public class PaginationAttr {
private Integer offset;
private Integer limit;
private String pageId = "1";

public Integer getOffset() {
return offset;
}

public void setOffset(Integer offset) {
this.offset = offset;
}

public Integer getLimit() {
return limit;
}

public void setLimit(Integer limit) {
this.limit = limit;
}

public String getPageId() {
return pageId;
}

public void setPageId(String pageId) {
this.pageId = pageId;
}

}

2.Create your own arrayAdapter class extending OkHttpSpiceArrayAdapter class of spicelist and include two abstract methods - loadMore(int totalCount), isLoadMoreCompeted()(for e.g)
public abstract class ExampleArrayAdapter<T> extends OkHttpSpiceArrayAdapter<T> {

public ExampleArrayAdapter(Context context,
OkHttpBitmapSpiceManager spiceManagerBinary) {
super(context, spiceManagerBinary);

}

public ExampleArrayAdapter(Context context,
OkHttpBitmapSpiceManager spiceManagerBinary, List<T> objects) {
super(context, spiceManagerBinary, objects);
}

public abstract void loadMore(int totalCount);
public abstract boolean isLoadMoreCompeted();

}

3.Implement your own list adapter class extending your arrayAdapter class and implements its two methods
public void loadMore(int count) {
         if (!isLoadMoreCompeted() && activity instanceof MainActivity) {
((MainActivity) activity).loadMore(count);
}

}

public void setComplete() {
this.isLoadMoreCompleted = true;

}

@Override
public boolean isLoadMoreCompeted() {
return this.isLoadMoreCompleted;
}

4.Set the pagination attributes in your request.For eg.
@Override
    public Response loadDataFromNetwork() throws Exception {
        String url = "";
    Map&lt;String, Object&gt; queryParams = request.initializeParams();

    queryParams.put("page_id", this.request.getPaginationAttr().getPageId());
    queryParams.put("item_per_page", this.request.getPaginationAttr().getLimit());
    queryParams.put("offset", this.request.getPaginationAttr().getOffset());
    
   //Build your reqest url here including these attributes //
   


    return getRestTemplate().getForObject(queryUrl, Response.class);
}
5.In your Main Activity write a method and include the following code:
private void loadList() {
PaginationAttr paginationAttr = new PaginationAttr();
paginationAttr.setOffset(0);              //starting element of the list
paginationAttr.setLimit(ITEMS_PER_PAGE);    //no. of items to be listed per page
  //write your other stuff here for request

}
6.Write another method which will be called as we scroll down .

loadMore(int count) {
PaginationAttr paginationAttr = new PaginationAttr();
paginationAttr.setOffset(count);        //no.of item from where listing has to be done next
paginationAttr.setLimit(ITEMS_PER_PAGE);
paginationAttr.setPageId(String.valueOf(Math.ceil(count/ ITEMS_PER_PAGE) + 1));   //id for the new page to be displayed

// write your other stuffs here for request

}

7.Create your listView class extending ListView and Override the onScroll() including the following code:
 @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            final int lastItem = firstVisibleItem + visibleItemCount;
            if (lastItem == totalItemCount) {
                if (previousLast != lastItem) { // to avoid multiple calls for last item
                    previousLast = lastItem;
                    if (!getAdapter().isLoadMoreCompeted()) {
                        getAdapter().loadMore(totalItemCount);
                    }
                }
        }
    }</pre>

8.Update your listview content by adding following code:

  if (listAdapter == null) {
            //create your adapter here and set to the listview
            listAdapter = new ListAdapter(//put your parameters here);
            listView.setAdapter(listAdapter);
            listAdapter.notifyDataSetChanged();
            listView.setSelection(listAdapter.getCount() - 1);
        } else {
            listAdapter.addAll(items);
            listView.setAdapter(listAdapter);
            listAdapter.notifyDataSetChanged();
        }
  if (Respose.getTotal_page_count() <= Respose.getPage_id()) {
                listAdapter.setComplete();
         }

Conclusion

Implementation of loadmore function for listview in our application was a very good experience.It is easy to implement and understand.

References

Robospice

ListView


Posted from my blog with : https://www.ipragmatech.com/loadmore-android-robospice-listview/

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

That’s coming along nicely!

You got a 7.81% upvote from @emperorofnaps courtesy of @shannanpasha!

Want to promote your posts too? Send 0.05+ SBD or STEEM to @emperorofnaps to receive a share of a full upvote every 2.4 hours...Then go relax and take a nap!