#Android学习#下拉刷新SwipeRefreshLayout
2016-03-08 15:50
363 查看
下拉刷新是我们经常会用到的一个功能,网上也能找到很多的开源下拉刷新的资源,如ActionBar-PullToRefresh。但这些都需要引入第三方的库,而事实上,Google官方也推出了它的下拉刷新组件SwipeRefreshLayout,可以让我们很方便的实现下拉刷新的效果。
SwipeRefreshLayout的使用非常简单,主要分为两步:
1)在布局文件里,添加SwipeRefreshLayout控件,然后再这个控件里添加另外一个可滚动的组件,如ListView、RecyclerView、 ScrollView等。
2)在Activity里找到对应的组件,然后调用setOnRefreshListener,之后可通过setColorSchemeResources来设置刷新进度条的颜色。
结合前面已经学习过的RecydlerView,我们就可以很方便的做出下拉刷新的效果了。
通过Android Studio新建project,选择Blank Activity。修改
注意
我们在刷新的时候通过线程停留5秒的时间,以体现效果。RecyclerView的使用可参考RecyclerView基础这篇文章。
SwipeRefreshLayout的使用非常简单,主要分为两步:
1)在布局文件里,添加SwipeRefreshLayout控件,然后再这个控件里添加另外一个可滚动的组件,如ListView、RecyclerView、 ScrollView等。
2)在Activity里找到对应的组件,然后调用setOnRefreshListener,之后可通过setColorSchemeResources来设置刷新进度条的颜色。
结合前面已经学习过的RecydlerView,我们就可以很方便的做出下拉刷新的效果了。
通过Android Studio新建project,选择Blank Activity。修改
content_main.xml增加刷新控件
// content_main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/swipe_refresh_layout_container" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.cyfloel.learnswiperefreshlayout.MainActivity" tools:showIn="@layout/activity_main"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout>
注意
app:layout_behavior="@string/appbar_scrolling_view_behavior"这句可以使得刷新控件在toolbar下显示,如果缺少这个属性,SwipeRefreshLayout根据我们设置的
match_parent属性就会覆盖整个屏幕
// MainActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // 找到下拉组件 swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout_container); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); // 设置listener swipeRefreshLayout.setOnRefreshListener(this); // 设置刷新进度条的颜色 swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_purple, android.R.color.holo_blue_bright, android.R.color.holo_orange_light, android.R.color.holo_red_light); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new MyRecyclerViewAdapter()); } @Override public void onRefresh() { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); Message msg = mHandler.obtainMessage(0x1); mHandler.sendMessage(msg); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 0x1){ Snackbar.make(findViewById(R.id.mycoordinatorLayout),"刷新成功",Snackbar.LENGTH_SHORT).show(); swipeRefreshLayout.setRefreshing(false); } } };
我们在刷新的时候通过线程停留5秒的时间,以体现效果。RecyclerView的使用可参考RecyclerView基础这篇文章。
相关文章推荐
- Android Studio新手全然指引
- Android之Fragment
- 第一个自定义view,折线图
- Android 逐帧动画
- android--屏幕旋转方法总结
- 4.10 Vitamio 快速导入androidstudio中和简单使用
- [Android]The connection to adb is down, and a severe error has occured.
- Android dex: method ID not in [0, 0xffff]: 65536问题
- Android---获取已安装应用列表
- android Notification动态更新
- android ImageView的属性android: 4000 scaleType,即ImageView.setScaleType(ImageView.ScaleType)
- Android nine-patch 图片
- android如何调用显示和隐藏系统默认的输入法
- 详解Android开发中Activity的四种launchMode
- Android分享笔记(3)Android 使用存放在存assets文件夹下的SQLite数据库
- Android ---读取dimen值
- android studio中如何删除项目
- 编写Android时遇到的问题
- Android中GridView使用
- Android项目:手机安全卫士(7)—— 手机防盗功能