0

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();
                           }
                    }
                }
            });`
Rustam
  • 6,307
  • 1
  • 21
  • 25
Hitesh Matnani
  • 513
  • 2
  • 7
  • 23
  • possible duplicate of [Retaining position in ListView after calling notifyDataSetChanged](http://stackoverflow.com/questions/8276128/retaining-position-in-listview-after-calling-notifydatasetchanged) – Panther Sep 26 '14 at 18:06
  • Read here Try this.. http://stackoverflow.com/questions/8276128/retaining-position-in-listview-after-calling-notifydatasetchanged – Panther Sep 26 '14 at 18:06

1 Answers1

0

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

arlistan
  • 701
  • 8
  • 20
  • Can you please tell how to do that with an sample example – Hitesh Matnani Sep 26 '14 at 17:58
  • Post in your question how you are currently fetching your new items and how you are setting them in the list, and I'll help you. – arlistan Sep 26 '14 at 18:02
  • Here is my complete code! http://stackoverflow.com/questions/26067673/how-to-implement-a-proper-scroll-in-list-view-to-implement-pagination-on-fragmen – Hitesh Matnani Sep 26 '14 at 20:18
  • Ok, answered there, but you should delete this question, there is no need to have to questions asking the same thing. – arlistan Sep 26 '14 at 20:23