【问题标题】:How to refresh listview in android without scrolling如何在不滚动的情况下刷新android中的listview
【发布时间】:2019-06-18 13:42:35
【问题描述】:

我正在动态地将项目添加到我的列表视图中,我的代码工作正常,但我的问题是当列表视图更新时它会到达起始位置(添加项目但滚动视图从初始位置开始)。我在片段中使用列表视图.我想避免滚动到初始位置。

代码

 ListAdapter adapter =
                            new SimpleAdapter(getContext(), productsList, R.layout.list_notify, new String[]{"id","title","des"},
                                    new int[]{R.id.id, R.id.title,R.id.des});

lv.setAdapter(adapter);
lv.invalidateViews();

参考:How to refresh Android listview?

ListView Refresh in Android

Refresh Listview in android

Android refresh listview in fragment

How to refresh Android listview?

【问题讨论】:

    标签: android


    【解决方案1】:

    ListView 是正式的遗产。尝试使用RecyclerView,然后您将能够告诉您没有使用notifyItemChanged(position)之类的方法更新整个列表... 在你的情况下,你会打电话给notifyItemRangeInserted(position, count) https://developer.android.com/guide/topics/ui/layout/recyclerview

    【讨论】:

      【解决方案2】:

      在您的列表视图中尝试smoothScrollToPosition

      请参阅this,如果我理解正确您想要的,则非常相似。

      【讨论】:

        【解决方案3】:

        因此,为了让滚动停止,您基本上必须阻止列表视图布局其子视图,因此首先您必须创建一个自定义列表视图,例如

            public class BlockingListView extends ListView {
        
            private boolean mBlockLayoutChildren;
        
            public BlockingListView(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
        
            public void setBlockLayoutChildren(boolean block) {
                mBlockLayoutChildren = block;
            }
        
            @Override
            protected void layoutChildren() {
                if (!mBlockLayoutChildren) {
                    super.layoutChildren();
                }
            }
        }
        

        那么你可以像这样使用它,例如

        int firstVisPos = mListView.getFirstVisiblePosition();
        View firstVisView = mListView.getChildAt(0);
        int top = firstVisView != null ? firstVisView.getTop() : 0;
        
        // Block children layout for now 
        mListView.setBlockLayoutChildren(true);
        
        // Number of items added before the first visible item 
        int itemsAddedBeforeFirstVisible = ...;
        
        // Change the cursor, or call notifyDataSetChanged() if not using a Cursor
        mAdapter.swapCursor(...);
        
        // Let ListView start laying out children again 
        mListView.setBlockLayoutChildren(false);
        
        // Call setSelectionFromTop to change the ListView position
        mListView.setSelectionFromTop(firstVisPos + itemsAddedBeforeFirstVisible, top);
        

        setBlockLayoutChildren 为 true 会阻止您的列表视图滚动,当然您可以设置任何您希望它执行的操作

        你可能也只是想看看 recyclerview 虽然可能会让你的生活更轻松

        【讨论】:

          【解决方案4】:

          我使用了一个gridview而不是我的listview,它解决了我的问题

          每行设置 1 项可以在我的代码中充当列表视图

          <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/grid"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              **android:numColumns="1"**
              android:verticalSpacing="10dp"
              android:horizontalSpacing="10dp"
              android:scrollbarStyle="outsideOverlay"
              android:verticalScrollbarPosition="right"
              android:scrollbars="vertical">
          

          参考:Custom layout as an item for a grid view

          【讨论】:

            猜你喜欢
            • 2020-01-02
            • 1970-01-01
            • 2020-04-09
            • 2018-12-16
            • 1970-01-01
            • 1970-01-01
            • 2021-12-22
            • 2021-02-05
            • 2019-11-25
            相关资源
            最近更新 更多