Android下拉刷新官方版
作者:wzben 时间:2021-11-25 06:52:51
网上关于下拉刷新的文章也不少,不过都太长了。恰好发现了官方的下拉刷新库,而且效果还是不错的,简洁美观,用得也挺方便。
下面是效果图:
我的好友原来是空的,刷新后多了两个。
使用还是挺方便的,在布局文件中加入SwipeRefreshLayout ,这个就是下拉刷新的布局。
我在SwipeRefreshLayout的里面还加入了一个ListView 因为我主要用下拉刷新更新了listView里面的内容 。
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
代码写到这里后,只要在主界面向下滑动到顶部,就会出现下拉刷新了。
然后我们要实例化这个下拉刷新布局,设置好它在刷新时要干什么,完成刷新后,旋转的图标要消失。
mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.haoyouliebiaoshuaxin);//实例化
//handler 用来更新UI的
haoyouhandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//要做的事
Item i1 = new Item();
i1.name = "呵呵";
i1.account = 25566;
haoyou.add(i1);
Item i2 = new Item();
i2.name = "哈哈";
i2.account = 25577;
haoyou.add(i2);
haoyouadpter = new liebiaoAdapter(MainActivity.this, R.layout.liebiao, haoyou);
haoyoulist.setAdapter(haoyouadpter);
//取消更新的动画
mSwipeLayout.setRefreshing(false);
}
};
//设置 *
mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
//正在更新的时候执行什么代码
public void onRefresh() {
new Thread(new Runnable() {
@Override
public void run() {
//等待一秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//发消息给handler更新UI
Message ok = new Message();
haoyouhandler.sendMessage(ok);
}
}).start();
我是在 * 的onRefresh函数中,等待一秒,然后发消息给Handler去更新UI,更新完后再取消更新。
标签:Android,下拉刷新
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现的新浪微博分享代码实例
2023-07-06 08:33:13
Java数据结构之链表实现(单向、双向链表及链表反转)
2021-10-17 18:04:25
![](https://img.aspxhome.com/file/2023/9/128849_0s.png)
mybatis 通过拦截器打印完整的sql语句以及执行结果操作
2023-07-06 04:26:42
浅谈C#中的for循环与foreach循环
2021-08-23 03:27:38
Spring Security实现微信公众号网页授权功能
2021-07-09 08:28:46
![](https://img.aspxhome.com/file/2023/4/72284_0s.png)
Java设计模式之抽象工厂模式实例详解
2023-11-29 04:04:57
![](https://img.aspxhome.com/file/2023/1/60341_0s.png)
Java动态规划之硬币找零问题实现代码
2023-01-23 20:37:38
Android实现 EditText输入手机号空格功能
2021-09-17 00:29:27
springboot使用redis实现从配置到实战
2023-05-05 09:49:08
C#将Sql数据保存到Excel文件中的方法
2023-11-11 08:34:26
SpringBoot ResponseBody返回值处理的实现
2023-06-29 23:18:43
Android App中实现简单的刮刮卡抽奖效果的实例详解
2023-07-12 19:06:44
![](https://img.aspxhome.com/file/2023/3/138903_0s.gif)
Elasticsearch percolate 查询示例详解
2021-07-15 04:57:54
SpringBoot 3.0 新特性内置声明式HTTP客户端实例详解
2022-11-25 13:47:20
![](https://img.aspxhome.com/file/2023/9/84289_0s.png)
Java使用条件语句和循环结构确定控制流(实例)
2022-07-04 04:52:35
ViewFlipper实现上下翻滚轮播效果
2023-10-06 08:55:40
java并发之ArrayBlockingQueue详细介绍
2023-04-23 07:30:01
java request.getHeader("user-agent")获取浏览器信息的方法
2022-05-28 01:04:26
![](https://img.aspxhome.com/file/2023/6/125336_0s.png)
Android广播接实现监听电话状态(电话的状态,拦截)
2022-12-21 18:50:49
![](https://img.aspxhome.com/file/2023/4/111704_0s.png)
Java操作Redis详细介绍
2023-07-27 06:53:04