CoordinatorLayout+AppBarLayout+C
2020-08-19 本文已影响0人
hao_developer
布局
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/abLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="@dimen/dp_0"
app:layout_behavior=".view.AppBarLayoutBehavior">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white_fc"
app:elevation="@dimen/dp_0"
app:expandedTitleGravity="center"
app:titleEnabled="false"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<View
android:id="@+id/bannerBgView"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_70"
android:background="@color/zong_db" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.dsy.jxih.view.banner.MZBannerView
android:id="@+id/bannerView"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_133"
android:layout_centerInParent="true"
android:layout_marginTop="@dimen/dp_7"
android:scaleType="centerCrop"
app:canLoop="true"
app:layout_constraintTop_toTopOf="parent"
app:open_mz_mode="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvMenuList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_12"
android:layout_marginBottom="@dimen/dp_16"
android:scrollbars="none" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_150"
android:layout_marginLeft="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_10"
android:layout_marginBottom="@dimen/dp_16"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="@dimen/dp_0"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/dp_2_5"
android:layout_weight="1"
app:cardCornerRadius="@dimen/dp_5"
app:cardElevation="@dimen/dp_0">
<ImageView
android:id="@+id/ivLeft"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@mipmap/ic_image_2" />
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="@dimen/dp_0"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp_2_5"
android:layout_weight="1"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:layout_marginBottom="@dimen/dp_2_5"
android:layout_weight="1"
app:cardCornerRadius="@dimen/dp_5"
app:cardElevation="@dimen/dp_0">
<ImageView
android:id="@+id/ivTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/ic_image_7" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:layout_marginTop="@dimen/dp_2_5"
android:layout_weight="1"
app:cardCornerRadius="@dimen/dp_5"
app:cardElevation="@dimen/dp_0">
<ImageView
android:id="@+id/ivBottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/ic_image_7" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLay"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_35"
android:background="@color/white_fc"
android:elevation="@dimen/dp_0"
app:tabBackground="@android:color/transparent"
app:tabIndicator="@drawable/indicator"
app:tabIndicatorFullWidth="false"
app:tabMode="scrollable"
app:tabPadding="-1dp"
app:tabRippleColor="@android:color/transparent"
app:tabSelectedTextColor="@color/red_f3"
app:tabTextAppearance="@style/TabLayoutTextSize"
app:tabTextColor="@color/black_3" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/scroView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/dp_10"
app:layout_anchorGravity="bottom"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginRight="@dimen/dp_5"
android:nestedScrollingEnabled="false" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
监听事件
abLay.addOnOffsetChangedListener(this)
//AppBarLayout滑动监听事件 改变标题、搜索、banner底部背景
override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
val color = this.activity!!.resources.getColor(R.color.zong_db)
val cauResult = Math.abs(verticalOffset*1f)/(appBarLayout?.totalScrollRange ?: 1)
status.setBackgroundColor(PublicTools.tools.changeAlpha(color, abs(cauResult - 1)))
llTitle.setBackgroundColor(PublicTools.tools.changeAlpha(color,abs(cauResult - 1)))
bannerBgView.setBackgroundColor(PublicTools.tools.changeAlpha(color,abs(cauResult - 1)))
}
/**
* 根据百分比改变颜色透明度
*/
fun changeAlpha(color: Int, fraction: Float): Int {
val red: Int = Color.red(color)
val green: Int = Color.green(color)
val blue: Int = Color.blue(color)
val alpha = (Color.alpha(color) * fraction).toInt()
return Color.argb(alpha, red, green, blue)
}