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; }}
但不是最好的方法时间有限,先研究到这里,如果后续有变动,会陆续加入文章之后。
阅读全文
0 0
- CoordinatorLayout 实现顶部悬浮停靠布局 折叠布局
- CoordinatorLayout协调布局,实现悬浮导航条
- 复杂布局置顶悬浮,CoordinatorLayout
- CoordinatorLayout:CollapsingToolbarLayout折叠效果的布局容器
- RecyclerView实现带有头部的顶部悬浮置顶布局
- iOS实现上滑停靠顶部悬浮框
- android 顶部悬停折叠CoordinatorLayout,AppBarLayout
- CoordinatorLayout协调布局
- CoordinatorLayout,协调者布局
- CoordinatorLayout布局出错
- 协调者布局:CoordinatorLayout
- CoordinatorLayout,协调者布局
- Android 粘性布局CoordinatorLayout
- android-RelativeLayout实现顶部、中部、底部布局
- Qt 布局管理 - 停靠窗口
- Qt 布局管理 - 停靠窗口
- CollapsingToolbarLayout配合CoordinatorLayout、viewpage实现伸缩式布局
- Android Design Library(二)使用CoordinatorLayout布局实现联动效果
- 源码解析之路--spring解析(一)
- Android5.0 按键kl文件加载过程分析
- 阿里-应届生面试求职基础题
- C# 学习——静态(第四天)
- 韦东山第10课-内核启动
- CoordinatorLayout 实现顶部悬浮停靠布局 折叠布局
- NPM常用命令
- 百度地图如何实现图层控制
- ElasticSearch
- 每天一个linux命令:find
- 虚函数相关讨论
- android之Fragment中监听back回退事件
- Android编程实例
- Spring Aop实现