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

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

我能想到的一些可能性:

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

你有什么推荐吗?

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


当前回答

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

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

其他回答

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

适用于:

ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager

将此添加到布局中

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

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

 mSwipeRefreshLayout.setOnRefreshListener(() -> {

       // your refresh code here

    });

都做完了

Yalantis的“拉到刷新”非常有趣。 Gif for iOS,但你可以检查它:)

<com.yalantis.pulltorefresh.library.PullToRefreshView
android:id="@+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
    android:id="@+id/list_view"
    android:divider="@null"
    android:dividerHeight="0dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

那些正在寻找实现拉刷新功能的RecyclerView可以遵循我的简单教程如何实现拉刷新的RecyclerView在Android。

要导入的库

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.gridlayout:gridlayout:1.0.0'

XML代码

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

活动JAVA代码

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {

private SwipeRefreshLayout swipeRefreshLayout;

...

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        ...
        swipeRefreshLayout = findViewById(R.id.swipe_layout);
        initializeRefreshListener();
}

    void initializeRefreshListener() {

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // This method gets called when user pull for refresh,
                // You can make your API call here,
                // We are using adding a delay for the moment
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(swipeRefreshLayout.isRefreshing()) {
                            swipeRefreshLayout.setRefreshing(false);
                        }
                    }
                }, 3000);
            }
        });
    }

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

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

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