谷歌官方控件SwipeRefreshLayout实现下拉刷新

前一阵子,学习了一下下拉刷新,github上有很多写好的,而且有的写的很好,但是感觉很复杂,偶然间看到了SwipeRefreshLayout,谷歌官方推出的下拉刷新控件,使用起来非常方便,于是就学洗了一下。
先来看官方的解释:
The SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via a vertical swipe gesture. The activity that instantiates this view should add an OnRefreshListener to be notified whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout will notify the listener each and every time the gesture is completed again; the listener is responsible for correctly determining when to actually initiate a refresh of its content. If the listener determines there should not be a refresh, it must call setRefreshing(false) to cancel any visual indication of a refresh. If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child. This view will also be made the target of the gesture and will be forced to match both the width and the height supplied in this layout. The SwipeRefreshLayout does not provide accessibility events; instead, a menu item must be provided to allow refresh of the content wherever this gesture is used.


有点多,翻译过来大概意思就是:用户可以在任何时候通过一个垂直的滑动手势来更新一个view里面的内容。Activity在实例化一个view的时候需要添加OnRefreshListener来通知这个垂直的滑动手势何时结束。每次滑动结束时,SwipeRefreshLayout都会通知这个监听器,这个监听器的职责是用来决定什么时候更新所要刷新的内容,如果要取消刷新,则调用setRefreshing(false)来取消刷新的可视化视图(就是那个显示刷新状态的图标)。注意,这个控件只能有一个子View,一会在代码中会看到。
下面就附上代码进行说明:

首先是布局文件:
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<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>
 注意:包名不要写错了,可以看到这个控件下只有一个<listview>,如果里面再放个其他view就会出问题,也就是官方说的只能有一个子view

再来看activity代码

    public class MainActivity extends Activity implements OnRefreshListener{
    private static final int REFRESH_COMPLETE = 0x110;   //这是一个刷新完的状态值,可以设置为其他的
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private ListView mListView;
    private ArrayAdapter<String> mAdapter;
    private List<String> mDatas=new ArrayList<String>(Arrays.asList("hello","world","java","google","android"));


    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if(msg.what==REFRESH_COMPLETE) {
                mDatas.addAll(Arrays.asList("你好","我好","大家好"));
                mAdapter.notifyDataSetChanged();
                mSwipeRefreshLayout.setRefreshing(false);
            }

        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.id_listview);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mDatas);
        mListView.setAdapter(mAdapter);

    }

    @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
    }
}

activity实现了OnRefreshListener接口,重写onRefresh()方法,代码比较简单,就不一一解释了。
这个控件也可以实现上拉加载,具体的可以去谷歌官网上看。
地址:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
ps:第一次用markdown编辑 不太习惯

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值