欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SwipeRefreshLayout使用

程序员文章站 2024-03-18 14:23:10
...

1、用法

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refreshClient"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner1">
    <!-- 这里是一个子view,仅能有一个子view,一般为AbsListView -->
</android.support.v4.widget.SwipeRefreshLayout>

让Activity继承SwipeRefreshLayout.OnRefreshListener
然后refreshLayout.setOnRefreshListener(this);
即可监听刷新动作,只要在onRefresh()里执行刷新代码即可

博主在使用时发现SwipeRefreshLayout会占满剩余屏幕,且它的子view也是一样,可以看SwipeRefreshLayout源码的onMeasure和onLayout方法。如果想要使它的子view是适应内容高度的话,博主测试了下,跟ScrollView的做法是一样的,但是这样子的话AbsListView就不能滑动了,从而知道SwipeRefreshLayout跟ScrollView还是不同的,之后就试了下在AbsListView外面套一层ScrollView,这时候就能让AbsListView自适应内容高度且能上下滑动了。

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mTarget == null) {
            ensureTarget();
        }
        if (mTarget == null) {
            return;
        }
        mTarget.measure(MeasureSpec.makeMeasureSpec(
                getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
                MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY));
        mCircleView.measure(MeasureSpec.makeMeasureSpec(mCircleDiameter, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(mCircleDiameter, MeasureSpec.EXACTLY));
        mCircleViewIndex = -1;
        // Get the index of the circleview.
        for (int index = 0; index < getChildCount(); index++) {
            if (getChildAt(index) == mCircleView) {
                mCircleViewIndex = index;
                break;
            }
        }
    }
    其中mCircleView就是那个刷新时出现的转圈圈的ImageView,而mTarget就是我们在布局文件里定义的SwipeRefreshLayout子View。可以看到mTarget和SwipeRefreshLayout的宽高是一致的,当然啦,不包括SwipeRefreshLayout的padding。
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        final int width = getMeasuredWidth();
        final int height = getMeasuredHeight();
        if (getChildCount() == 0) {
            return;
        }
        if (mTarget == null) {
            ensureTarget();
        }
        if (mTarget == null) {
            return;
        }
        final View child = mTarget;
        final int childLeft = getPaddingLeft();
        final int childTop = getPaddingTop();
        final int childWidth = width - getPaddingLeft() - getPaddingRight();
        final int childHeight = height - getPaddingTop() - getPaddingBottom();
        child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        int circleWidth = mCircleView.getMeasuredWidth();
        int circleHeight = mCircleView.getMeasuredHeight();
        mCircleView.layout((width / 2 - circleWidth / 2), mCurrentTargetOffsetTop,
                (width / 2 + circleWidth / 2), mCurrentTargetOffsetTop + circleHeight);
    }
    一开始博主有试过在这里让mTarget自适应内容高度,也成功了,结果就是不能滑动,跟上面的一个方法效果一样,所以最后只能选择再套一层ScrollView了。

这里只是android本身自带的刷新控件的使用,网上很多人把这个layout修改成下拉刷新,上拉加载的控件了,博主还没看过源码,水平较低,暂不知怎么实现。如果要实现,怎么的也要将SwipeRefreshLayout负责出来修改,同时也要把MaterialProgressDrawable和CircleImageView复制出来。