Search code examples
androidlistviewpaginationlistviewitem

Android: Maintain the last viewed position in ListView


i completed the top pagination like

messagesContainer.setOnScrollListener(new OnScrollListener() {

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

                if (visibleItemCount == totalItemCount){
                    java.lang.System.out.println("too little items to use a ScrollView!");
                } else {
                    if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
                        //Log.e("bottomPosition", "bottomPosition");

                    }else if (firstVisibleItem == 0) {
                        Log.e("topPosition", "topPosition");



                            index = messagesContainer.getLastVisiblePosition();
                            View v = messagesContainer.getChildAt(messagesContainer.getHeaderViewsCount());
                            top = (v == null) ? 0 : v.getTop();



                        if(rechedTopPosition != null){

                            rechedTopPosition = null;

                                Log.e("pageNoForVolUrl", pageNoForVolUrl+"");

                                getPrevChatVolley();

                        }   


                    }
                }

            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

                if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
                    //Log.e("a", "scrolling stopped..."); 
                }


            }
        });

for(int i = 0; i < rowsArray.length() ; i++){

    JSONObject singleObj = rowsArray.getJSONObject(i);
    JSONObject valueObj = singleObj.getJSONObject("value");

    String commentId = valueObj.getString("_id");
    String textMessage = valueObj.getString("body");





    ChatMessageItems chatMessage = new ChatMessageItems();
    chatMessage.setId(122);//dummy
    chatMessage.setMessageId(commentId);
    chatMessage.setMessage(textMessage);

    if (fromUserId.equals("384")) {
        chatMessage.setMe(false); // False = right side, True = left side
    } else {
        chatMessage.setMe(true);
    }

    itemsAdapter.insert(chatMessage, 0);

}

    rechedTopPosition = "fulFilled";

    messagesContainer.setSelectionFromTop(index, top);

<ListView
        android:id="@+id/messagesContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:stackFromBottom="true"
        android:listSelector="@android:color/transparent" />    

By using this code i'm able to add the list view items at the top but it's not maintaining the last viewed position

For ex:

My initial listView is like

----
15
16
17
18
19
20

When i scroll to 15 (top) position it is loading all items at top but it's not maintaining the last viewed 15 item position. It showing the some other item after updating the listView. So, i want to maintain the last viewed position after updating the items at top position. Please give me any idea...... Thank you


Solution

    1. Declare Parcelable state = null;as globally to the present class
    2. Inside of onScroll (Before calling the list view data method) store the position like

      state = listView.onSaveInstanceState();

    3. After appending the list view data into adapter maintain the position like

      if(state != null){ listView.onRestoreInstanceState(state); }