0

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?

Rahul R
  • 303
  • 2
  • 11

4 Answers4

2

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

Axbor Axrorov
  • 2,720
  • 2
  • 17
  • 35
1

Try smoothScrollToPosition on your listview.

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

gvlachakis
  • 474
  • 7
  • 14
1

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

Brent
  • 740
  • 1
  • 5
  • 11
0

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

Rahul R
  • 303
  • 2
  • 11