卡片滑动 ToolBar显示/隐藏

277 阅读1分钟

本文已参加「新人创作礼」活动,一起开启掘金创作之路。

image.png

红色卡片上滑出屏幕时,显示ToolBar姓名信息;
红色卡片下滑出ToolBar时,隐藏ToolBar

layout布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@color/background_color">


    <!--  1-滑动布局  -->
    <androidx.core.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <!--    2-    -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background_color"
            android:orientation="vertical">


            <!--     3-内容布局       -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/id_buttom_view"
                android:layout_marginTop="-57dip"
                android:background="@drawable/bg_person"
                android:orientation="vertical"
                android:paddingTop="20dip">


            </LinearLayout>

            <!--     3-卡片布局       -->
            <RelativeLayout
                android:id="@+id/id_buttom_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <!--        4-        -->
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignBottom="@+id/id_card_view_person_info"
                    android:layout_marginBottom="57dip"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/ic_group_50_copy" />

                <!--      4-用户信息      -->
                <androidx.cardview.widget.CardView
                    android:id="@+id/id_card_view_person_info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="100dp"
                    android:layout_marginRight="20dp"
                    app:cardCornerRadius="10dp"
                    app:cardElevation="5dp">


                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:adjustViewBounds="true"
                        android:background="@drawable/group_50"
                        android:scaleType="centerCrop" />


                </androidx.cardview.widget.CardView>
            </RelativeLayout>

        </RelativeLayout>


    </androidx.core.widget.NestedScrollView>


    <!--  1-顶部名字显示  -->
    <RelativeLayout
        android:id="@+id/relative_layout_user_name"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:visibility="invisible">


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_group_50_copy" />


        <TextView
            android:id="@+id/tv_tool_bar_user_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="12dp"
            android:ellipsize="end"
            android:gravity="center_horizontal"
            android:lines="1"
            android:text="userName"
            android:textSize="18dp"
            android:textStyle="bold" />


    </RelativeLayout>


</FrameLayout>

滑动监听

	//scrollView滑动监听
	viewBinding.scrollView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
		val distanceCardInfoView = viewBinding.idCardViewPersonInfo.top + viewBinding.idCardViewPersonInfo.measuredHeight
		if (viewBinding.relativeLayoutUserName.visibility == View.INVISIBLE) {
			//ToolBar不可见时
			if (scrollY >= distanceCardInfoView) {
				//滑动距离大于卡片高度
				viewBinding.relativeLayoutUserName.visibility = View.VISIBLE
			}
		} else {
			//ToolBar可见时
			val cardRect = Rect()
			val toolBarRect = Rect()
			viewBinding.idCardViewPersonInfo.getGlobalVisibleRect(cardRect)
			viewBinding.relativeLayoutUserName.getGlobalVisibleRect(toolBarRect)
			val cardBottom = cardRect.bottom
			val toolBarBottom = toolBarRect.bottom
			if (cardBottom > toolBarBottom) {
				viewBinding.relativeLayoutUserName.visibility = View.INVISIBLE
			}
		}

	}