11

Following are my xmls

MainView

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your content -->
    <include layout="@layout/content_main"/>

    <!-- Bottom Sheet -->
    <include layout="@layout/main_bottom_sheet"/>

</android.support.design.widget.CoordinatorLayout>

BottomSheet View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_navigation_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical"
    app:behavior_peekHeight="56dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <FrameLayout
        android:id="@+id/fragment_bottom_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

RecycleView Screen

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@color/white"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        android:background="@color/alabaster"
        android:elevation="@dimen/elevation_normal"
        android:orientation="horizontal"
        tools:targetApi="lollipop">

        <FrameLayout
            android:id="@+id/search_as_you_type_back_button"
            android:layout_width="@dimen/toolbar_height"
            android:layout_height="@dimen/toolbar_height"
            android:foreground="?selectableItemBackground">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/accessibility_back"
                app:srcCompat="@drawable/ic_arrow_back"/>

        </FrameLayout>

        <EditText
            android:id="@+id/search_as_you_type_edit_text"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/margin_medium"
            android:layout_weight="1"
            android:background="@color/transparent"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center_vertical"
            android:imeOptions="actionSearch"
            android:inputType="text"
            android:textColor="@color/darkest_grey"
            android:textSize="@dimen/text_medium"
            tools:text="Chocolate"/>

        <FrameLayout
            android:id="@+id/search_as_you_type_close_container"
            android:layout_width="?attr/actionBarSize"
            android:layout_height="?attr/actionBarSize"
            android:foreground="?selectableItemBackground"
            android:visibility="invisible">

            <ImageView
                android:id="@+id/search_as_you_type_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/accessibility_close"
                app:srcCompat="@drawable/ic_cancel_grey_24dp"/>

        </FrameLayout>

    </LinearLayout>

    <ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inAnimation="@android:anim/fade_in"
        android:outAnimation="@android:anim/fade_out">


      <android.support.v7.widget.RecyclerView
          android:id="@+id/auto_suggest_list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"
          android:nestedScrollingEnabled="true"/>


    </ViewFlipper>

</LinearLayout>

Fragment bottom container layout will be inflated with a fragment which has recyclerview screen. The Recyclerview here is not scrolling after populating it with data. As well its not getting adjusted when the keyboard pops up. I tried setting windowssoftInputMode. Could someone help in how to solve this.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
andinrajesh
  • 575
  • 2
  • 9
  • 24

4 Answers4

15

This is the working code for RecyclerView inside BottomSheet

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">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewMore"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusableInTouchMode="true"
                android:orientation="vertical"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
          
        </RelativeLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

In Java in onCreateView

B.recyclerViewMore.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.getParent().requestDisallowInterceptTouchEvent(true);
        v.onTouchEvent(event);
        return true;
    }
});
Aldan
  • 674
  • 9
  • 23
lincy
  • 151
  • 1
  • 3
14

lincy's solution didn't work for me. The events never got through to the OnTouchListener.

My solution is to wrap the RecyclerView with a custom layout that hooks into dispatchTouchEvent:

<info.mschmitt.view.TouchEventInterceptorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</info.mschmitt.view.TouchEventInterceptorLayout>

The code for TouchEventInterceptorLayout (constructors removed):

public class TouchEventInterceptorLayout extends FrameLayout {
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean handled = super.dispatchTouchEvent(ev);
        requestDisallowInterceptTouchEvent(true);
        return handled;
    }
}

That will recursively disable touch event handling in all parents for the current touch interaction, leaving only the RecyclerView as a target for processing.

Why all of this is necessary I don't know. Maybe it's a bug in Google's BottomSheet implementation.

Aldan
  • 674
  • 9
  • 23
Matt
  • 476
  • 5
  • 7
  • Awesome, thank you so much! – Eric B. Oct 07 '21 at 18:41
  • My app will always crash with this error "Caused by: android.view.InflateException: Binary XML file line #10 in com.imincode.earthlings:layout/generic_item_list: Error inflating class com.imincode.earthlings.helpers.TouchEventInterceptorLayout Caused by: java.lang.NoSuchMethodException: com.imincode.earthlings.helpers.TouchEventInterceptorLayout. [class android.content.Context, interface android.util.AttributeSet]" – imin Jan 06 '22 at 17:28
  • 1
    nvm I got it fixed. Need to use the constructor `public TouchEventInterceptorLayout(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); }` – imin Jan 06 '22 at 17:34
  • class TouchEventInterceptorLayout @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null ) : FrameLayout(context, attrs) – Jayamurugan Jan 10 '23 at 08:12
0
rv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    v.getParent().requestDisallowInterceptTouchEvent(true);
    v.onTouchEvent(event);
    return true;
}
});
iminiki
  • 2,549
  • 12
  • 35
  • 45
0

This one is a optimization of @Matt solution as it was not working on my case.

class TouchEventInterceptorLayout(context: Context, attrs: AttributeSet?) :
FrameLayout(context, attrs) {
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
    val handled = super.dispatchTouchEvent(ev)
    requestDisallowInterceptTouchEvent(true)
    return handled
}}

And wrap the recylerview like this

<com.demo.view.TouchEventInterceptorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</com.demo.view.TouchEventInterceptorLayout>
Rabindra Khadka
  • 1,344
  • 1
  • 13
  • 23