1

I have a ListView with Pull to Refresh. After refreshing the ListView, it moves to the top. But I want to show the item added newly to the ListView. How to implement it, Can anyone help me. Thanks in advance.

Mycoola
  • 1,135
  • 1
  • 8
  • 29
Nijan
  • 93
  • 2
  • 6

3 Answers3

2

Use this attribute android:stackFromBottom="true", because it always add new item at bottom of list view. Like following code,

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp"
    android:divider="@null"
    android:stackFromBottom="true" >
</ListView>

And add one more thing add in your code,

[listView].smoothScrollToPosition([adapter].getCount() - 1);
Umang Kothari
  • 3,674
  • 27
  • 36
1

You can add reFill method to your adapter, for example:

public void reFill(List<MyData> newData){
    this.myData.clear();
    this.myData.addAll(newData);
}

So, after refreshing the ListView and getting new data, you call this method of adapter and set new data, and call notifyDataSetChanged() on adapter. If you doing this, ListView doesn't move to the top.

1

I think this topic has the answer you want in addition to Ando Masahashi's answer : https://stackoverflow.com/a/3505869/1506369

Community
  • 1
  • 1
Antoine
  • 583
  • 2
  • 6
  • 21