一、SwipeRefreshLayout简单介绍
SwipeRefreshLayout是Google官方推出的一个用于下拉刷新的插件,位于android.support.v4.widget包中。利用SwipeRefreshLayout,我们可以让下拉刷新变得更简单。
二、SwipeRefreshLayout类
常用的成员函数:
1、setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener)//添加监听器
2、 setColorSchemeResources(int colorRes1, int colorRes2, int colorRes3, int colorRes4)//设置刷新等待循环的颜色
3、isRefreshing()//判断是否正在刷新
4、setRefreshing(boolean refreshing)//显示或者隐藏该插件
利用上面的几个常用的成员函数,基本上能够满足常用的下拉刷新功能。
三、使用步骤
1、在布局文件中添加,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swiperefreshlayout" android:layout_width="match_parent" android:layout_height="match_parent">
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:gravity="center" android:text="@string/hello_world" /> </ScrollView>
<ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="14dp" />
</android.support.v4.widget.SwipeRefreshLayout>
|
2、代码中使用,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public class MainActivity extends Activity { private SwipeRefreshLayout mSwipeRefreshLayout; private ListView mListView; private ArrayList<String> mList = new ArrayList<String>(); private ArrayAdapter<String> mAdapter;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
mListView = (ListView) this.findViewById(R.id.listview); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, setData()); mListView.setAdapter(mAdapter); mSwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swiperefreshlayout); mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override public void onRefresh() { new Handler().postDelayed(new Runnable() {
@Override public void run() { if (mSwipeRefreshLayout.isRefreshing()) { mList.add("" + System.currentTimeMillis()); mAdapter.notifyDataSetChanged(); mSwipeRefreshLayout.setRefreshing(false); } } }, 3000); } }); mSwipeRefreshLayout.setColorSchemeResources( android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); }
private ArrayList<String> setData(){ this.mList.add("1111111"); this.mList.add("2222222"); return mList; } }
|