Arj 1411
Arj 1411

Reputation: 1401

Regarding Pagination in Android

I have implemented on Listview. And What i want is to implement pagination in it . when i reach the end of the listview(20 items per page) it should refresh and new contents should be loaded. I dont know how ... Could anyone please tell me.

private static ListView mList;

in Oncreate i have added

mList = (ListView) view.findViewById(R.id.mypclist);

Once it reaches the first 20 items in the listview , then the list should refresh and add new items... Please help me

Upvotes: 1

Views: 153

Answers (1)

Médéric
Médéric

Reputation: 948

You must create a scroll listener and add it to your listview.

yourListView.setOnScrollListener(this);

In the onScroll implementation

@Override
public void onScroll(AbsListView lw, final int firstVisibleItem,
             final int visibleItemCount, final int totalItemCount) {

    switch(lw.getId()) {
       case android.R.id.list:     
            final int lastItem = firstVisibleItem + visibleItemCount;
            if (lastItem == totalItemCount) {
                if(preLast != lastItem){ //to avoid multiple calls for last item
                    loadNextItems();
                    preLast = lastItem;
                }
            }
        }
    }


private void loadNextItems() {
    // Load your next items
    // Modify your adapter list and call adapter.notifyDataSetChange()
}

Upvotes: 3

Related Questions