底部表格中的回收表无法正常工作



我直接在底部的布局内部的recyclerview有问题。问题是,当底部的底表被扩展并向下滚动内容时,当我滚动滚动时,它会导致底部开始倒塌,而不是首先将RecyClerview滚回顶部。

这是一个展示问题的视频。如您所见,当我向下滚动在扩展的底部纸上时,出现了问题。它立即开始崩溃,而不是"等待" recyclerview首先滚动到顶部。

这是我的布局代码

<?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:id="@+id/scheduleRoot"
    android:layout_height="match_parent"
    tools:context=".schedule.ScheduleFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scheduleSheet"
        app:behavior_peekHeight="300dp"
        android:elevation="16dp"
        android:clickable="false"
        android:focusable="false"
        android:background="@drawable/bg_bottom_sheet"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scheduleRecyclerView"
            android:clickable="true"
            android:focusable="true"
            android:layout_marginTop="8dp"/>
    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

任何帮助都将受到赞赏!

我刚刚遇到了同样的问题,但是我通过将其添加到otCreate中解决了:

androidx.core.view.ViewCompat.setNestedScrollingEnabled(recyclerview, false);

add

android:nestedScrollingEnabled="true"

在底部的底部布局中。

我也有类似的问题:也许解决我的问题的解决方案会给您一些想法。我的底部床单扩大到了全高,其中有回收器的视图。底部的纸在用户拖盘上崩溃了,即使recycler视图中的第一项尚不可见。

所以,我做了什么:

  1. 您可以启用/禁用底部表格" iSdraggable"。= true/false
  2. 添加回收器视图的OnScrolllistener。
  3. 覆盖了crolled并查看layoutmanager.findfirstvisibleitemposition((
  4. 如果第一个项目可见 - 更新底部表行为。

也许这不是最佳的,但它适合我的需求,也许会帮助您。

您的recyclerview项目已超过滚动状态,因此此错误会生成。您提供的布局没有足够的数据来确定原因。您更改项目是一个独特的视图,可以检查

我玩了很长时间,并尝试了太多解决方案。对我来说,这效果最好:

        val layoutManager = LinearLayoutManager(requireContext())
        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.setOnScrollChangeListener { _, _, _, _, _ ->
            if (bottomSheetBehavior.state == BottomSheetBehavior.STATE_EXPANDED) {
                bottomSheetBehavior.isDraggable = layoutManager.findFirstCompletelyVisibleItemPosition() == 0
            } else {
                bottomSheetBehavior.isDraggable = true
            }
        }

解决方案的关键是控制用户能力在部分滚动时拖动底部表。该方法仅一旦最高单元格完全可见,该方法才允许再次滚动。

它不是理想的选择,因为用户可能希望抓住底部纸的顶部(假设其不是回收器视图的一部分(并将底部丢弃,而不管其滚动位置如何。我只是接受。

无论您做什么,> 都不尝试这些,因为它们只是禁用任何回收功能,并且在实例化时加载了所有细胞,对性能产生非常不良影响:

wrap_content

或:

binding.recyclerView.isNestedScrollingEnabled = false

启用底部表格的滚动状态,如果可见的是recyclerview 0,允许滚动。

activity_main.xml

<layout 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">
    <data />
    <androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#A8A7A7"
    tools:context=".MainActivity">
    <LinearLayout
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:orientation="vertical"
        app:behavior_hideable="true"
        app:behavior_peekHeight="80dp"
        app:layout_behavior="com.asadmukhtar.recyclerviewinsidebottomsheet.LockableBottomSheetBehavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="Drag Me"
            android:textColor="#000"
            android:textSize="20sp"
            android:textStyle="bold" />
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_items"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

用于处理的LockableBottomSheet文件是否允许拖动选项。

class LockableBottomSheetBehavior<V : View?> : BottomSheetBehavior<V> {
    private var mAllowUserDragging = true
    constructor()
    constructor(context: Context, attrs: AttributeSet?) : super(
        context,
        attrs
    )
    fun setAllowUserDragging(allowUserDragging: Boolean) {
        mAllowUserDragging = allowUserDragging
    }
    override fun onInterceptTouchEvent(
        parent: CoordinatorLayout,
        child: V,
        event: MotionEvent
    ): Boolean {
        return if (!mAllowUserDragging) {
            false
        } else super.onInterceptTouchEvent(parent, child, event)
    }
}

mainActivity.java

var bottomSheetBehavior: LockableBottomSheetBehavior<*>? = null
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding =
        DataBindingUtil.setContentView(this, R.layout.activity_main)
    setUpBottomSheetBehaviour()
    binding.rvItems.layoutManager = LinearLayoutManager(this)
    binding.rvItems.adapter = RecyclerViewAdapter(this)
    binding.rvItems.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
            val firstPosition = (binding.rvItems.layoutManager as LinearLayoutManager)
                .findFirstVisibleItemPosition()
            updateBottomSheetLockState(firstPosition == 0)
        }
    })
}
fun updateBottomSheetLockState(allow: Boolean) {
    bottomSheetBehavior?.setAllowUserDragging(allow)
}

fun updateBottomSheetState(state: Int) {
    if (bottomSheetBehavior != null) {
        bottomSheetBehavior?.state = state
    }
}
private fun setUpBottomSheetBehaviour() {
    val bottomSheetBehavior: BottomSheetBehavior<LinearLayout> =
        BottomSheetBehavior.from(binding.parent)
    this.bottomSheetBehavior = bottomSheetBehavior as LockableBottomSheetBehavior<*>
    updateBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED)
}

您的实现可能需要更多的编码,并且使用提供的代码,我们可能无法为您提供良好的反馈。

尝试此文档https://material.io/develop/android/components/bottom-sheet-behavior/

加上我发现了另一个实现。https://www.youtube.com/watch?v=weaylhawiik

最新更新