android CollapsingToolbarLayout+RecyclerView折叠+BaseRecyclerViewAdapterHelper
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/Theme.Test1.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@mipmap/mm"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/Theme.Test1.PopupOverlay" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="这是标题"/>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- <include layout="@layout/content_scrolling" />-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcv_data"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="wrap_content"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/fab_margin"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
activity代码
package com.nyw.folddemo
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.AppBarLayout.OnOffsetChangedListener
import com.nyw.folddemo.view.RecyManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var adapter: BaseQuickAdapter<HomeItem, BaseViewHolder>
var homeDataList: MutableList<HomeItem> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initView();
}
private fun initView() {
initAdapter()
}
/**
* 初始化容器
*/
fun initAdapter(){
RecyManager.setBaseVertical(this, rcv_data)
RecyManager.setItemDecoration(rcv_data, 15, 15, 0, 0)
adapter = object : BaseQuickAdapter<HomeItem, BaseViewHolder>(R.layout.layout_index_item,
mutableListOf()){
override fun convert(holder: BaseViewHolder, item: HomeItem) {
Glide.with(context).load(item.imgUrl).error(R.mipmap.m1)
.into(holder.getView(R.id.iv_img))
holder.setText(R.id.tv_title,item.title)
}
}
rcv_data.adapter=adapter
for (i in 1..20){
var homeItem :HomeItem = HomeItem()
homeItem.title="好看的小姐姐"
homeItem.imgUrl=""
homeDataList.add(homeItem)
}
adapter.setList(homeDataList)
}
}
themes.xml文件
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.FoldDemo" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/purple_200</item> <!-- 导航栏颜色 -->
<item name="colorPrimaryDark">@color/purple_200</item> <!-- 状态栏颜色 -->
<item name="colorAccent">@color/purple_200</item> <!-- 控件选中颜色 -->
<item name="android:windowBackground">@color/gray_f5</item> <!-- 窗口背景色 -->
</style>
<dimen name="app_bar_height">180dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="text_margin">16dp</dimen>
<style name="Theme.Test1.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.Test1.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
recyclerView设置布局管理器代码
package com.nyw.folddemo.view
import android.annotation.SuppressLint
import android.content.Context
import androidx.recyclerview.widget.*
/**
* 类描述:recyclerView设置布局管理器
* 修改备注:
*/
object RecyManager {
//设置item的间距
fun setItemDecoration(recyclerView: RecyclerView, space_top: Int, space_bottom: Int, space_left: Int, space_right: Int){
recyclerView.addItemDecoration(SpaceItemDecoration(space_top, space_bottom, space_left, space_right))
}
//竖向滑动
@SuppressLint("WrongConstant")
fun setBaseVertical(mContext: Context, recyclerView: RecyclerView): LinearLayoutManager {
val layoutManager = LinearLayoutManager(mContext)
//设置为垂直布局,这也是默认的
layoutManager.orientation = OrientationHelper.VERTICAL
//设置增加或删除条目的动画
recyclerView.itemAnimator = DefaultItemAnimator()
//设置布局管理器
recyclerView.layoutManager = layoutManager
return layoutManager
}
//横向滑动
@SuppressLint("WrongConstant")
fun setHorizontalBase(mContext: Context, recyclerView: RecyclerView): LinearLayoutManager {
val layoutManager = LinearLayoutManager(mContext)
//设置为垂直布局,这也是默认的
layoutManager.orientation = OrientationHelper.HORIZONTAL
//设置增加或删除条目的动画
recyclerView.itemAnimator = DefaultItemAnimator()
//设置布局管理器
recyclerView.layoutManager = layoutManager
return layoutManager
}
//Grid
fun setGridBase(mContext: Context, recyclerView: RecyclerView, line: Int) {
val layoutManager = GridLayoutManager(mContext, line)
//设置增加或删除条目的动画
recyclerView.itemAnimator = DefaultItemAnimator()
//设置布局管理器
recyclerView.layoutManager = layoutManager
}
}
SpaceItemDecoration类
package com.nyw.folddemo.view
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
/**
* 类描述:
* 创建人:Administrator
* 修改备注:
*/
class SpaceItemDecoration(var space_top: Int, var space_bottom: Int, var space_left: Int, var space_right: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
outRect.top = space_top
outRect.bottom = space_bottom
outRect.left = space_left
outRect.right = space_right
}
}
HomeItem类
package com.nyw.folddemo
class HomeItem {
var imgUrl :String = ""
var title :String = ""
}
build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
//不需要findViewById
apply plugin: 'kotlin-android-extensions'
android {
compileSdk 32
defaultConfig {
applicationId "com.nyw.folddemo"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'
implementation 'com.github.bumptech.glide:glide:4.11.0'
}
demo代码
(101条消息) kotlinCollapsingToolbarLayout+RecyclerView折叠-Android文档类资源-CSDN文库