Rahul R
Rahul R

Reputation: 305

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?

ListView Refresh in Android

Refresh Listview in android

Android refresh listview in fragment

How to refresh Android listview?

Upvotes: 0

Views: 1417

Answers (4)

Rahul R
Rahul R

Reputation: 305

I used a gridview instead of my listview and it solved my problem

set 1 item per row can act as listview in my code

<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

Upvotes: 0

Brent
Brent

Reputation: 778

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

    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

you may also just want to look into recyclerview though may make your life easier

Upvotes: 1

Axbor Axrorov
Axbor Axrorov

Reputation: 2806

ListView is officially legacy. 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

Upvotes: 2

gvlachakis
gvlachakis

Reputation: 474

Try smoothScrollToPosition on your listview.

See this, pretty similar if I understand correct what you want.

Upvotes: 1

Related Questions