自定义控件

改造SwipeRefreshLayout,实现折叠效果

2017-03-02  本文已影响220人  浩运
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>

上一篇下一篇

猜你喜欢

热点阅读