简体   繁体   中英

How to implement scroll in list view to implement pagination?

I am building an application like techcrunch. I am fetching data from server in JSON format and displaying the data in list view like article title,author name and image. I have applied pagination means when user scroll more articles load in a list view. My pagination works fine but there is an issue in the scroll function as the fresh or new data loads the scroll dose not aligns with the data. To clarify more in simple words my scroll-er goes at the top of the page when i am actually scrolling down this is my code :

 listView.setOnScrollListener(new AbsListView.OnScrollListener()
            {
                @Override
                public void onScrollStateChanged(AbsListView absListView, int i)
                {

                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
                {
                    int lastItem = firstVisibleItem + visibleItemCount;
                       if(lastItem == totalItemCount){
                           if (mPreLast != lastItem)
                           {
                               mPreLast = lastItem;
                               onStart();
                           }
                    }
                }
            });`

You can use:

listView.setSelectionFromTop(newPosition, 0);

With this method you can set the position to a ListView, using the first parameter as the new position on the list, and the second parameter can be used to set the distance from the top

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