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

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

我能想到的一些可能性:

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

你有什么推荐吗?

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


当前回答

我已经尝试实现一个拉刷新组件,它还远远没有完成,但演示了一个可能的实现,https://github.com/johannilsson/android-pulltorefresh。

主要逻辑在扩展ListView的PullToRefreshListView中实现。在内部,它使用smoothScrollBy (API Level 8)来控制头视图的滚动。这个小部件现在更新了,支持1.5及以后的版本,请阅读README以获得1.5的支持。

在你的布局中,你只需像这样添加它。

<com.markupartist.android.widget.PullToRefreshListView
    android:id="@+id/android:list"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    />

其他回答

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

适用于:

ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager

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

它被称为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的拉到刷新,试试这段代码,

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/pullToRefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</android.support.v4.widget.SwipeRefreshLayout>

活动类:

ListView lv = (ListView) findViewById(R.id.lv);
SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) findViewById(R.id.pullToRefresh);


lv.setAdapter(mAdapter);

pullToRefresh.setOnRefreshListener(new OnRefreshListener() {

        @Override
        public void onRefresh() {
            // TODO Auto-generated method stub

            refreshContent();

        }
    });



private void refreshContent(){ 

     new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                pullToRefresh.setRefreshing(false);
            }
        }, 5000);

 }

没有人提到新的类型的“拉刷新”,显示在操作栏的顶部,就像谷歌Now或Gmail应用程序。

有一个ActionBar-PullToRefresh库,它的工作原理完全相同。

将此添加到布局中

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

现在只需在代码中添加以下这些行

 mSwipeRefreshLayout.setOnRefreshListener(() -> {

       // your refresh code here

    });

都做完了