協(xié)調(diào)者布局CoordinatorLayout,我想大家都陌生吧,最近公司產(chǎn)品想抄某個(gè)APP的部分功能,經(jīng)判定,應(yīng)該采用協(xié)調(diào)者布局,大概的效果如下:

什么頭部展開折疊啊,滑動(dòng)懸浮置頂,這些都不是事,不難實(shí)現(xiàn)。
這里插個(gè)題外話:其實(shí),使用CoordinatorLayout,可以很容易地實(shí)現(xiàn)給RecyclerView加頭部,這也算是一種另類的加頭部方法吧,O(∩_∩)O~
回正題,這個(gè)需求比較糾結(jié)的地方在于,頭部折疊后鎖死,下面列表需要實(shí)現(xiàn)下拉刷新功能。直接上布局文件:
<?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>
布局文件其實(shí)沒什么好說的,layout_scrollFlags和layout_behavior這些玩過協(xié)調(diào)者布局的都知道,就不多說了,需要注意的是,我們定義了兩個(gè)距離,@dimen/appBarHeight和@dimen/tabHeight,因?yàn)橄胍獙?shí)現(xiàn)本次的需求,最終還是得通過監(jiān)聽距離的變化來實(shí)現(xiàn)的,直接上代碼:
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("協(xié)調(diào)者布局");
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);//禁止滑動(dòng)
child.setLayoutParams(layoutParams);
ViewGroup.LayoutParams appBarLP = appBarLayout.getLayoutParams();
appBarLP.height = tabHeight;//直接設(shè)置高度為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);//恢復(fù)滑動(dòng)
child.setLayoutParams(layoutParams);
ViewGroup.LayoutParams appBarLP = appBarLayout.getLayoutParams();
appBarLP.height = appBarHeight;//恢復(fù)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萬能適配器,這個(gè)超級(jí)好用,強(qiáng)烈推薦,項(xiàng)目地址:RecyclerView萬能適配器
用過ListView的同學(xué)都知道android.R.layout.simple_list_item_1這個(gè)布局,好吧,我承認(rèn)我也是懶得可以,但畢竟這不是今天重點(diǎn),方便快捷最重要。
說回重點(diǎn),需要注意的是,當(dāng)監(jiān)聽到頭部折疊之后,我們除了重新對(duì)頭部的高度及可見性進(jìn)行設(shè)置外,還需要禁止其滑動(dòng),否則會(huì)影響到最終的滑動(dòng)效果,同樣的,當(dāng)點(diǎn)擊“展開”按鈕后,需恢復(fù)其滑動(dòng)。