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.
Asked
Active
Viewed 622 times
1
-
get your item number u want to show and do scroll programmatically – Ando Masahashi Dec 10 '14 at 10:06
3 Answers
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
-
-
I'm using PulltoRefresh Listview. I think this works for normal list view. – Nijan Dec 10 '14 at 11:45
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.

Suvitruf - Andrei Apanasik
- 5,582
- 9
- 49
- 88
1
I think this topic has the answer you want in addition to Ando Masahashi's answer : https://stackoverflow.com/a/3505869/1506369
-
For PullToRefresh List view, there is no method setSelection(int position). – Nijan Dec 10 '14 at 11:20
-
listView.getRefreshableView().setSelection(int position); works fine. Thank you. – Nijan Dec 10 '14 at 11:55