改造SwipeRefreshLayout,实现折叠效果

123.gif

要实现如上图的效果,红色区域是筛选条件的区域,随着想向上滚动并且停留在顶部。

第一感觉是想用CoordinatorLayout.Behavior 来实现,将Toolbar+图片区域+红色筛选区域作为折叠的头部。但是这样SwipeRefreshLayout的下拉圈圈就不是从Toolbar下面下拉出来的。

我尝试过自己用CoordinatorLayout.Behavior来实现这这个效果,而且上下滚动的效果已经实现了,但是写到下拉刷新处的时候,我暂时放弃了,我想到了另外一个方法,就是仿CoordinatorLayout.Behavior 来拦截SwipeRefreshLayout 的nestedscroll事件,因为SwipeRefreshLayout也继承了NestedScrollingParent。

public class MySwipeRefreshLayout extends SwipeRefreshLayout {
    private static final String TAG = "MySwipeRefreshLayout";
    private static  int HEADER_EXPEND_HEIGTH =0;
    private  View rootScrollLayout;
    private View mScrollUpChild;
    private int curentTranslationY;

    public MySwipeRefreshLayout(Context context) {
        this(context, null);
    }

    public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        HEADER_EXPEND_HEIGTH = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, context.getResources().getDisplayMetrics());
        curentTranslationY = HEADER_EXPEND_HEIGTH;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        rootScrollLayout = findViewById(R.id.rootScrollLayout);
        Log.i(TAG, "MySwipeRefreshLayout: rootScrollLayout=" + rootScrollLayout);
        rootScrollLayout.setTranslationY(curentTranslationY);

    }

    @Override
    public boolean canChildScrollUp() {
        if (mScrollUpChild != null) {
            return ViewCompat.canScrollVertically(mScrollUpChild, -1);
        }
        return super.canChildScrollUp();
    }

    public void setScrollUpChild(View view) {
        mScrollUpChild = view;
    }


    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        Log.i(TAG, "onStartNestedScroll: child="+child);
        //接收垂直方向的滚动事件,SwipeRefreshLayout默认是,只要正在刷新,父控件就不接受嵌套事件,由scrollview自己内部消耗
        return isEnabled() &&  (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
//        return super.onStartNestedScroll(child, target, nestedScrollAxes);
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        if (dy > 0) {//向上滚动
            if (curentTranslationY>=0) {//可以整体先向上滚动
                int min = (int) Math.min(rootScrollLayout.getTranslationY(), dy);
                curentTranslationY = (int) (rootScrollLayout.getTranslationY() - min);
                rootScrollLayout.setTranslationY(curentTranslationY);
                consumed[1] = min;
            }
        }
        super.onNestedPreScroll(target, dx, dy, consumed);
        Log.i(TAG, "onNestedPreScroll: ");
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {

        int max = 0;
        if (dyConsumed <= 0 && dyUnconsumed < 0) {//向下滚动
            if (curentTranslationY<HEADER_EXPEND_HEIGTH) {//可以整体先向下滚动
                 max = (int) Math.max(rootScrollLayout.getTranslationY()-HEADER_EXPEND_HEIGTH, dyUnconsumed);
                curentTranslationY = (int) (rootScrollLayout.getTranslationY() - max);
                rootScrollLayout.setTranslationY(curentTranslationY);
            }
            if (isRefreshing()) {//如果正在更新,则不显示让SwipeRefreshLayout处理,否则会二度下拉正在更新
                return;
            }
        }
        //如果还有多余的需要SwipeRefreshLayout 处理,比如再下拉显示正在更新
        super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed-max);
        Log.i(TAG, "onNestedScroll: ");
    }

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        Log.i(TAG, "drawChild: child=" + child);
        return super.drawChild(canvas, child, drawingTime);
    }
}

onlayout

主要处理红色筛选区与scrollview所在区域的setTranslationY偏移,以露出imageView,实现滑动遮盖的效果

由于滚动后系统有可能会调用onlayout方法,所以要记录当前的curentTranslationY,在onlayout的时候设置他的偏移

onStartNestedScroll

