简体   繁体   English

如何在Android中刷新列表视图而无需滚动

[英]How to refresh listview in android without scrolling

I am dynamically adding items to my listview my code works fine but my problem is when the listview is updated it is going to the starting position (items are added but scroll view begins from initial position).I am using listview inside fragment.I want to avoid that scrolling to initial position. 我正在将项目动态添加到我的列表视图中,我的代码可以正常工作,但是我的问题是,当更新列表视图时,它将转到起始位置(添加了项目,但滚动视图从初始位置开始)。我在片段中使用了列表视图。以避免滚动到初始位置。

CODE

 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();

Reference : How to refresh Android listview? 参考: 如何刷新Android listview?

ListView Refresh in Android Android中的ListView刷新

Refresh Listview in android 在Android中刷新Listview

Android refresh listview in fragment Android刷新片段视图

How to refresh Android listview? 如何刷新Android ListView?

ListView is officially legacy. ListView是官方遗留的。 Try to use RecyclerView then you will be able to tell that you don't update whole list with methods like notifyItemChanged(position) ... In your case you will call notifyItemRangeInserted(position, count) https://developer.android.com/guide/topics/ui/layout/recyclerview 尝试使用RecyclerView然后您将能够告诉您不要使用notifyItemChanged(position)类的方法更新整个列表。在这种情况下,您将调用notifyItemRangeInserted(position, count) https://developer.android.com /引导/主题/ UI /设计/ recyclerview

Try smoothScrollToPosition on your listview. 在您的列表视图上尝试smoothScrollToPosition

See this , pretty similar if I understand correct what you want. 看到这个 ,如果我理解正确的内容,则非常相似。

So in order to get that scrolling to stop you basically have to block the listview from laying out its children so first off you have to create a custom listview something like 因此,为了使滚动停止,您基本上必须阻止listview布局其子级,因此首先您必须创建一个自定义listview之类的东西

    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();
        }
    }
}

then you can use it like this for example 那么你可以像这样使用它

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);

the setBlockLayoutChildren being true is what will stop your listview from scrolling and of course you can set whatever else you would like it to do setBlockLayoutChildren为true会阻止您的列表视图滚动,当然您可以设置您想要执行的其他任何操作

you may also just want to look into recyclerview though may make your life easier 您可能还想研究recyclerview,尽管这可能会使您的生活更轻松

I used a gridview instead of my listview and it solved my problem 我使用了gridview而不是listview,它解决了我的问题

set 1 item per row can act as listview in my code 每行设置1个项目可以在我的代码中充当listview

<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">

reference : Custom layout as an item for a grid view 参考: 自定义布局作为网格视图的项目

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

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