CoordinatorLayout 实现顶部悬浮停靠布局 折叠布局


核心布局:
<com.xxx.Mycoor
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="40dip"
    android:layout_below="@+id/title"
    android:background="@color/white"
    android:orientation="vertical">
    <!-- AppBarLayout 布局 app:layout_behavior 解决滑动卡顿问题-->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:layout_behavior="com.xxx.util.FlingBehavior">

        <!-- title部分 app:layout_scrollFlags="scroll|exitUntilCollapsed" 这句是重点-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

           <!-- title内容随便写 -->
        </LinearLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="56dip"
            android:background="@android:color/white"
            app:tabGravity="fill"
            app:tabIndicatorColor="@android:color/holo_red_dark"
            app:tabIndicatorHeight="1dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@android:color/holo_red_dark"
            app:tabTextColor="#8e8e8e" />
    </android.support.design.widget.AppBarLayout>

    <com.xxx.ScrollViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dip"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:background="#F1F1F1" />

</com.xxx.Mycoor>

布局只要分两部分,一部分title,一部分是TabLayout的创建,按照提示完成即可;

工具类(重写CoordinatorLayout):

public class Mycoor extends CoordinatorLayout {

    public String[] strings = {"进行中", "待评价", "已完成", "待报修"};
    public Mycoor(Context context) { super(context); }
    public Mycoor(Context context, AttributeSet attrs) { super(context, attrs); }
    public Mycoor(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr); }
    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        //获取 appBarLayout
        AppBarLayout appBarLayout = (AppBarLayout) getChildAt(0); if (dyConsumed > 0) {
            //这种情况是当tablayout悬浮的状态 //获取 tableLayout
            TabLayout tableLayout = (TabLayout) appBarLayout.getChildAt(1);
            for (int i = 0; i < tableLayout.getTabCount(); i++) {
                TabLayout.Tab tab = tableLayout.getTabAt(i);
                tab.setText(i + "");
                tab.setIcon(R.drawable.ic_launcher);
            }
        }
        else if (dyConsumed == 0) {
            //这种情况是当title要出现在屏幕上的时候
            TabLayout tableLayout = (TabLayout) appBarLayout.getChildAt(1);
            for (int i = 0; i < tableLayout.getTabCount(); i++) {
                TabLayout.Tab tab = tableLayout.getTabAt(i);
                tab.setText(strings[i]); tab.setIcon(null);
            }
        }
    }
}

TabLayout 不在本文不介绍,没有自定义的改动等,只是普通的TabLayout,自行加入即可;


问题:可能会遇到滑动卡顿的情况,所谓滑动卡顿,也指没有惯性滑动的情况,解决办法:


AppBarLayout ,布局上,加入自定义app:layout_behavior="com.xxx.FlingBehavior"属性即可。

自定义FlingBehavior类:

public class FlingBehavior extends AppBarLayout.Behavior {

    private static final int TOP_CHILD_FLING_THRESHOLD = 3;
    private boolean isPositive;

    public FlingBehavior() { }

    public FlingBehavior(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    @Override
    public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed)
    {
        if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive)
        {
            velocityY = velocityY * -1;
        }
        if (target instanceof RecyclerView && velocityY < 0)
        {
            final RecyclerView recyclerView = (RecyclerView) target;
            final View firstChild = recyclerView.getChildAt(0);
            final int childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild); consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD;
        }
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed)
    {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        isPositive = dy > 0;
    }
}



但不是最好的方法时间有限,先研究到这里,如果后续有变动,会陆续加入文章之后。






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值