欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android SwipeRefreshLayout下拉刷新源码解析

程序员文章站 2024-03-03 16:19:10
本文实例为大家分享了swiperefreshlayout下拉刷新源码,供大家参考,具体内容如下 1.swiperefreshlayout是google在support v...

本文实例为大家分享了swiperefreshlayout下拉刷新源码,供大家参考,具体内容如下

1.swiperefreshlayout是google在support v4 19.1版本的library更新的一个下拉刷新组件,实现刷新效果更方便。

弊端:只有下拉

Android SwipeRefreshLayout下拉刷新源码解析

//设置刷新控件圈圈的颜色
swipe_refresh_layout.setcolorschemeresources(android.r.color.holo_blue_light, android.r.color.holo_orange_light, android.r.color.holo_red_light, android.r.color.holo_green_light);
//设置刷新控件背景色
swipe_refresh_layout.setprogressbackgroundcolorschemecolor(getresources().getcolor(android.r.color.white));
//设置滑动距离
swipe_refresh_layout.setdistancetotriggersync(100);
//设置大小模式
swipe_refresh_layout.setsize(swiperefreshlayout.default);
//设置下拉刷新控件状态隐藏
swipe_refresh_layout.setrefreshing(false);  

2.在xml文件中引用android.support.v4.widget.swiperefreshlayout控件,在里面可以放置任何一个控件,例如listview,gridview等。

<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> 

3.java代码

public class mainactivity extends activity implements swiperefreshlayout.onrefreshlistener {
 
 private swiperefreshlayout swipelayout;
 private listview listview;
 private listviewadapter adapter;
 private list<iteminfo> infolist;
  
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
 
  swipelayout = (swiperefreshlayout) this.findviewbyid(r.id.swipe_refresh);
  swipelayout.setonrefreshlistener(this);
   
  // 顶部刷新的样式
  swipelayout.setcolorscheme(android.r.color.holo_red_light, android.r.color.holo_green_light,
    android.r.color.holo_blue_bright, android.r.color.holo_orange_light);
 
  infolist = new arraylist<iteminfo>();
  iteminfo info = new iteminfo();
  info.setname("coin");
  infolist.add(info);
  listview = (listview) this.findviewbyid(r.id.listview);
  adapter = new listviewadapter(this, infolist);
  listview.setadapter(adapter);
 }
 
 public void onrefresh() {
  new handler().postdelayed(new runnable() {
   public void run() {
    swipelayout.setrefreshing(false);
    iteminfo info = new iteminfo();
    info.setname("coin-refresh");
    infolist.add(info);
    adapter.notifydatasetchanged();
   }
  }, 500);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。