coodinatorlayout主要监听子控件子容器的滑动,比如:recyclerview、nestScrollview,ViewPager。这次尝试的是recyclerview的监听,但是有点坑爹的是behavior返回recyclerview滑动距离有点问题,这个应该是behavior的bug吧,之前尝试的scrollview是完美切合。
behavior 绑定中间和右边的recycleview,滑动左边的recycleview来联动中间和右边的两个recyclerview同时滑动
布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.minwenping.md_behavior2demo.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerA"
android:layout_width="120dp"
android:layout_height="320dp"
android:layout_gravity="left|top" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerB"
android:layout_width="120dp"
android:layout_height="320dp"
android:layout_gravity="center|top"
app:layout_behavior="com.example.minwenping.md_behavior2demo.MyBehavior" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerC"
android:layout_width="120dp"
android:layout_height="320dp"
android:layout_gravity="right|top"
app:layout_behavior="com.example.minwenping.md_behavior2demo.MyBehavior" />
</android.support.design.widget.CoordinatorLayout>
自定义behavior的代码如下:
public class MyBehavior extends CoordinatorLayout.Behavior<View> {
public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
static final String TAG = "Test";
/**
* 开始滑动的时候标记监听滑动方向
*
* @param coordinatorLayout 直接父容器协调者布局
* @param child 围观的子view
* @param directTargetChild
* @param target 被围观的孩子
* @param nestedScrollAxes 滑动的轴向
* @return
*/
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
/**
* 监听第一个recycleview的垂直滑动
* 方向肯定是个枚举常量,从源码查询如下
* 监听第一个recycleview的垂直滑动
* {@link ViewCompat#SCROLL_AXIS_HORIZONTAL},
* {@link ViewCompat#SCROLL_AXIS_VERTICAL}
*/
if (target.getId() == R.id.recyclerA) {
Log.i(TAG, "onNestedScroll: jiantingdao A");
}
if (child.getId() == R.id.recyclerB) {
Log.i(TAG, "onNestedScroll: jiantingdao B");
}
if (child.getId() == R.id.recyclerC) {
Log.i(TAG, "onNestedScroll: jiantingdao C");
}
boolean scrollAxes = nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
return scrollAxes || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}
int initTemp=0;
//滑动的时刻
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target,
int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
//target 被围观的孩子
RecyclerView target1 = (RecyclerView) target;
target1.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.i(TAG, "onScrolled: 滑动的距离11===="+dy);
}
});
Log.i(TAG, "onNestedPreScroll: 滑动的距离:"+target1.getScrollY()+"==="+dy);
LinearLayoutManager layoutManager = (LinearLayoutManager) target1.getLayoutManager();
int fvp = layoutManager.findFirstCompletelyVisibleItemPosition();
int delt=fvp-initTemp;
Log.i(TAG, "onNestedScroll:第一个位置 ="+fvp+"==delt"+delt);
LinearLayoutManager childLm = (LinearLayoutManager) ((RecyclerView) child).getLayoutManager();
int itemCount = ((RecyclerView) child).getAdapter().getItemCount();
if (initTemp == fvp) {
return;
}
switch (child.getId()) {
case R.id.recyclerB:
int chidFV1 = childLm.findFirstVisibleItemPosition();
int dest1=chidFV1;
if (chidFV1<fvp) {
dest1=chidFV1+fvp;
}
if (chidFV1>fvp){
dest1=chidFV1-fvp;
}
if (dest1>=0&&dest1<itemCount){
((RecyclerView) child).smoothScrollToPosition(dest1);
}
Log.i(TAG, "onNestedScroll: kaishihuaodng B");
break;
case R.id.recyclerC:
int chidFV2 = childLm.findFirstCompletelyVisibleItemPosition();
int dest2=0;
if (chidFV2<fvp) {
dest2=chidFV2+fvp;
}
if (chidFV2>fvp){
dest2=chidFV2-fvp;
}
if (dest2>=0&&dest2<itemCount){
((RecyclerView) child).smoothScrollToPosition(dest2);
}
Log.i(TAG, "onNestedScroll: kaishihuaodng C");
break;
}
initTemp=fvp;
}
//滑动松手惯性滑动
@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, View child,
View target, float velocityX, float velocityY, boolean consumed) {
return super.onNestedFling(coordinatorLayout, child, target,
velocityX, velocityY, consumed);
//TODO 滑动松手后处理
}
}