AppBarLayout+CollapsingToolbarLayout+TabLayout+ViewPager 组合使用

6 篇文章 0 订阅
前言:在项目中遇到需要一个需求是要用到AppBarLayout和CollapsingToolBarLayout实现上下滑动头部布局随之展开和收缩的效果,头布局下面 需要结合tablayout和viewPager和Fragment点击切换。Fragment中包含RecyclerView 或者NestScrollView。(这里是不带toolBar的效果)

大概就这样子吧

先贴我的布局吧:
<?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:background="@color/white"
    android:fitsSystemWindows="true"
    >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#7f000000"
        app:layout_behavior="包名.FlingBehavior"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:titleEnabled="false">



        <include
            layout="@layout/head_view"
          />


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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.design.widget.TabLayout

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:tabSelectedTextColor="@color/textColor_FF5722"
                app:tabTextColor="@color/black" />


        </LinearLayout>


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

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"   app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

在使用appBarLayout的时候需要注意添加一下属性

1.在CollapsingToolbarLayout中需要添加

app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false"//可选

2.在需要滑动的布局中添加


app:layout_behavior="@string/appbar_scrolling_view_behavior"

可能出现的问题

1.RecyclerView所在的Fragment可能存在 RV向下滑动的时候 头布局滑动不流畅,这样的问题也存在于NestScrollView向上滑动的时候。

就上面提及的问题解决方法:

1.RecyclerView

问题应该是 appBarLayout没有获得fling指令导致卡顿,应该是recyclerView在分发fling的时候出现了错误。
在StackOverFlow上有人提出了解决办法:
自定义Behavior

copy代码—>在布局文件中添加属性app:layout_behavior="包名.Behavior名称"

2.NestScrollView

自定义一个NestScrollView。
将NestScrollView中的代码全部copy—->并在onTouchEvent()方法的case MotionEvent.ACTION_UP: 中去掉if (mIsBeingDragged) 判断。

原因:`case MotionEvent.ACTION_UP:

            if (mIsBeingDragged) {
                final VelocityTracker velocityTracker = mVelocityTracker;

                velocityTracker.addMovement(vtev);

                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker,
                        mActivePointerId);
                if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                    flingWithNestedDispatch(-initialVelocity);
                } else if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0,
                        getScrollRange())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
            mActivePointerId = INVALID_POINTER;
            endDrag();
            break;`

触发up事件的时候会来处理fling事件。flingWithNestedDispatch(-initialVelocity) 用来发送fling事件和处理fling事件。出现卡顿的时候就是if (mIsBeingDragged) 这个判断为false的时候。

总结:很多时候遇到问题记下来印象会深很多,虽然很多也都是照着其他的博客copy的。具体看参考链接。

参考:

https://stackoverflow.com/questions/30923889/flinging-with-recyclerview-appbarlayout

http://blog.csdn.net/bigggfish/article/details/53585783

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值