【问题标题】:Get the new items added after refreshing a Listview using Pull to refresh in Android在Android中使用Pull刷新Listview后获取添加的新项目
【发布时间】:2014-12-10 10:05:11
【问题描述】:

我有一个下拉刷新的 ListView。刷新 ListView 后,它移动到顶部。但我想显示新添加到 ListView 的项目。如何实现它,任何人都可以帮助我。提前致谢。

【问题讨论】:

  • 获取您想要显示的商品编号并以编程方式进行滚动

标签: android android-listview pull-to-refresh


【解决方案1】:

使用这个属性android:stackFromBottom="true",因为它总是在列表视图的底部添加新项目。就像下面的代码,

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

在你的代码中再添加一件事,

[listView].smoothScrollToPosition([adapter].getCount() - 1);

【讨论】:

  • 请再看我的回答。
  • 我正在使用 PulltoRefresh 列表视图。我认为这适用于普通列表视图。
【解决方案2】:

我觉得这个话题除了安藤正桥的回答还有你想要的答案:https://stackoverflow.com/a/3505869/1506369

【讨论】:

  • 对于 PullToRefresh List 视图,没有方法 setSelection(int position)。
  • listView.getRefreshableView().setSelection(int position);工作正常。谢谢。
【解决方案3】:

您可以在适配器中添加reFill 方法,例如:

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

所以,刷新ListView并获取新数据后,调用adapter的这个方法并设置新数据,然后在adapter上调用notifyDataSetChanged()。如果这样做,ListView 不会移到顶部。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多