下拉刷新功能之前一直使用的是XlistView很方便我前面的博客有介绍
SwipeRefreshLayout是google官方推出的下拉刷新控件使用方法也比较简单
今天就来使用下SwipeRefreshLayout 以后再需要时可以参考.
首先在布局里面加入SwipeRefreshLayout 布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiperefreshlayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/tab_topical_list" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
Activity文件中的代码
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout); mSwipeRefreshLayout.setColorSchemeResources(R.color.colorRed , R.color.colorGre , R.color.colorBlu); mSwipeRefreshLayout.setProgressViewEndTarget(true , 150); mSwipeRefreshLayout.setOnRefreshListener(this);
使用setColorSchemeResources设置下拉刷新的图标颜色可设置四种颜色.
setProgressViewEndTarget(true , 150)设置距离顶端的距离
setOnRefreshListener(this)设置监听
监听方法:
//滑动手势监听 , 加载数据 @Override public void onRefresh() { Toast.makeText(getActivity() , "loding..." , Toast.LENGTH_SHORT).show(); getDate();//获取数据 mSwipeRefreshLayout.setRefreshing(false); }
搞定了上面两步就可以使用SwipeRefreshLayout这个控件了.
看下效果:
很简单吧.