SwipeRefreshLayout 、RecyclerView 与 CoordinatorLayout 嵌套刷新滑动冲突的解决办法

SwipeRefreshLayout 与 CoordinatorLayout 嵌套刷新

 1.设置   mSwipeRefreshLayout.setOnRefreshListener(this);

 2. 动态设置SwipeRefreshLayout的是否可以刷新 setEnable(boolean isEnable);

 3. 设置SwipRefreshLayout刷新图标的位置 setProgressViewOffset(true, -20, 100);

4. 监听 AppBarLayout Offset 变化,动态设置 SwipeRefreshLayout 是否可用

     appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (verticalOffset >= 0) {
                    mSwipeRefreshLayout.setEnabled(true);
                } else {
                    mSwipeRefreshLayout.setEnabled(false);
                }
            }
        });

重点是第四步可以解决滑动冲突的

SwipeRefreshLayout 与 CoordinatorLayout 嵌套刷新

 1.设置   mSwipeRefreshLayout.setOnRefreshListener(this);

 2. 动态设置SwipeRefreshLayout的是否可以刷新 setEnable(boolean isEnable);

 3. 设置SwipRefreshLayout刷新图标的位置 setProgressViewOffset(true, -20, 100);

4. 监听 AppBarLayout Offset 变化,动态设置 SwipeRefreshLayout 是否可用

     appBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (verticalOffset >= 0) {
                    mSwipeRefreshLayout.setEnabled(true);
                } else {
                    mSwipeRefreshLayout.setEnabled(false);
                }
            }
        });

重点是第四步可以解决滑动冲突的


--------------------- 
作者:hgyixf 
来源:CSDN 
原文:https://blog.csdn.net/hgyxfnnii/article/details/73104798 
版权声明:本文为博主原创文章,转载请附上博文链接!

以上是网上搜到的大部分解决方案!然而并不能解决,终极解决方案

SwipeRefreshLayout这个控件大家可能几百年前就已经在熟练使用了,相关的博客也多不胜数,方法也许不同,但实质都是一样的,写这个的目的也只是为了先把公众号和星球转起来。

SwipeRefreshLayout是Android自己支持库的下拉刷新控件,官方文档中提示,只有其包裹的孩子是RecyclerView、ListView、ScrollView等可滑动控件才能正常执行下拉刷新完整逻辑,显示下拉刷新图标以及回收图标。如果是非滑动控件,比如我们会常用到Material Design设计风格中的CoordinatorLayout控件AppBarLayout结合RecyclerView的使用,下拉刷新就会出现拦截问题导致无法滑动列表。

很多博客对SwipeRefreshLayout都介绍得很详细,包括源码的分析,以及分析并解决遇到各种问题。对于列表拦截冲突的解决方法,大致都是根据查看onInterceptTouchEvent方法里面的拦截机制,根据判断逻辑继承SwipeRefreshLayout类重写canChildScrollUp()来解决,其实有个更简单粗暴的方法,直接按照自己的想要的滑动逻辑来设置是否拦截就可以了,上代码

public class AdvanceSwipeRefreshLayout extends SwipeRefreshLayout {
    private OnPreInterceptTouchEventDelegate mOnPreInterceptTouchEventDelegate;

    public AdvanceSwipeRefreshLayout(Context context) {
super(context);
    }

public AdvanceSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
        mConfiguration = ViewConfiguration.get(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean disallowIntercept = false;
        if (mOnPreInterceptTouchEventDelegate != null)
            disallowIntercept = mOnPreInterceptTouchEventDelegate.shouldDisallowInterceptTouchEvent(ev);

        if (disallowIntercept) {
return false;
        }
return super.onInterceptTouchEvent(ev);
    }

public void setOnPreInterceptTouchEventDelegate(OnPreInterceptTouchEventDelegate listener) {
mOnPreInterceptTouchEventDelegate = listener;
    }

public interface OnPreInterceptTouchEventDelegate {
boolean shouldDisallowInterceptTouchEvent(MotionEvent ev);
    }
}


使用的时候根据自己的需要设置就可以了。

View appBarLayout = v_findView(mContentSection, R.id.appBarLayout);
mRefreshLayout.setOnPreInterceptTouchEventDelegate(ev -> appBarLayout.getTop() < 0);


是不是很快,如果有什么问题希望大家指正,谢谢!


--------------------- 
作者:Evan_Chun 
来源:CSDN 
原文:https://blog.csdn.net/Evan_huangchun/article/details/78872051 
版权声明:本文为博主原创文章,转载请附上博文链接!

@Subscribe
public void appBarOffChange(appBarOffChangeEvent event) {


    switch (event.type){

        case appBarOffChangeEvent.CHANGE:
            recyclerView.mSwipeRefreshLayout.setOnPreInterceptTouchEventDelegate(new VpSwipeRefreshLayout.OnPreInterceptTouchEventDelegate() {
                @Override
                public boolean shouldDisallowInterceptTouchEvent(MotionEvent ev) {
                    return false;
                }
            });
            Log.e("CHANGE","CHANGE");
           // recyclerView.mSwipeRefreshLayout.setVisibility(View.VISIBLE);
            break;
        case appBarOffChangeEvent.NOCHANGE:

            recyclerView.mSwipeRefreshLayout.setOnPreInterceptTouchEventDelegate(new VpSwipeRefreshLayout.OnPreInterceptTouchEventDelegate() {
                @Override
                public boolean shouldDisallowInterceptTouchEvent(MotionEvent ev) {
                    return true;
                }
            });
            Log.e("NOCHANGE","NOCHANGE");
         //   recyclerView.mSwipeRefreshLayout.setVisibility(View.GONE);
            break;
    }




}


     参考链接https://blog.csdn.net/Evan_huangchun/article/details/78872051

猜你喜欢

转载自blog.csdn.net/berlor/article/details/83444986