下拉刷新是每个app中都会用到的一个功能,最近接手一个项目的时候,看到了SwipeRefreshLayout 这个控件,表示以前没用过,以前用的都是pullTorefresh,于是去网上查找了一下,谷歌的这个控件将刷新和listview做了解耦。这里做一下记录。
先看一下效果图:
首先贴一下布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/id_listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>
然后是代码:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.util.TypedValue; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener { private ListView mListView; private SwipeRefreshLayout mSwipeLayout; private static final int REFRESH_COMPLETE = 0X110; private ArrayAdapter<String> mAdapter; private List<String> mDatas = new ArrayList<String>(Arrays.asList("Android", "我要学Android", "我要学IOS", "我要学H5", "我要学PHP", "我要学ASP.NET")); private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case REFRESH_COMPLETE: mDatas.addAll(Arrays.asList("Java是基础", "PHP是最好的鱼眼", "C#也不错哦")); mAdapter.notifyDataSetChanged(); mSwipeLayout.setRefreshing(false); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.id_listview); mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly); setSwipeRefresh(); onRefresh();// 手动刷新 // mSwipeLayout.setRefreshing(true); // mSwipeLayout.post(new Runnable() { // // @Override // public void run() { // // TODO Auto-generated method stub // mSwipeLayout.setRefreshing(true); // } // }); mSwipeLayout.setOnRefreshListener(this); // setColorScheme()已经弃用,使用setColorSchemeResources()来设置颜色。 mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mDatas); mListView.setAdapter(mAdapter); } @Override public void onRefresh() { // TODO Auto-generated method stub mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000); } private void setSwipeRefresh() { TypedValue typed_value = new TypedValue(); this.getTheme().resolveAttribute( android.support.v7.appcompat.R.attr.actionBarSize, typed_value, true); // mSwipeLayout.setProgressViewOffset(false, 0, // getResources().getDimensionPixelSize(typed_value.resourceId)); mSwipeLayout.setRefreshing(true); } }
这里就介绍完毕了,虽然效果很炫,但是唯一的缺点就是只有下拉刷新,没有上拉加载。那么下一节我们将自定义实现SwipeFefreshLayout的上拉刷新和下拉加载。
这里附上demo的下载链接:
http://download.csdn.net/detail/u014727709/9710933
欢迎start,欢迎评论,欢迎指正