如果appBarLayout中有折叠控件 CollapsingToolbarLayout 或者其他一些控件 在滚动的时候进行判断和刷新事件处理
在这里:刷新框架:为
SwipyRefreshLayout
第一步:设置appBarLayout的监听:
if (appBarLayout != null) appBarLayout.addOnOffsetChangedListener(this);
@Override public void onOffsetChanged(AppBarLayout appBarLayout, int i) { super.onOffsetChanged(appBarLayout, i); if (srlLayout == null) return; srlLayout.setEnabled(i >= 0||isSlideToBottom(recyclerview) ? true : false); }
至于 isSlideToBottom方法请查看 reyclerView滚动到底部的监听
当recyclerView滚动到底部 也需要开启 刷新控件的触摸事件 enable=true;
屏蔽刷新控件的范围:当appBarLayout 在滚动中 recylerView 没有滚动到底部
这里还有一种实现方式:
@Override public void onOffsetChanged(AppBarLayout appBarLayout, int i) { super.onOffsetChanged(appBarLayout, i); LogUtils.d("-------->off:" + i + " ScrollRange:" + appBarLayout.getTotalScrollRange() + " height:" + appBarLayout.getHeight()); }
可以观察到i>=0 表示appBarLaoyut完全打开
i==-appBarLayout.getTotalScrollRange() 时表示appBarLayout完全关闭或者折叠了
所以:i==-appBarLayout.getTotalScrollRange() 也可以进行判断 然后分发给刷新控件
顺便给出一个工具类:
<pre name="code" class="java"> import android.support.design.widget.AppBarLayout; import android.support.v7.widget.RecyclerView; /** * @author xuanyouwu * @email [email protected] * @time 2016-04-13 16:25 */ public class DesignViewUtils { /** * AppBarLayout 完全显示 打开状态 * * @param verticalOffset * @return */ public static boolean isAppBarLayoutOpen(int verticalOffset) { return verticalOffset >= 0; } /** * AppBarLayout 关闭或折叠状态 * * @param appBarLayout * @param verticalOffset * @return */ public static boolean isAppBarLayoutClose(AppBarLayout appBarLayout, int verticalOffset) { return appBarLayout.getTotalScrollRange() == Math.abs(verticalOffset); } /** * RecyclerView 滚动到底部 最后一条完全显示 * * @param recyclerView * @return */ public static boolean isSlideToBottom(RecyclerView recyclerView) { if (recyclerView == null) return false; if (recyclerView.computeVerticalScrollExtent() + recyclerView.computeVerticalScrollOffset() >= recyclerView.computeVerticalScrollRange()) return true; return false; } /** * RecyclerView 滚动到顶端 * * @param recyclerView * @return */ public static boolean isSlideToTop(RecyclerView recyclerView) { return recyclerView.computeVerticalScrollOffset() <= 0; } }
完美解决刷新案例:
<pre name="code" class="java"> import android.support.design.widget.AppBarLayout; import android.support.v4.view.ViewCompat; import android.support.v7.widget.RecyclerView; import android.view.ViewGroup; import com.kekeclient.widget.design.DesignViewUtils; /** * @author xuanyouwu * @email [email protected] * @time 2016-04-15 11:20 * <p/> * 解决 嵌套nestScrolling appbar 刷新冲突 */ public class SwipyAppBarScrollListener extends RecyclerView.OnScrollListener implements AppBarLayout.OnOffsetChangedListener { private AppBarLayout appBarLayout; private RecyclerView recyclerView; private ViewGroup refreshLayout; private boolean isAppBarLayoutOpen = true; private boolean isAppBarLayoutClose; public SwipyAppBarScrollListener(AppBarLayout appBarLayout, ViewGroup refreshLayout, RecyclerView recyclerView) { this.appBarLayout = appBarLayout; this.refreshLayout = refreshLayout; this.recyclerView = recyclerView; disptachScrollRefresh(); } private void disptachScrollRefresh() { if (this.appBarLayout != null && this.recyclerView != null && refreshLayout != null) { this.appBarLayout.addOnOffsetChangedListener(this); this.recyclerView.addOnScrollListener(this); } } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); dispatchScroll(); } @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { isAppBarLayoutOpen = DesignViewUtils.isAppBarLayoutOpen(verticalOffset); isAppBarLayoutClose = DesignViewUtils.isAppBarLayoutClose(appBarLayout, verticalOffset); dispatchScroll(); } private void dispatchScroll() { if (this.recyclerView != null && this.appBarLayout != null && this.refreshLayout != null) { //不可滚动 if (!(ViewCompat.canScrollVertically(recyclerView, -1) || ViewCompat.canScrollVertically(recyclerView, 1))) { refreshLayout.setEnabled(isAppBarLayoutOpen); } else//可以滚动 { if (isAppBarLayoutOpen || isAppBarLayoutClose) { if (!ViewCompat.canScrollVertically(recyclerView, -1) && isAppBarLayoutOpen) { refreshLayout.setEnabled(true); } else if (isAppBarLayoutClose && !ViewCompat.canScrollVertically(recyclerView, 1)) { refreshLayout.setEnabled(true); } else { refreshLayout.setEnabled(false); } } else { refreshLayout.setEnabled(false); } } } } }
recyclerview.addOnScrollListener(new SwipyAppBarScrollListener(appBarLayout, srlLayout, recyclerview));