Android UI:SwipeRefreshLayout

简介:SwipeRefreshLayout

SwipeRefreshLayout是google新出的下拉刷新的控件,但不可以上拉加载更多。

相关的方法:

设置进度条的方法有:(进度条就是圆圈)

setProgressBackgroundColor(int resId):进度条背景色
setProgressBackgroundColorSchemeColor(int color):进度条背景色
setProgressBackgroundColorSchemeResource(int colorRes):进度条背景色

setProcessViewOffSet(boolean scale,int start,int end)::进度条出现的时候是否缩放,Y轴的偏移量范围
setSize(int size)圆圈的大小,2个默认值:LARGE + DEFAULT,设置具体的数字无效
setColorSchemeResources(int... colorResIds):圆圈的颜色,白底黑圈(最多4种颜色)

其他方法:

setRefreshing(boolean refreshing):控制刷新状态(True:进度条一直存在;false:进度条消失)
isRefreshing():正在刷新中(返Yes/NO)

常用的方法就4种:

if (swipeRefreshLayout.isRefreshing()) {//是否正在刷新
    swipeRefreshLayout.setRefreshing(false);//刷新停止
}
swipeRefreshLayout.setOnRefreshListener(this);//设置刷新监听
swipeRefreshLayout.setColorSchemeResources(R.color.item3, R.color.item11, R.color.gold);//设置刷新时圆圈的颜色

接下来,通过案例演示

SwipeRefeshLayout内包裹RecyclerView,下拉刷新出现进度条,并添加数据。

效果图:

这里写图片描述

步骤:

1 找到这2个控件
2 设置recyclerview
3 设置swipeRefreshLayout(设置进度条+监听)

代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.cqc.swiperefreshlayoutdemo.MainActivity">

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/swipeRefreshLayout">

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

</RelativeLayout>

item_main.xml

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

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="45dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#FF4081"/>
</LinearLayout>

初始化recyclerView:

private void initRecyclerView() {
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    adapter = new MyAdapter();
    recyclerView.setAdapter(adapter);
}

初始化SwipeRefreshLayout

 private void initSwipeRefreshLayout() {
        //设置下拉圆圈:是否缩放,出现的位置,结束的位置(进度条在Y轴的展示范围)
        swipeRefreshLayout.setProgressViewOffset(true, 10, 30);
        //设置圆圈的背景色
        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(getResources().getColor(android.R.color.white));
//        swipeRefreshLayout.setProgressBackgroundColor(android.R.color.white);
//        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);

        //设置下拉圆圈的大小,只有2种常量LARGE + DEFAULT,设置具体的数字无效
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);

        //设置下拉圆圈的颜色变化,默认白底黑圈进度条,不是setColorSchemeColors()
        swipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        //刷新过程2秒
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        list.add(0,"item_add");
                        adapter.notifyItemInserted(0);
                        recyclerView.scrollToPosition(0);//回到顶部
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 2000);
            }
        });
    }

源码地址:https://git.oschina.net/AndroidUI/SwipeRefreshLayoutDemo.git

SwipeRefreshLayout继承ViewGroup,但是一般只可以有一个子控件。

SwipeRefreshLayout继承ViewGroup,但是一般只可以有一个子控件。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_light"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/colorAccent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

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

这里写图片描述

SwipeRefreshListFragment

google sample: https://github.com/googlesamples/android-SwipeRefreshListFragment

ListAdapter adapter = new ArrayAdapter<String>(
        getActivity(),
        android.R.layout.simple_list_item_1,
        android.R.id.text1,
        Cheeses.randomList(LIST_ITEM_COUNT));

// Set the adapter between the ListView and its backing data.
setListAdapter(adapter);

界面可见是自动刷新

参考:SwipeRefreshLayout进入页面自动刷新

如果希望一进入页面就执行下拉的动画
这个方法是没有效果的:

swipeRefreshLayout.setRefreshing(true);

这个方法只是显示刷新效果,但是不会调用onRefresh()方法,所以必须创建OnRefreshListener对象,调用onRefresh()方法。

@Override
protected void onResume() {
    super.onResume();
    swipeRefreshLayout.setRefreshing(true);
    onRefreshListener.onRefresh();
}

若是fragment+ViewPager,那么上述方法无效,
可以采用

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser && isViewCreated) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
                onRefreshListener.onRefresh();
            }
        });
    }
}
private boolean isViewCreated = false;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    isViewCreated = true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值