欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android SwipeRefreshLayout下拉刷新源码解析

时间:2021-05-21 08:29:17|栏目:Android代码|点击:

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

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

弊端:只有下拉

//设置刷新控件圈圈的颜色
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);
 }
}

上一篇:android实现筛选菜单效果

栏    目:Android代码

下一篇:Android编程重写ViewGroup实现卡片布局的方法

本文标题:Android SwipeRefreshLayout下拉刷新源码解析

本文地址:http://www.codeinn.net/misctech/126099.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有