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

来源:互联网 发布:windows电脑装mac系统 编辑:程序博客网 时间:2024/06/10 17:27

核心布局:
<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;    }}



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






原创粉丝点击