【发布时间】:2020-02-25 06:16:28
【问题描述】:
背景
我想支持如下所示的全屏导航 UI:
https://medium.com/androiddevelopers/windowinsets-listeners-to-layouts-8f9ccc8fa4d1 https://developer.android.com/guide/navigation/gesturenav
问题
虽然我的应用程序的所有活动都运行良好,但我突然遇到了一个有问题的活动,它有一个带有拇指的 RecyclerView。
在这里,我遇到了 2 个奇怪的问题:
- 滚动到底部时,最后一项不会完全显示。
- 在横向模式下,拇指会移到我可以触及的任何地方,因此它们也无法触摸。不仅如此,我还可以看到正常的滚动条,滚动到底部时两者都会消失:
我尝试过的
我尝试将插图应用于各种视图,包括填充和边距,但没有任何帮助。
另外,在某些网站中,我看到我应该使用View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION,而在某些网站中,我还需要添加View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN。没有帮助。
这是我当前的代码(项目可用here,因为我认为这是一个错误):
MainActivity.kt
class MainActivity : AppCompatActivity(R.layout.activity_main) {
inline fun View.updateMargins(@Px left: Int = marginLeft, @Px top: Int = marginTop, @Px right: Int = marginRight, @Px bottom: Int = marginBottom) {
updateLayoutParams<ViewGroup.MarginLayoutParams> {
this.bottomMargin = bottom
this.topMargin = top
this.leftMargin = left
this.rightMargin = right
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setSupportActionBar(toolbar)
findViewById<View>(android.R.id.content).systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
ViewCompat.setOnApplyWindowInsetsListener(appBarLayout) { _, insets ->
val systemWindowInsets = insets.systemWindowInsets
appBarLayout.updateMargins(
left = systemWindowInsets.left,
top = systemWindowInsets.top,
right = systemWindowInsets.right
)
insets
}
ViewCompat.setOnApplyWindowInsetsListener(recyclerView) { _, insets ->
val systemWindowInsets = insets.systemWindowInsets
recyclerView.updatePadding(
left = systemWindowInsets.left,
bottom = systemWindowInsets.bottom,
right = systemWindowInsets.right
)
insets
}
recyclerView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
init {
setHasStableIds(true)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder {
return object : RecyclerView.ViewHolder(
LayoutInflater.from(this@MainActivity).inflate(R.layout.list_item, parent, false)
) {}
}
override fun getItemId(position: Int): Long = position.toLong()
override fun getItemCount(): Int = 100
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.itemView.imageView.setColorFilter(if (position % 2 == 0) 0xffff0000.toInt() else 0xff00ff00.toInt())
holder.itemView.textView.text = "item $position"
}
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menu.add("test").setIcon(android.R.drawable.ic_dialog_email).setOnMenuItemClickListener {
true
}.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
return super.onCreateOptionsMenu(menu)
}
}
styles.xml(我在清单中使用 AppTheme 作为主题)
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">
@android:color/transparent
</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/colorPrimaryDark
</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />
</resources>
activity_main.xml
<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" tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<!--app:popupTheme="@style/AppTheme.PopupOverlay"-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:scrollbars="vertical" app:fastScrollEnabled="true"
app:fastScrollHorizontalThumbDrawable="@drawable/thumb_drawable"
app:fastScrollHorizontalTrackDrawable="@drawable/line_drawable"
app:fastScrollVerticalThumbDrawable="@drawable/thumb_drawable"
app:fastScrollVerticalTrackDrawable="@drawable/line_drawable"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:itemCount="100"
tools:listitem="@layout/list_item" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
line.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
<padding
android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape>
line_drawable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/line" android:state_pressed="true" />
<item android:drawable="@drawable/line" />
</selector>
thumb.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners
android:bottomLeftRadius="44dp" android:topLeftRadius="44dp" android:topRightRadius="44dp" />
<padding
android:paddingLeft="22dp" android:paddingRight="22dp" />
<solid android:color="@color/colorPrimaryDark" />
</shape>
thumb_drawable.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/thumb" android:state_pressed="true" />
<item android:drawable="@drawable/thumb" />
</selector>
问题
- 为什么会发生? 它适用于其他各种地方...
- 如何让 RecyclerView 避免这两种情况,同时允许底部的导航栏显示 RecyclerView 的内容,因为它是透明的?
- 在哪些情况下我应该添加标志
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN?在这些情况下它有什么帮助?
编辑:一种可能的解决方法是避免使用 CoordinatorLayout。它运作良好,但我想以“官方方式”做事。这是解决方法:
我使用的不是 CoordinatorLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
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" tools:context=".MainActivity">
...
在代码中,我为 RecyclerView 设置了边距和内边距:
ViewCompat.setOnApplyWindowInsetsListener(recyclerView) { _, insets ->
val systemWindowInsets = insets.systemWindowInsets
recyclerView.updatePadding(
bottom = systemWindowInsets.bottom
)
recyclerView.updateMargins(
left = systemWindowInsets.left,
right = systemWindowInsets.right
)
insets
}
【问题讨论】:
标签: android android-recyclerview