如果你用 CoordinatorLayout 嵌套AppBarLayout RecyclerView,你会发现一个问题,滑动会有卡顿,关键是,你如果一直用手指触碰屏幕滑动的话不会感觉什么。而卡顿是在产生惯性的时候。我们都知道现在的android滑动组件很牛逼,都有惯性的效果,你比如说RecyclerView,你滑动一定速度之后你放开手,它还会以减速的方式滑动一段距离。而这个demo不顺畅的地方就是在放开手之后的惯性上。别人也有遇到过这个问题
一 :解决CoordinatorLayout滑动卡顿
1. 大屌推荐之自定义Behavior 方法(这是我的布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:background="@null"
app:elevation="@dimen/dp_0"
android:id="@+id/app_bar"
app:layout_behavior=".utils.basic.CustomBehavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="@+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll">
<LinearLayout
android:id="@+id/day_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/data_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_19"
android:text="06/18"
android:textColor="@color/white"
android:textSize="@dimen/dp_17">
</TextView>
<TextView
android:id="@+id/day_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_9"
android:background="@mipmap/day_backgroup"
android:paddingLeft="@dimen/dp_15"
android:paddingTop="@dimen/dp_6"
android:text="每日精选歌曲"
android:textColor="@color/white"
android:textSize="@dimen/dp_13">
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_9"
android:src="@mipmap/right_image">
</ImageView>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/re_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/day_music"
android:layout_marginLeft="@dimen/dp_19"
android:layout_marginTop="@dimen/dp_12">
</android.support.v7.widget.RecyclerView>
<TextView
android:id="@+id/nusic_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/re_day"
android:layout_marginLeft="@dimen/dp_20"
android:layout_marginTop="@dimen/dp_30"
android:text="歌曲分类"
android:textColor="@color/white"
android:textSize="@dimen/dp_17">
</TextView>
<android.support.v7.widget.RecyclerView
android:id="@+id/re_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nusic_class"
android:layout_marginLeft="@dimen/dp_19"
android:layout_marginTop="@dimen/dp_12">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabSelectedTextColor="#FFFFFF"
app:tabTextColor="@color/black40" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
二:我的工具包 utils CustomBehavior
public class CustomBehavior extends AppBarLayout.Behavior {
private static final String TAG = "AppbarLayoutBehavior";
private static final int TYPE_FLING = 1;
/**
* 记录是否有fling
*/
private boolean isFlinging;
/**
* 记录是否
*/
private boolean shouldBlockNestedScroll;
public CustomBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 是否拦截触摸事件
* @param parent CoordinatorLayout
* @param child AppBarLayout
* @param ev ev
* @return
*/
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
LogUtil.d(TAG, "onInterceptTouchEvent:" + child.getTotalScrollRange());
shouldBlockNestedScroll = isFlinging;
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
//手指触摸屏幕的时候停止fling事件
stopAppbarLayoutFling(child);
break;
default:
break;
}
return super.onInterceptTouchEvent(parent, child, ev);
}
/**
* 反射获取私有的flingRunnable 属性,考虑support 28以后变量名修改的问题
* @return Field
* @throws NoSuchFieldException
*/
private Field getFlingRunnableField() throws NoSuchFieldException {
Class<?> superclass = this.getClass().getSuperclass();
try {
// support design 27及一下版本
Class<?> headerBehaviorType = null;
if (superclass != null) {
String name = superclass.getName();
LogUtil.d("AppBarLayout.Behavior父类",name);
headerBehaviorType = superclass.getSuperclass();
}
if (headerBehaviorType != null) {
String name = headerBehaviorType.getName();
LogUtil.d("AppBarLayout.Behavior父类的父类1",name);
return headerBehaviorType.getDeclaredField("mFlingRunnable");
}else {
return null;
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
// 可能是28及以上版本
Class<?> headerBehaviorType = superclass.getSuperclass().getSuperclass();
if (headerBehaviorType != null) {
String name = headerBehaviorType.getName();
LogUtil.d("AppBarLayout.Behavior父类的父类2",name);
return headerBehaviorType.getDeclaredField("flingRunnable");
} else {
return null;
}
}
}
/**
* 反射获取私有的scroller 属性,考虑support 28以后变量名修改的问题
* @return Field
* @throws NoSuchFieldException
*/
private Field getScrollerField() throws NoSuchFieldException {
Class<?> superclass = this.getClass().getSuperclass();
try {
// support design 27及一下版本
Class<?> headerBehaviorType = null;
if (superclass != null) {
headerBehaviorType = superclass.getSuperclass();
}
if (headerBehaviorType != null) {
return headerBehaviorType.getDeclaredField("mScroller");
}else {
return null;
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
// 可能是28及以上版本
Class<?> headerBehaviorType = superclass.getSuperclass().getSuperclass();
if (headerBehaviorType != null) {
return headerBehaviorType.getDeclaredField("scroller");
}else {
return null;
}
}
}
/**
* 停止appbarLayout的fling事件
* @param appBarLayout
*/
private void stopAppbarLayoutFling(AppBarLayout appBarLayout) {
//通过反射拿到HeaderBehavior中的flingRunnable变量
try {
Field flingRunnableField = getFlingRunnableField();
Field scrollerField = getScrollerField();
if (flingRunnableField != null) {
flingRunnableField.setAccessible(true);
}
if (scrollerField != null) {
scrollerField.setAccessible(true);
}
Runnable flingRunnable = null;
if (flingRunnableField != null) {
flingRunnable = (Runnable) flingRunnableField.get(this);
}
OverScroller overScroller = null;
if (scrollerField != null) {
overScroller = (OverScroller) scrollerField.get(this);
}
//下面是关键点
if (flingRunnable != null) {
LogUtil.d(TAG, "存在flingRunnable");
appBarLayout.removeCallbacks(flingRunnable);
flingRunnableField.set(this, null);
}
if (overScroller != null && !overScroller.isFinished()) {
overScroller.abortAnimation();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 嵌套滑动开始(ACTION_DOWN),确定Behavior是否要监听此次事件
*/
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child,
View directTargetChild, View target,
int nestedScrollAxes, int type) {
LogUtil.d(TAG, "onStartNestedScroll");
stopAppbarLayoutFling(child);
return super.onStartNestedScroll(parent, child, directTargetChild, target,
nestedScrollAxes, type);
}
/**
* 嵌套滑动进行中,要监听的子 View将要滑动,滑动事件即将被消费(但最终被谁消费,可以通过代码控制)
*/
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout,
AppBarLayout child, View target,
int dx, int dy, int[] consumed, int type) {
LogUtil.d(TAG, "onNestedPreScroll:" + child.getTotalScrollRange()
+ " ,dx:" + dx + " ,dy:" + dy + " ,type:" + type);
//type返回1时,表示当前target处于非touch的滑动,
//该bug的引起是因为appbar在滑动时,CoordinatorLayout内的实现NestedScrollingChild2接口的滑动
//子类还未结束其自身的fling
//所以这里监听子类的非touch时的滑动,然后block掉滑动事件传递给AppBarLayout
if (type == TYPE_FLING) {
isFlinging = true;
}
if (!shouldBlockNestedScroll) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
}
}
/**
* 嵌套滑动进行中,要监听的子 View的滑动事件已经被消费
*/
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
View target, int dxConsumed, int dyConsumed, int
dxUnconsumed, int dyUnconsumed, int type) {
LogUtil.d(TAG, "onNestedScroll: target:" + target.getClass() + " ,"
+ child.getTotalScrollRange() + " ,dxConsumed:"
+ dxConsumed + " ,dyConsumed:" + dyConsumed + " " + ",type:" + type);
if (!shouldBlockNestedScroll) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,
dyConsumed, dxUnconsumed, dyUnconsumed, type);
}
}
/**
* 嵌套滑动结束(ACTION_UP或ACTION_CANCEL)
*/
@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl,
View target, int type) {
LogUtil.d(TAG, "onStopNestedScroll");
super.onStopNestedScroll(coordinatorLayout, abl, target, type);
isFlinging = false;
shouldBlockNestedScroll = false;
}
private static class LogUtil{
static void d(String tag, String string){
Log.d(tag,string);
}
}
}
欧克 这样就可以解决这个问题 了
网友评论