您的位置:首页 > 移动开发 > Android开发

Android官方出品SwipeRefreshLayout下拉刷新组件

2014-04-01 14:17 393 查看


android-support-v4更新到19.1.0


Android
Support Library, revision 19.1.0 (March 2014)

Changes for v4 support library:
Added the 
SwipeRefreshLayout
 class,
which enables users to refresh the contents of a view with a vertical swipe gesture.
Fixed accessibility issues with navigation drawers.
Changes for v7 appcompat library:
Fixed background issues with the action bar.

是的,如你所见,更新的内容并不多,新加入的内容只有一个:下拉刷新组件!


SwipeRefreshLayout官方Demo演示



官方API解释:


SwipeRefreshLayout


extends ViewGroup

java.lang.Object
   ↳android.view.View
    ↳android.view.ViewGroup
     ↳android.support.v4.widget.SwipeRefreshLayout


Class Overview

The SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed.
The SwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content. If the listener determines there should not be a refresh,
it must call setRefreshing(false) to cancel any visual indication of a refresh. If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child. This view will also be made the target of the gesture and will be forced to match both the width and the height supplied
in this layout. The SwipeRefreshLayout does not provide accessibility events; instead, a menu item must be provided to allow refresh of the content wherever this gesture is used.


代码步骤:

1.在布局文件中声明控件:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_widget"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- some full screen pullable view that will be the offsetable content -->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"/>
</android.support.v4.widget.SwipeRefreshLayout>


2.SwipeRefreshLayoutActivity的onCreate()方法中:
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
//1.设置视图
setContentView(R.layout.swipe_refresh_widget_sample);
//2.实例化SwipeRefreshLayout控件
mSwipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_widget);
//3.给SwipeRefreshLayout控件的ProgressBar设置4个颜色值
mSwipeRefreshWidget.setColorScheme(R.color.color1, R.color.color2, R.color.color3,
R.color.color4);
//4.实例化SwipeRefreshLayout控件下的唯一子控件ListView
mList = (ListView) findViewById(R.id.content);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, TITLES);
mList.setAdapter(arrayAdapter);
//5.给SwipeRefreshLayout控件设置监听器
mSwipeRefreshWidget.setOnRefreshListener(this);
}


3.SwipeRefreshLayoutActivity实现OnRefreshListener接口,并重写onRefresh方法:
@Override
public void onRefresh() {

//用户下拉操作时,回调onRefresh()方法,在此方法中我们去调用自定义方法refresh()

//以下的方法中才是真正下拉刷新要做的事情,比如开启子线程,请求数据
refresh();
}


4.实现refresh()方法,真正要执行的逻辑
private void refresh() {

//每次下拉刷新时,移除原有线程,重新启动子线程去执行逻辑
mHandler.removeCallbacks(mRefreshDone);
mHandler.postDelayed(mRefreshDone, 1000);
}
//多线程启动,执行耗时操作
private final Runnable mRefreshDone = new Runnable() {

@Override
public void run() {

//在这里写真正的耗时操作:比如请求数据

//耗时操作完成时,要取消刷新动画
mSwipeRefreshWidget.setRefreshing(false);

}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: