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

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

我能想到的一些可能性:

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

你有什么推荐吗?

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


当前回答

我们首先应该知道什么是拖动刷新布局在android。我们可以在android中调用滑动刷新来刷新。当你从上到下滑动屏幕时,它会基于setOnRefreshListener做一些动作。

这是一个演示如何实现android拉刷新的教程。我希望这能有所帮助。

其他回答

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

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

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

我认为最简单的方法是android支持库提供的:

android.support.v4.widget.SwipeRefreshLayout;

一旦导入,你就可以像下面这样定义你的布局:

  <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refresh"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    <android.support.v7.widget.RecyclerView
        xmlns:recycler_view="http://schemas.android.com/apk/res-auto"
        android:id="@android:id/list"
        android:theme="@style/Theme.AppCompat.Light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/button_material_light"
        >

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

我假设您使用的是回收视图而不是列表视图。然而,listview仍然可以工作,所以你只需要用listview替换recyclerview并更新java代码中的引用(Fragment)。

在您的活动片段中,您首先实现接口swiperfreshlayout。OnRefreshListener: i, e

public class MySwipeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
private SwipeRefreshLayout swipeRefreshLayout;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item, container, false);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh);
        swipeRefreshLayout.setOnRefreshListener(this);
}


 @Override
  public void onRefresh(){
     swipeRefreshLayout.setRefreshing(true);
     refreshList();
  }
  refreshList(){
    //do processing to get new data and set your listview's adapter, maybe  reinitialise the loaders you may be using or so
   //when your data has finished loading, cset the refresh state of the view to false
   swipeRefreshLayout.setRefreshing(false);

   }
}

希望这对大众有所帮助

我们首先应该知道什么是拖动刷新布局在android。我们可以在android中调用滑动刷新来刷新。当你从上到下滑动屏幕时,它会基于setOnRefreshListener做一些动作。

这是一个演示如何实现android拉刷新的教程。我希望这能有所帮助。

如果你想在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'