热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Google官方的控件SwipeRefreshLayout实现下拉刷新功能

之前做东西的时候,经常会用到下拉刷新的功能,之前大家都在使用Github上的一个很著名的开源项目PullToRefresh但是,现在好消息

  之前做东西的时候,经常会用到下拉刷新的功能,之前大家都在使用Github上的一个很著名的开源项目 PullToRefresh 

  

  但是,现在好消息来了,google在19.1版本的support-v4兼容包下面正式提供了官方的下拉刷新组件——SwipeRefreshLayout

  注意,你要使用这个组件必须要把你的支持库升级到19.1版本以上

 

  我们只需要在需要进行刷新的控件外面加上 SwipeRefreshLayout 就可以了  ,但是 ,这个控件的child必须是可滑动的View,比如说ScrollerView或者ListView等

 

  不多说,直接上代码,布局文件

  

1 <LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"
2 xmlns:tools&#61;"http://schemas.android.com/tools"
3 android:layout_width&#61;"fill_parent"
4 android:layout_height&#61;"fill_parent"
5 android:orientation&#61;"vertical"
6 tools:context&#61;"com.android.teypullrepfesh.MainActivity" >
7
8 <TextView
9 android:layout_width&#61;"fill_parent"
10 android:layout_height&#61;"64dp"
11 android:text&#61;"&#64;string/hello_world" />
12
13 <android.support.v4.widget.SwipeRefreshLayout
14 android:id&#61;"&#64;&#43;id/refresh_layout"
15 android:layout_width&#61;"fill_parent"
16 android:layout_height&#61;"match_parent" >
17
18 <ListView
19 android:id&#61;"&#64;&#43;id/list"
20 android:layout_width&#61;"fill_parent"
21 android:layout_height&#61;"match_parent" />
22 android.support.v4.widget.SwipeRefreshLayout>
23
24 LinearLayout>

activity_main.xml

 

  然后在代码中的使用也很简单

 

1 public class MainActivity extends Activity implements OnRefreshListener {
2
3 public ListView listView;
4 public SwipeRefreshLayout refreshLayout;
5 private String[] mListStr &#61; { "姓名&#xff1a;菜菜", "性别&#xff1a;男", "年龄&#xff1a;20", "居住地&#xff1a;大连",
6 "邮箱&#xff1a;cwr941012&#64;gmail.com" };
7 private String[] mListStr_ &#61; { "姓名&#xff1a;翠翠", "性别&#xff1a;男", "年龄&#xff1a;23", "居住地&#xff1a;北京",
8 "邮箱&#xff1a;cwr941012&#64;gmail.com" };
9 //定义两个不同的数据源
10 &#64;Override
11 protected void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.activity_main);
14 listView &#61; (ListView) findViewById(R.id.list);
15 listView.setAdapter(new ArrayAdapter(this,
16 android.R.layout.simple_list_item_1, mListStr));
17 refreshLayout &#61; (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
18 refreshLayout.setOnRefreshListener(this);
19 //设置一个监听器
20 refreshLayout.setColorSchemeColors(android.R.color.holo_orange_light,
21 android.R.color.holo_purple, android.R.color.holo_blue_dark,
22 android.R.color.holo_red_light);
23 //设置刷新条的颜色
24
25 }
26
27 &#64;Override
28 public void onRefresh() {
29 // TODO Auto-generated method stub
30 new Handler().postDelayed(new Runnable() {
31 public void run() {
32 listView.setAdapter(new ArrayAdapter(MainActivity.this,
33 android.R.layout.simple_list_item_1, mListStr_));
34 refreshLayout.setRefreshing(false);
35 }
36 }, 5000);
37
38 }
39 }

MainActivity.java

 

  在这里面&#xff0c;重写了 onRefresh() 函数进行刷新之中的操作

 

  基本上就是这样了&#xff0c;希望google能尽快把下拉加载更多的功能也添加进去就更加完美了


转载于:https://www.cnblogs.com/cwr941012/p/4110636.html


推荐阅读
author-avatar
雪国文话天下
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有