Android:维护ListView中最后查看的位置

Android: Maintain the last viewed position in ListView

我完成了像

这样的顶部分页
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" />    

通过使用此代码,我可以在顶部添加列表视图项目,但它不会保留上次查看的位置

例如:

我的初始 listView 就像

----
15
16
17
18
19
20

当我滚动到 15(顶部)位置时,它会在顶部加载所有项目,但不会保持最后查看的 15 项目位置。它在更新 listView 后显示其他一些项目。所以,我想在更新顶部位置的项目后保持最后查看的位置。请给我任何想法......谢谢

您可以通过以下方式获取列表视图的可见位置:

int position;

YOURLISTVIEW.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }

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

        }
    });

然后您可以通过以下方式设置列表视图的位置:

YOURLISTVIEW.setSelection(position);
  1. 宣布Parcelable state = null;为全球至今class
  2. Inside of onScroll(调用列表视图数据方法前)存储位置如

    state = listView.onSaveInstanceState();

  3. 将列表视图数据附加到适配器后,保持

    的位置

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