Skip to content Skip to sidebar Skip to footer

Recyclerview In Bottomsheet Not Working As Expected

I have a problem with RecyclerView directly inside of layout with bottomsheetbehaviour. The problem is that when bottom sheet is expanded and content is scrolled down, when I go to

Solution 1:

I just encountered same problem, but I fixed it by adding this to onCreate:

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

Solution 2:

Your recyclerview item has overighted the scrolling state, so this error generates. The layout you provided does not have enough data to determine the cause. You change the item is a unique view to check

Solution 3:

I had similar issue: Maybe the solution to my problem will give you some ideas. My bottom sheet was expanded to full height with recycler view in it; the bottom sheet was collapsing on user-drag, even though the first item in recycler view wasn't visible yet.

So, what I did:

  1. You can enable/disable bottom sheet dragging by "isDraggable" = true/false
  2. Add OnScrollListener for recycler view.
  3. Override onScrolled and check layoutManager.findFirstVisibleItemPosition() in it
  4. If first item is visible - update bottom sheet behavior.isDraggable = true, i also added small delay before setting behavior.isDraggable = true, because bottom sheet was collapsing too fast, but you might not need it

Maybe it's not optimal but it was fitting my needs and maybe will help you.

Solution 4:

Your implementation might need more coding and with the provided code we might not able to give you good feedback.

Try this documentation https://material.io/develop/android/components/bottom-sheet-behavior/

Plus I found this another implementation. https://www.youtube.com/watch?v=WeaylHAwIIk

Solution 5:

Enable the scroll state of BottomSheet to allow scroll if recyclerview 0th item is visible.

activity_main.xml

<layoutxmlns: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.CoordinatorLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#A8A7A7"tools:context=".MainActivity"><LinearLayoutandroid: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"><TextViewandroid: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.RecyclerViewandroid:id="@+id/rv_items"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout></androidx.coordinatorlayout.widget.CoordinatorLayout></layout>

LockableBottomSheet file that used for handling allow dragging option or not.

classLockableBottomSheetBehavior<V : View?> : BottomSheetBehavior<V> {
    privatevar mAllowUserDragging = trueconstructor()

    constructor(context: Context, attrs: AttributeSet?) : super(
        context,
        attrs
    )

    funsetAllowUserDragging(allowUserDragging: Boolean) {
        mAllowUserDragging = allowUserDragging
    }

    overridefunonInterceptTouchEvent(
        parent: CoordinatorLayout,
        child: V,
        event: MotionEvent
    ): Boolean {
        returnif (!mAllowUserDragging) {
            false
        } elsesuper.onInterceptTouchEvent(parent, child, event)
    }
}

MainActivity.java

var bottomSheetBehavior: LockableBottomSheetBehavior<*>? = nulllateinitvar binding: ActivityMainBinding
overridefunonCreate(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() {

        overridefunonScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)

            val firstPosition = (binding.rvItems.layoutManager as LinearLayoutManager)
                .findFirstVisibleItemPosition()

            updateBottomSheetLockState(firstPosition == 0)
        }
    })

}

funupdateBottomSheetLockState(allow: Boolean) {
    bottomSheetBehavior?.setAllowUserDragging(allow)
}


funupdateBottomSheetState(state: Int) {
    if (bottomSheetBehavior != null) {
        bottomSheetBehavior?.state = state
    }
}

privatefunsetUpBottomSheetBehaviour() {
    val bottomSheetBehavior: BottomSheetBehavior<LinearLayout> =
        BottomSheetBehavior.from(binding.parent)
    this.bottomSheetBehavior = bottomSheetBehavior as LockableBottomSheetBehavior<*>
    updateBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED)
}

Post a Comment for "Recyclerview In Bottomsheet Not Working As Expected"