在Twitter(官方应用)等Android应用中,当你遇到一个ListView时,你可以下拉它(它会在发布时反弹)来刷新内容。

我想知道,在你看来,最好的实现方式是什么?

我能想到的一些可能性:

一个在ListView顶部的项目-然而我不认为滚动回项目位置1(基于0)在ListView上的动画是一个简单的任务。 ListView之外的另一个视图-但我需要注意在它被拉时将ListView位置向下移动,我不确定我们是否可以检测到对ListView的拖动触摸是否仍然真正滚动ListView上的项。

你有什么推荐吗?

另外,我想知道官方的Twitter应用程序源代码什么时候发布。有消息说它会发布,但6个月过去了,我们一直没有听到它的消息。


当前回答

最后,谷歌发布了一个官方版本的拉取刷新库!

它被称为swiperfreshlayout,在支持库中,文档在这里:

Add SwipeRefreshLayout as a parent of view which will be treated as a pull to refresh the layout. (I took ListView as an example, it can be any View like LinearLayout, ScrollView etc.) <androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pullToRefresh" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> Add a listener to your class protected void onCreate(Bundle savedInstanceState) { final SwipeRefreshLayout pullToRefresh = findViewById(R.id.pullToRefresh); pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refreshData(); // your code pullToRefresh.setRefreshing(false); } }); }

你也可以调用pullToRefresh.setRefreshing(true/false);按您的要求。

更新

Android支持库已弃用,并已被AndroidX所取代。新库的链接可以在这里找到。

此外,你需要添加以下依赖到你的项目:

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

OR

你可以去Refactor>>Migrate to AndroidX, Android Studio会为你处理依赖项。

其他回答

我还为Android实现了一个健壮的、开源的、易于使用的和高度可定制的PullToRefresh库。你可以用PullToRefreshListView替换你的ListView,就像在项目页面的文档中描述的那样。

https://github.com/erikwt/PullToRefresh-ListView

我在这里写了一个拉取刷新组件:https://github.com/guillep/PullToRefresh 它的工作事件,如果列表没有项目,我已经在>=1.6安卓手机上测试了它。

欢迎任何建议或改进:)

如果你不想让你的程序看起来像一个强行安装在Android上的iPhone程序,目标是一个更原生的外观和感觉,并做一些类似于Gingerbread的事情:

在这个链接中,你可以找到著名的PullToRefresh视图的一个分支,它有新的有趣的实现,如PullTorRefreshWebView或PullToRefreshGridView,或者可以在列表的底部边缘添加一个PullToRefresh。

https://github.com/chrisbanes/Android-PullToRefresh

最好的是,它在Android 4.1中工作完美(正常的PullToRefresh不起作用)

我认为最好的库是:https://github.com/chrisbanes/Android-PullToRefresh。

适用于:

ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager