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

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

我能想到的一些可能性:

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

你有什么推荐吗?

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


当前回答

如果你想在webview或其他地方使用pull来刷新,然后简单地复制粘贴这段代码到你的项目中,它工作得很好。

XML源代码:

   <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <WebView
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true">
    </WebView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

JAVA代码:

  final SwipeRefreshLayout pullToRefresh = findViewById(R.id.swiperefresh);
    pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh()
        {
          Toast.makeText(WebBrowser.this,"pull to refresh",Toast.LENGTH_LONG).show();
            mwebView.loadUrl(mainUrl);
            pullToRefresh.setRefreshing(false);//if false then refresh progress dialog will disappear when page loading finish, but if it is true then always the refresh load dialog show even after the page load or not
        }
    });

构建。Gradle源代码:

 implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
      

其他回答

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

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

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

那些正在寻找实现拉刷新功能的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);
            }
        });
    }

如果你想在webview或其他地方使用pull来刷新,然后简单地复制粘贴这段代码到你的项目中,它工作得很好。

XML源代码:

   <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <WebView
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true">
    </WebView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

JAVA代码:

  final SwipeRefreshLayout pullToRefresh = findViewById(R.id.swiperefresh);
    pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh()
        {
          Toast.makeText(WebBrowser.this,"pull to refresh",Toast.LENGTH_LONG).show();
            mwebView.loadUrl(mainUrl);
            pullToRefresh.setRefreshing(false);//if false then refresh progress dialog will disappear when page loading finish, but if it is true then always the refresh load dialog show even after the page load or not
        }
    });

构建。Gradle源代码:

 implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
      

我已经尝试实现一个拉刷新组件,它还远远没有完成,但演示了一个可能的实现,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"
    />