【Android+Kotlin】NestedScrollView
阿新 • • 發佈:2021-02-18
扉:
- 找了蠻多帖子感覺不是很友好,找到了一個帖子並升級了androidx
- 這個帖子不錯
- AppBarLayout與沉浸式佈局好看
- CollapsingToolLayout可摺疊工具欄
- 下載地址
1. 效果圖
- 上面是banner區域
- 向下拖動到一定程度圖片會變暗幕
- 固定中間的懸浮部分 下側RecycleView正常滑動
2. 上程式碼
1. XML
- 注意事項
1)將需要懸浮的layout放到CollapsingToolbarLayout之外,AppBarLayout之內
2)將CollapsingToolbarLayout的app:layout_scrollFlags設定為scroll
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
}
}