协调者布局CoordinatorLayout,我想大家都陌生吧,最近公司产品想抄某个APP的部分功能,经判定,应该采用协调者布局,大概的效果如下:
什么头部展开折叠啊,滑动悬浮置顶,这些都不是事,不难实现。
这里插个题外话:其实,使用CoordinatorLayout,可以很容易地实现给RecyclerView加头部,这也算是一种另类的加头部方法吧,O(∩_∩)O~
回正题,这个需求比较纠结的地方在于,头部折叠后锁死,下面列表需要实现下拉刷新功能。直接上布局文件:
<?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.MyCoordinatorActivity">
<include layout="@layout/layout_base_item_header1"/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/appBarHeight"
app:contentScrim="@color/colorPrimary">
<LinearLayout
android:id="@+id/headLayout"
app:layout_scrollFlags="scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_orange"/>
</LinearLayout>
<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.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
布局文件其实没什么好说的,layout_scrollFlags和layout_behavior这些玩过协调者布局的都知道,就不多说了,需要注意的是,我们定义了两个距离,@dimen/appBarHeight和@dimen/tabHeight,因为想要实现本次的需求,最终还是得通过监听距离的变化来实现的,直接上代码:
public class MyCoordinatorActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
@BindView(R.id.tabLayout)
TabLayout tabLayout;
@BindView(R.id.recyclerview)
RecyclerView recyclerview;
@BindView(R.id.appBarLayout)
AppBarLayout appBarLayout;
@BindView(R.id.btn1_header1_save)
Button openBT;
@BindView(R.id.refreshLayout)
SwipeRefreshLayout refreshLayout;
@BindView(R.id.headLayout)
LinearLayout headLayout;
private int tabHeight;
private int appBarHeight;
private int slideDistance;
private static final int scrollFlag0 = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL;
private static final int scrollFlag1 = AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED;
@Override
protected int attachLayoutRes() {
return R.layout.activity_my_coordinator;
}
@Override
protected void initViews() {
setPageTitle("协调者布局");
setBackIV();
openBT.setText("展开");
refreshLayout.setColorSchemeColors(getResources().getColor(R.color.THEME_BLUE));
refreshLayout.setOnRefreshListener(this);
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);
tabHeight = getResources().getDimensionPixelSize(R.dimen.tabHeight);
appBarHeight = getResources().getDimensionPixelOffset(R.dimen.appBarHeight);
slideDistance = appBarHeight - tabHeight;
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (-1 * slideDistance == verticalOffset) {
View child = appBarLayout.getChildAt(0);
AppBarLayout.LayoutParams layoutParams = (AppBarLayout.LayoutParams) child.getLayoutParams();
layoutParams.setScrollFlags(0);//禁止滑动
child.setLayoutParams(layoutParams);
ViewGroup.LayoutParams appBarLP = appBarLayout.getLayoutParams();
appBarLP.height = tabHeight;//直接设置高度为tabLayout的高度
appBarLayout.setLayoutParams(appBarLP);
headLayout.setVisibility(View.GONE);
openBT.setVisibility(View.VISIBLE);
}
}
});
}
@Override
protected void updateViews(boolean isRefresh) {
}
@OnClick(R.id.btn1_header1_save)
public void onViewClicked() {
View child = appBarLayout.getChildAt(0);
AppBarLayout.LayoutParams layoutParams = (AppBarLayout.LayoutParams) child.getLayoutParams();
layoutParams.setScrollFlags(scrollFlag0 | scrollFlag1);//恢复滑动
child.setLayoutParams(layoutParams);
ViewGroup.LayoutParams appBarLP = appBarLayout.getLayoutParams();
appBarLP.height = appBarHeight;//恢复appBar的高度
appBarLayout.setLayoutParams(appBarLP);
headLayout.setVisibility(View.VISIBLE);
openBT.setVisibility(View.GONE);
}
@Override
public void onRefresh() {
Global.getHandler().postDelayed(new Runnable() {
@Override
public void run() {
refreshLayout.setRefreshing(false);
Global.showToast("刷新成功!");
}
}, 2000);
}
}
其中,我们使用了RecyclerView万能适配器,这个超级好用,强烈推荐,项目地址:RecyclerView万能适配器
用过ListView的同学都知道android.R.layout.simple_list_item_1这个布局,好吧,我承认我也是懒得可以,但毕竟这不是今天重点,方便快捷最重要。
说回重点,需要注意的是,当监听到头部折叠之后,我们除了重新对头部的高度及可见性进行设置外,还需要禁止其滑动,否则会影响到最终的滑动效果,同样的,当点击“展开”按钮后,需恢复其滑动。