詳解Android使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou實(shí)現(xiàn)手指滑動(dòng)效果
CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou實(shí)現(xiàn)手指滑動(dòng)效果
如何使用 CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou實(shí)現(xiàn)下面GIF圖中的效果,再展開的時(shí)候頭像處于紅白中間,根據(jù)收縮程度改變頭像的位置!底下的RecyclerView也跟隨這個(gè)移動(dòng),不會(huì)出現(xiàn)中間隔出一段距離!(僅提供源碼復(fù)制粘貼,很簡(jiǎn)單的)
先看下效果圖:

下面上代碼
XML布局代碼如下:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/Fragment_ontstf_CoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false">
<android.support.design.widget.AppBarLayout
android:id="@+id/Fragment_ontstf_AppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cFF3523"
android:clipChildren="false"
app:elevation="0dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/Fragment_ontstf_CollapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:id="@+id/Fragment_ontstf_RelativeLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.25">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"
android:text="331"
android:textColor="@color/c333333"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="積分"
android:textColor="@color/c333333"
android:textSize="10sp" />
</LinearLayout>
<View
android:layout_width="0.7px"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:background="#727272"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"
android:text="5"
android:textColor="@color/c333333"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="優(yōu)惠卷"
android:textColor="@color/c333333"
android:textSize="10sp" />
</LinearLayout>
<View
android:layout_width="0.7px"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:background="#727272"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"
android:text="0.00"
android:textColor="@color/c333333"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="代金卷"
android:textColor="@color/c333333"
android:textSize="10sp" />
</LinearLayout>
<View
android:layout_width="0.7px"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:background="#727272"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal|bottom"
android:text="3314"
android:textColor="@color/c333333"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="淘幣"
android:textColor="@color/c333333"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/cFF3523"
android:gravity="center_vertical"
app:layout_collapseMode="pin">
<ImageView
android:id="@+id/Fragment_ontstf_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/Fragment_ontstf_message"
android:src="@mipmap/set" />
<ImageView
android:id="@+id/Fragment_ontstf_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="@mipmap/message" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/Fragment_ontstf_Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetStart="0dp"
app:layout_collapseMode="pin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/Fragment_ontstf_portrait"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center_vertical"
android:src="@mipmap/ic_launcher_round" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/Fragment_ontstf_name"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<TextView
android:id="@+id/Fragment_ontstf_introduce"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginBottom="5dp"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/Fragment_ontstf_RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/c00000000"
app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
JAVA代碼如下:
/**
* 獲取控件信息
*/
private void initView() {
FragmentOntstfAppBar = (AppBarLayout) view.findViewById(R.id.Fragment_ontstf_AppBar);
FragmentOntstfToolbar = (Toolbar) view.findViewById(R.id.Fragment_ontstf_Toolbar);
FragmentOntstfPortrait = (CircleImageView) view.findViewById(R.id.Fragment_ontstf_portrait);
FragmentOntstfName = (TextView) view.findViewById(R.id.Fragment_ontstf_name);
FragmentOntstfIntroduce = (TextView) view.findViewById(R.id.Fragment_ontstf_introduce);
FragmentOntstfCollapsingToolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.Fragment_ontstf_CollapsingToolbarLayout);
FragmentOntstfSet = (ImageView) view.findViewById(R.id.Fragment_ontstf_set);
FragmentOntstfMessage = (ImageView) view.findViewById(R.id.Fragment_ontstf_message);
FragmentOntstfRecyclerView = (RecyclerView) view.findViewById(R.id.Fragment_ontstf_RecyclerView);
FragmentOntstfCoordinatorLayout = (CoordinatorLayout) view.findViewById(R.id.Fragment_ontstf_CoordinatorLayout);
FragmentOntstfRelativeLayout = (RelativeLayout) view.findViewById(R.id.Fragment_ontstf_RelativeLayout);
setFragmentOntstfRecyclerView();
AppBar();
mPresenter.PselectUser(Userid);
}
/**
* 最主要的代碼
* AppBar滑動(dòng)效果
*/
private void AppBar() {
FragmentOntstfAppBar.setExpanded(true);
FragmentOntstfAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
float alpha = (float) Math.abs(i) / appBarLayout.getTotalScrollRange();
if (alpha == 0) {
CollapsingToolbarLayout.LayoutParams layoutParams = new CollapsingToolbarLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
FragmentOntstfRecyclerView.setPadding(0,FragmentOntstfToolbar.getHeight() / 2,0,0);
layoutParams.setMargins(0, 0, 0, (-FragmentOntstfToolbar.getHeight() / 2));
layoutParams.gravity = Gravity.BOTTOM;
FragmentOntstfToolbar.setLayoutParams(layoutParams);
FragmentOntstfCoordinatorLayout.setClipChildren(false);
} else if (FragmentOntstfRelativeLayout.getHeight() - Math.abs(i * 1.0f) == FragmentOntstfToolbar.getHeight()) {
FragmentOntstfCoordinatorLayout.setClipChildren(true);
} else {
int a = (int) ((FragmentOntstfToolbar.getHeight() / 2) * alpha);
CollapsingToolbarLayout.LayoutParams layoutParams = new CollapsingToolbarLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
FragmentOntstfRecyclerView.setPadding(0,FragmentOntstfToolbar.getHeight() / 2 - a,0,0);
layoutParams.setMargins(0, 0, 0, -(FragmentOntstfToolbar.getHeight() / 2) - (-a));
layoutParams.gravity = Gravity.BOTTOM;
FragmentOntstfToolbar.setLayoutParams(layoutParams);
FragmentOntstfCoordinatorLayout.setClipChildren(false);
}
}
});
}
這樣就完成了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Android使用CoordinatorLayout+AppBarLayout實(shí)現(xiàn)拉伸頂部圖片功能
- Android進(jìn)階NestedScroll嵌套滑動(dòng)機(jī)制實(shí)現(xiàn)吸頂效果詳解
- Android RecyclerView實(shí)現(xiàn)吸頂動(dòng)態(tài)效果流程分析
- Android itemDecoration接口實(shí)現(xiàn)吸頂懸浮標(biāo)題
- Android Jetpack Compose實(shí)現(xiàn)列表吸頂效果
- Android實(shí)現(xiàn)上拉吸頂效果
- Android進(jìn)階CoordinatorLayout協(xié)調(diào)者布局實(shí)現(xiàn)吸頂效果
相關(guān)文章
android利用handler實(shí)現(xiàn)打地鼠游戲
這篇文章主要為大家詳細(xì)介紹了android利用handler實(shí)現(xiàn)打地鼠游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
Android7.0中關(guān)于ContentProvider組件詳解
本文描述了Android7.0中關(guān)于ContentProvider組件實(shí)現(xiàn)原理以及ContentProvider發(fā)布者和調(diào)用者這兩在Framework層是如何實(shí)現(xiàn)的。2017-11-11
Android編程實(shí)現(xiàn)ListView滾動(dòng)提示等待框功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)ListView滾動(dòng)提示等待框功能,結(jié)合實(shí)例形式分析了Android ListView滾動(dòng)事件相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02
Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view的示例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
ViewPager滑動(dòng)靈敏度調(diào)整的方法實(shí)力
這篇文章主要介紹了ViewPager滑動(dòng)靈敏度調(diào)整的方法實(shí)力,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
如何在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動(dòng)效果
本篇文章是對(duì)在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動(dòng)效果進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
編寫簡(jiǎn)易Android天氣應(yīng)用的代碼示例
這篇文章主要介紹了編寫簡(jiǎn)易Android天氣應(yīng)用的代碼示例,文中的例子主要是利用到了RxAndroid處理異步方法,需要的朋友可以參考下2016-02-02