只要是垂直方向的滚动就都要处理,SwipeRefreshLayout默认的是在Refreshing状态就不接收子view的nested事件。而这里正在刷新的时候,整体还要能向上滚动,所以重写了此处。

onNestedPreScroll

当开始向上滚动的时候,先整体向上移动SwipeRefreshLayout的内部控件,剩余的再给子view去内部消耗
向下滚动的时候,先由子view内部消耗。

onNestedScroll
当子view 滚动完毕后,如果是向下滚动,先整体向下移动SwipeRefreshLayout的内部控件,还有多余的就给SwipeRefreshLayout自己处理,比如显示下拉进度条。而如果正在刷新的时候,是不应该二次显示下拉进度条的,这也是为啥isRefreshing 为true的时候,直接return的原因,否则你会看到下拉进度条再次被下拉下来。

布局文件如下:

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

    android:orientation="vertical"
    tools:context="com.tospur.exmind.testswiperefresh.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>

    <com.tospur.exmind.testswiperefresh.MySwipeRefreshLayout
        android:id="@+id/mySwipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/mImgView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@mipmap/test"
                />
            <LinearLayout
                android:id="@+id/rootScrollLayout"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:id="@+id/mFilterView"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:background="#f0f"
                    android:layout_height="72dp"></LinearLayout>
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:background="#ff0"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
            </LinearLayout>

        </FrameLayout>
    </com.tospur.exmind.testswiperefresh.MySwipeRefreshLayout>

</LinearLayout>

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setSupportActionBar((Toolbar) findViewById(R.id.toolBar));
        MySwipeRefreshLayout mySwipeRefreshLayout = (MySwipeRefreshLayout) findViewById(R.id.mySwipeRefreshLayout);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new MAdapter());
        mySwipeRefreshLayout.setScrollUpChild(recyclerView);


    }

    private class MAdapter extends RecyclerView.Adapter<MHolder> {
        @Override
        public MHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            TextView textView = new TextView(parent.getContext());
            return new MHolder(textView);
        }

        @Override
        public void onBindViewHolder(MHolder holder, int position) {
            holder.textView.setText("A "+position);
        }

        @Override
        public int getItemCount() {
            return 50;
        }
    }

    private class MHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public MHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView;
        }
    }
}

如果要实现平滑滚动,就是imageview随着一起向上滚动,只需要做个简单的调整

123.gif
public class MySwipeRefreshLayout2 extends SwipeRefreshLayout {
    private static final String TAG = "MySwipeRefreshLayout";
    private static  int HEADER_MAX_TOP_OFFSET =0;
    private  View rootScrollLayout;
    private View mScrollUpChild;
    private int curentTranslationY;

    public MySwipeRefreshLayout2(Context context) {
        this(context, null);
    }

