协调者布局CoordinatorLayout应用(3)
前面我们在协调者布局应用(2)中实现了类似地图的功能,那是在参考协调者布局应用(1)的基础上完成的,需求是实现了,但总感觉有点差强人意,难道就没的实现方案了吗?
答案当然不是,要知道协调者布局还是很强悍的,还记得前两篇在协调者布局中,我们都加入了AppBarLayout,并对其滑动进行监听来实现相应功能的,同时,设置下面列表页的滑动动作为:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
那么,问题来了,系统只提供这一种behavior吗?当然不是!
我们可以点击上面这个behavior进去看看,发现其下面还有另外一种behavior:
app:layout_behavior="@string/bottom_sheet_behavior"
没错,我们今天就是用这种behavior来实现上一篇类似地图的功能,还是直接看看效果图:
协调者布局应用
好像看起来跟上一篇的效果差不多,当然差不多咯,不然还玩什么?但其表现还是有一些差别的,下面我们会细说,老规矩,直接上布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".ui.activity.demo.MyCoordinatorActivity3">
<include layout="@layout/layout_base_item_header1"/>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/headLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/color_green"/>
<TextView
android:id="@+id/openTV"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="@dimen/tabHeight"
android:gravity="center"
android:background="@color/white"
android:textSize="16sp"
android:layout_gravity="bottom"
android:textColor="@color/THEME_BLUE"
android:text="点击展开"/>
</LinearLayout>
<LinearLayout
android:id="@+id/bottomLL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_peekHeight="350dp"
app:behavior_hideable="true">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/tabHeight"
android:background="@color/color_white"
app:tabSelectedTextColor="@color/THEME_BLUE">
</android.support.design.widget.TabLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
很明显,与前两篇相比,我们取消了AppBarLayout,更改了layout_behavior,我们再来看看代码的实现:
public class MyCoordinatorActivity3 extends BaseActivity {
@BindView(R.id.openTV)
TextView openTV;
@BindView(R.id.tabLayout)
TabLayout tabLayout;
@BindView(R.id.recyclerview)
RecyclerView recyclerview;
@BindView(R.id.bottomLL)
LinearLayout bottomLL;
private BottomSheetBehavior bottomSheetBehavior;
@Override
protected int attachLayoutRes() {
return R.layout.activity_my_coordinator3;
}
@Override
protected void initViews() {
setPageTitle("协调者布局");
setBackIV();
List<String> stringList = new ArrayList<>();
for (int i = 0; i < 32; i++) {
if (i <= 8) {
tabLayout.addTab(tabLayout.newTab());
tabLayout.getTabAt(i).setText("tab" + i);
}
stringList.add(String.valueOf(i));
}
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(linearLayoutManager);
recyclerview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
MyDemoAdapter myDemoAdapter = new MyDemoAdapter(android.R.layout.simple_list_item_1, stringList);
recyclerview.setAdapter(myDemoAdapter);
bottomSheetBehavior = BottomSheetBehavior.from(bottomLL);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
//如果当前处于隐藏状态,则显示按钮
openTV.setVisibility(View.VISIBLE);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}
@Override
protected void updateViews(boolean isRefresh) {
}
@OnClick(R.id.openTV)
public void onViewClicked() {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
哇,相同的功能,与第二篇相比,代码少了不少哦!
其实,这得益于BottomSheetBehavior,它本来就是用来干这个的,而且其定义了下面5种状态,使其使用起来更加简单方便:
/**
* The bottom sheet is dragging.
*/
public static final int STATE_DRAGGING = 1;
/**
* The bottom sheet is settling.
*/
public static final int STATE_SETTLING = 2;
/**
* The bottom sheet is expanded.
*/
public static final int STATE_EXPANDED = 3;
/**
* The bottom sheet is collapsed.
*/
public static final int STATE_COLLAPSED = 4;
/**
* The bottom sheet is hidden.
*/
public static final int STATE_HIDDEN = 5;
也正因为有了这些状态,所以上面说到的差别就在于:下面的列表部分不能随意停放在任意位置,这一点与前一篇的实现方案不同(前一篇的效果图中特意做了这一点演示),当然,笔者认为这样有状态的更符合实际的需求,所以推荐此方案!
最后,说到BottomSheetBehavior,我们再扩展一下:BottomSheetDialogFragment,看这名字猜都猜得出来,它继承于DialogFragment,可以用来做底部弹窗的,应用起来也非常简单,这里就不多说了,所以,以后在考虑底部弹窗时,又多了一种实现的方案。