简体   繁体   English

更新时的ListView滚动

[英]ListView Scrolling when Update

In a chat app the listview have 2 ways of update once when a new message pops and the other one load more button is Clicked 在聊天应用程序中,当弹出一条新消息并且单击另一个“加载更多”按钮时,列表视图有两种更新方式

so i need 2 ways of update the first one is to scroll to the new message 所以我需要2种更新方法第一种是滚动到新消息

Second is to stay at the position that clicked from 二是停留在点击的位置

the list view have these attributes 列表视图具有这些属性

  <ListView

            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:divider="@null"
            android:fadingEdge="none"
            android:fastScrollEnabled="false"
            android:footerDividersEnabled="false"
            android:headerDividersEnabled="false"
            android:listSelector="@android:color/transparent"
            android:smoothScrollbar="true"
            android:stackFromBottom="true"
            android:transcriptMode="normal" >
        </ListView>

the first condition is working fine so i need to keep the position when clicking on LoadMore 第一个条件是工作正常,所以我需要在单击LoadMore时保持位置不变

    public void updateWithOutScroll() {

    final int position = chListView.getSelectedItemPosition();
    Parcelable state = chListView.onSaveInstanceState();

    ChatAdapter.notifyDataSetChanged();

    chListView.onRestoreInstanceState(state);
    chListView.post(new Runnable() {
        @Override
        public void run() {

            chListView.setSelection(position);
        }
    });
}

this is Making the list to scroll to bottom first then to top 这是使列表先滚动到底部然后再滚动到顶部

What is the wrong in this? 这有什么问题? and how to achieve my Goal? 以及如何实现我的目标?

I'm not sure I understand you're question. 我不确定我是否理解您的问题。 If you're asking how to add more to a listview, just call: 如果您要询问如何向列表视图添加更多内容,只需致电:

Adapter.add(...) or Adapter.addAll(...)

and then: 接着:

Adapter.notifyDataSetChanged();

The listview should then just add the new elements to the bottom. 然后,listview应该仅将新元素添加到底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM