SwipeRefreshLayout 的基本使用,自动刷新和上拉加载更多

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/da_caoyuan/article/details/53977343

基本使用

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/lv_activity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></ListView>

    </android.support.v4.widget.SwipeRefreshLayout>


</LinearLayout>

关键 Java 代码:

        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        //swipeRefresh.setColorSchemeColors(Color.BLACK,Color.YELLOW,Color.GRAY);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                System.out.println("ListActivity.onRefresh");
          new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //inData(); //初始化数据
                        //listAdapter.notifyDataSetChanged();//更新adapter
                        swipeRefresh.setRefreshing(false);//刷新结束,隐藏刷新进度条

                    }
                });
            }
        }).start();
            }
        });

自动刷新

关于自动刷新,查阅资料,总结为以下几种方法:

方法一:

SwipeRefreshLayout.OnRefreshListener listener = new SwipeRefreshLayout.OnRefreshListener() {
            public void onRefresh() {
                System.out.println("ListActivity.onRefresh");
                refresh();
            }
        };
        swipeRefresh.setOnRefreshListener(listener);

        //自动刷新,也可以延时
        swipeRefresh.post(new Runnable() {
            @Override
            public void run() {
                swipeRefresh.setRefreshing(true);
            }
        });
        listener.onRefresh();

        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);

方法二:

参考博客:

http://blog.csdn.net/picasso_l/article/details/50510924

SwipeRefreshLayout进入页面自动刷新:
http://www.jianshu.com/p/fb0d5f9adf82

实现SwipeRefreshLayout首次进入自动刷新:
http://www.cnblogs.com/RaphetS/p/6036845.html


上拉加载更多

参考地址:
SwipeRefreshLayout完美添加及完善上拉加载功能

猜你喜欢

转载自blog.csdn.net/da_caoyuan/article/details/53977343
今日推荐