【Android+Kotlin】NestedScrollView
作者:互联网
扉:
- 找了蛮多帖子感觉不是很友好,找到了一个帖子并升级了androidx
- 这个帖子不错
- AppBarLayout与沉浸式布局好看
- CollapsingToolLayout可折叠工具栏
- 下载地址[]
1. 效果图
- 上面是banner区域
- 向下拖动到一定程度图片会变暗幕
- 固定中间的悬浮部分 下侧RecycleView正常滑动
2. 上代码
1. XML
- 注意事项
1)将需要悬浮的layout放到CollapsingToolbarLayout之外,AppBarLayout之内
2)将CollapsingToolbarLayout的app:layout_scrollFlags设置为scroll
3)给滚动的NestedScroolView设置
app:layout_behavior="@String/appbar_scrolling_view_behavior"
就大功告成了(记得根布局要是CoordinatorLayout)
<?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:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="220dp"
app:contentScrim="#000000"
app:layout_scrollFlags="scroll">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/banner"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="banner区域"
android:textColor="#000000" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="悬浮的部分" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.ywjh.nestedscrollview.MyListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
2. MyListView
package com.ywjh.nestedscrollview
import android.content.Context
import android.util.AttributeSet
import android.widget.ListView
class MyListView : ListView {
constructor(context: Context?) : super(context) {}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
}
public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val expandSpec = MeasureSpec.makeMeasureSpec(
Int.MAX_VALUE shr 2,
MeasureSpec.AT_MOST
)
super.onMeasure(widthMeasureSpec, expandSpec)
}
}
3. MAC
package com.ywjh.nestedscrollview
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class MainActivity : AppCompatActivity() {
private var lv: MyListView? = null
private var list: MutableList<String?>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lv = findViewById<View>(R.id.lv) as MyListView
list = ArrayList()
for (i in 0..99) {
list?.add(i.toString() + "")
}
val adapter: ArrayAdapter<*> =
ArrayAdapter<Any?>(this, android.R.layout.simple_list_item_1,
list as ArrayList<String?> as List<Any?>
)
lv!!.adapter = adapter
}
}
标签:layout,Kotlin,list,context,NestedScrollView,import,Android,super,android 来源: https://blog.csdn.net/qq_38304672/article/details/113414863