AppBarLayout与RecycleView滑动卡顿问题

重定义AppBarLayout.Behavior

public class AppBarLayoutBehavior extends AppBarLayout.Behavior {
    float mOrignBottomOfAppBarLayout;
    private AppBarLayout mAppBarLayout;
    AppBarLayoutScrollListem appBarLayoutScrollListem ;
    public AppBarLayoutBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        appBarLayoutScrollListem = new AppBarLayoutScrollListem();
    }

    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, AppBarLayout child, int layoutDirection) {
        boolean handled = super.onLayoutChild(parent, child, layoutDirection);
        if(mAppBarLayout == null) {
            mAppBarLayout = child;
            mOrignBottomOfAppBarLayout = child.getBottom();
        }
        return handled;
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes, int type) {
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;//表示滚动方向是垂直的才触发nest scroll。
    }


    @Override
    public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY) {
        RecyclerView recyclerView = target.findViewWithTag("recyclerView");
        if (recyclerView != null) {
            recyclerView.removeOnScrollListener(appBarLayoutScrollListem);
            recyclerView.addOnScrollListener(appBarLayoutScrollListem);
            appBarLayoutScrollListem.child = child;
            appBarLayoutScrollListem.target = target;
        }
        return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
    }

    public class AppBarLayoutScrollListem extends RecyclerView.OnScrollListener {
        AppBarLayout child;
        View target;
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                if (child != null && target != null) {
                    RecyclerView recyclerView1 = target.findViewWithTag("recyclerView");
                    if (recyclerView1 != null) {
                        if (!recyclerView1.canScrollVertically(-1)) {
                            child.setExpanded(true);
                        }
                        recyclerView1.removeOnScrollListener(this);
                    }
                }

            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
        }

    }

}

xml文件如下:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayoutID"
        android:layout_width="match_parent"
        android:layout_height="@dimen/x90"
        app:layout_behavior="xxx.AppBarLayoutBehavior">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="50dip"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <...>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20px"
            android:layout_marginRight="20px"
            android:tag="recyclerView"/>


</android.support.design.widget.CoordinatorLayout>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值