官方实现下拉刷新

来源:互联网 发布:网络知识大赛 编辑:程序博客网 时间:2024/06/02 08:27

滑动刷新(swipe-to-refresh)

swipe-to-refresh在SwipeRefreshLayout的内部定义,当用户向竖直方向滑动页面的时候,会显示一个progress bar,并且触发回调方法,你可以把SwipeRefreshLayout作为父布局,在其中添加ListView或者GridView,并且实现相应的回调方法。

本篇文章向你展示了怎么在app中添加滑动刷新,和当无法进行滑动刷新时如何通过在overflow中的按钮进行手动刷新。

如图:
这里写图片描述
用过知乎移动端的可以发现在3.2.0版本中用的就是此下拉刷新样式。

添加SwipeRefreshLayout组件

要实现滑动刷新,首先在app中添加SwipeRefreshLayout组件,并且作为一个Listview或者一个Gridview的父布局,一定要注意,SwipeRefreshLayout只支持一个Listview或者Gridview作为其子view。

代码示例:

<android.support.v4.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">    <ListView        android:id="@android:id/list"        android:layout_width="match_parent"        android:layout_height="match_parent" /></android.support.v4.widget.SwipeRefreshLayout>

在action bar上添加refresh action

开发者应该在actionbar上添加一个刷新按钮,以便当用户无法通过滑动页面来刷新的时候,可以通过点击刷新按钮来手动的进行刷新,一般来说,我们应该把按钮放在overflow中,应为如果我们把按钮直接放在actionbar上,用户可能认为此按钮和一般的滑动刷新,功能是不一样的,这就会对用户造成一定的误导。

在overflow中添加按钮在Tool Bar(二)上已经详细讲解过了,如下代码所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:id="@+id/menu_refresh"        android:showAsAction="never"        android:title="@string/menu_refresh"/></menu>

对滑动刷新动作做出响应

当用户做出滑动操作的时候,系统显示progress bar,并且调用app的回调方法,你应该在此方法中进行数据的更新。

为了响应滑动动作,应该实现SwipeRefreshLayout.OnRefreshListener接口和onRefresh()方法,此方法会在滑动时被回调。

你应该但对数据的更新操作单独的定义成一个方法,然后在onRefresh()调用此方法,用这种方式可以在用户用actionbar按钮执行更新操作时进行复用。

在数据更新结束的时候,应该调用SwipeRefreshLayout中的setRefreshing(false)方法,目的是数据更新完毕后取消progress bar。

示例代码:

/* * Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when the user * performs a swipe-to-refresh gesture. */mySwipeRefreshLayout.setOnRefreshListener(    new SwipeRefreshLayout.OnRefreshListener() {        @Override        public void onRefresh() {            Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");            // This method performs the actual data-refresh operation.            // The method calls setRefreshing(false) when it's finished.            myUpdateOperation();        }    });

响应Refresh Action

如果用户使用action bar进行刷新,系统会调用onOptionsItemSelected()方法,在app中应该通过显示progress bar来刷新数据。

为了响应操作,我们首先要覆写onOptionsItemSelected(),在其内部通过调用setRefreshing()来显示progress bar(刚才让progress消失好像就是调用的这个方法,对,消失传入参数false,显示传入true),然后当数据更新完毕后,别忘了再调用此方法让progress bar消失。

代码示例:

/* * Listen for option item selections so that we receive a notification * when the user requests a refresh by selecting the refresh action bar item. */@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    switch (item.getItemId()) {        // Check if user triggered a refresh:        case R.id.menu_refresh:            Log.i(LOG_TAG, "Refresh menu item selected");            // Signal SwipeRefreshLayout to start the progress indicator            mySwipeRefreshLayout.setRefreshing(true);            // Start the refresh background task.            // This method calls setRefreshing(false) when it's finished.            myUpdateOperation();            return true;    }    // User didn't trigger a refresh, let the superclass handle this action    return super.onOptionsItemSelected(item);}
1 0
原创粉丝点击