Android:保持ListView中最后查看的位置

时间:2016-05-23 09:05:59

标签: android listview pagination listviewitem

我完成了像

这样的顶级分页
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后显示其他一些项目。因此,我希望在更新顶部位置的项目后保持最后查看的位置。请给我任何想法......谢谢

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式获得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 :(得分:0)

  1. Parcelable state = null;全局声明为当前类
  2. onScroll内部(在调用列表视图数据方法之前)存储位置如

    state = listView.onSaveInstanceState();

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

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