zoukankan      html  css  js  c++  java
  • 使用Google 官方的控件SwipeRefreshLayout实现下拉刷新功能

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

      

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

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

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

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

      

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="fill_parent"
     4     android:layout_height="fill_parent"
     5     android:orientation="vertical"
     6     tools:context="com.android.teypullrepfesh.MainActivity" >
     7 
     8     <TextView
     9         android:layout_width="fill_parent"
    10         android:layout_height="64dp"
    11         android:text="@string/hello_world" />
    12 
    13     <android.support.v4.widget.SwipeRefreshLayout
    14         android:id="@+id/refresh_layout"
    15         android:layout_width="fill_parent"
    16         android:layout_height="match_parent" >
    17 
    18         <ListView
    19             android:id="@+id/list"
    20             android:layout_width="fill_parent"
    21             android:layout_height="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 = { "姓名:菜菜", "性别:男", "年龄:20", "居住地:大连",
     6             "邮箱:cwr941012@gmail.com" };
     7     private String[] mListStr_ = { "姓名:翠翠", "性别:男", "年龄:23", "居住地:北京",
     8             "邮箱:cwr941012@gmail.com" };
     9     //定义两个不同的数据源
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.activity_main);
    14         listView = (ListView) findViewById(R.id.list);
    15         listView.setAdapter(new ArrayAdapter<String>(this,
    16                 android.R.layout.simple_list_item_1, mListStr));
    17         refreshLayout = (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     @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<String>(MainActivity.this,
    33                         android.R.layout.simple_list_item_1, mListStr_));
    34                 refreshLayout.setRefreshing(false);
    35             }
    36         }, 5000);
    37 
    38     }
    39 }
    MainActivity.java

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

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

  • 相关阅读:
    Java经典面试题及详解
    linux nc命令使用详解
    终端下更改文件显示颜色
    第二章 IoC Setter注入
    网络抓包wireshark
    一些软件软件开发原则
    开发原则之约定大于配置
    2016第31周六
    2016第31周五
    2016年第31周四
  • 原文地址:https://www.cnblogs.com/cwr941012/p/4110636.html
Copyright © 2011-2022 走看看