    public MySwipeRefreshLayout2(Context context, AttributeSet attrs) {
        super(context, attrs);
        HEADER_MAX_TOP_OFFSET = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, context.getResources().getDisplayMetrics());
        curentTranslationY = 0;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        rootScrollLayout = findViewById(R.id.rootScrollLayout);
        Log.i(TAG, "onMeasure: getMeasuredHeight="+rootScrollLayout.getMeasuredHeight()+" getMeasuredWidth="+rootScrollLayout.getMeasuredWidth());
        Log.i(TAG, "onMeasure: parent.getMeasuredHeight="+getMeasuredHeight());
        int w = View.MeasureSpec.makeMeasureSpec(rootScrollLayout.getMeasuredWidth(),View.MeasureSpec.EXACTLY);
        int h = View.MeasureSpec.makeMeasureSpec(getMeasuredHeight()+HEADER_MAX_TOP_OFFSET,View.MeasureSpec.EXACTLY);
        rootScrollLayout.measure(w,h);//让rootScrollLayout的子view知道,重新测量让子view铺满rootScrollLayout
        //扩大rootScrollLayout在SwipeRefreshLayout中的显示区域
        rootScrollLayout.layout(rootScrollLayout.getLeft(), rootScrollLayout.getTop(), rootScrollLayout.getRight(), rootScrollLayout.getBottom() + HEADER_MAX_TOP_OFFSET);
        rootScrollLayout.setTranslationY(curentTranslationY);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //这里的measure没有效果
        /*rootScrollLayout = findViewById(R.id.rootScrollLayout);
        Log.i(TAG, "onMeasure: getMeasuredHeight="+rootScrollLayout.getMeasuredHeight()+" getMeasuredWidth="+rootScrollLayout.getMeasuredWidth());
        Log.i(TAG, "onMeasure: parent.getMeasuredHeight="+getMeasuredHeight());
        int w = View.MeasureSpec.makeMeasureSpec(rootScrollLayout.getMeasuredWidth(),View.MeasureSpec.EXACTLY);
        int h = View.MeasureSpec.makeMeasureSpec(getMeasuredHeight()+HEADER_MAX_TOP_OFFSET,View.MeasureSpec.EXACTLY);
        rootScrollLayout.measure(w,h);*/
    }

    @Override
    public boolean canChildScrollUp() {
        if (mScrollUpChild != null) {
            return ViewCompat.canScrollVertically(mScrollUpChild, -1);
        }
        return super.canChildScrollUp();
    }

    public void setScrollUpChild(View view) {
        mScrollUpChild = view;
    }


    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        Log.i(TAG, "onStartNestedScroll: child="+child);
        //接收垂直方向的滚动事件,SwipeRefreshLayout默认是,只要正在刷新,父控件就不接受嵌套事件,由scrollview自己内部消耗
        return isEnabled() &&  (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
//        return super.onStartNestedScroll(child, target, nestedScrollAxes);
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        if (dy > 0) {//向上滚动
            if (curentTranslationY<=0) {//可以整体先向上滚动
                int min = (int) Math.min(rootScrollLayout.getTranslationY()+ HEADER_MAX_TOP_OFFSET, dy);
                curentTranslationY = (int) (rootScrollLayout.getTranslationY() - min);
                rootScrollLayout.setTranslationY(curentTranslationY);
                consumed[1] = min;
            }
        }
        super.onNestedPreScroll(target, dx, dy, consumed);
        Log.i(TAG, "onNestedPreScroll: ");
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {

        int max = 0;
        if (dyConsumed <= 0 && dyUnconsumed < 0) {//向下滚动
            if (curentTranslationY<0) {//可以整体先向下滚动
                max = (int) Math.max(rootScrollLayout.getTranslationY(), dyUnconsumed);
                curentTranslationY = (int) (rootScrollLayout.getTranslationY() - max);
                rootScrollLayout.setTranslationY(curentTranslationY);
            }
            if (isRefreshing()) {//如果正在更新,则不显示让SwipeRefreshLayout处理,否则会二度下拉正在更新
                return;
            }
        }
        //如果还有多余的需要SwipeRefreshLayout 处理,比如再下拉显示正在更新
        super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed-max);
    }

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        Log.i(TAG, "drawChild: child=" + child);
        return super.drawChild(canvas, child, drawingTime);
    }
}

这里要注意onlayout方法中的代码,需要调整rootScrollLayout 的高度,防止向上整体滚动时,底部空白问题


device-2017-03-02-112321.png

如上图底部。

刚开始在onMeasure中重新测量,死活出不来,然后想起CoordinatorLayout.Behavior 中也有调整位置大小的代码,于是在onLayout中重新layout,扩大显示区域,但是必须也要measure一下,否则里面的子view无法完全填充。

附布局代码

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

    android:orientation="vertical"
    tools:context="com.tospur.exmind.testswiperefresh.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>

    <com.tospur.exmind.testswiperefresh.MySwipeRefreshLayout2
        android:id="@+id/mySwipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <LinearLayout
                android:background="#00f"
                android:id="@+id/rootScrollLayout"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <ImageView
                    android:id="@+id/mImgView"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:src="@mipmap/test"
                    />
                <LinearLayout
                    android:id="@+id/mFilterView"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:background="#f0f"
                    android:layout_height="72dp"></LinearLayout>
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:background="#ff0"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
            </LinearLayout>
    </com.tospur.exmind.testswiperefresh.MySwipeRefreshLayout2>

</LinearLayout>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容