3

In my app I have my main page, in it a ViewPager. I can navigate between fragments in the ViewPager using a drawerLayout. One of the fragments is a RecyclerView (in a FrameLayout). The problem is I cannot scroll the RecyclerView.

I've asked a question of the sort before, and got the solution, only it was a ScrollView instead of RecyclerView, and the ViewPager was in a ConstraintLayout, and now it has to be in a RelativeLayout so the drawerLayout could exist.

code of the layouts:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/mainScreenContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorPrimary"
    tools:context=".MainActivity">


    <include
        android:id="@+id/mainToolbar"
        layout="@layout/main_toolbar_layout" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/mainTabLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="gone">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/activeGoals"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/achievedGoals"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/stats"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tipsAndTricks"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/aboutUs"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/mainViewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/mainToolbar"/>

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_below="@id/mainToolbar">

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/list_nav_drawer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/menu_items" />


    </androidx.drawerlayout.widget.DrawerLayout>



</RelativeLayout>

fragment_active_goals.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:fillViewport="true"
    tools:context=".ActiveGoals">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/activeGoalsRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

I'd want the RecyclerView to be scrollable. thanks in advance (:

Nitzan Daloomy
  • 166
  • 5
  • 24

7 Answers7

4

I have de same problem. After you define your recyclerView.adapter

Write this

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

I hope that's useful for someone else

3

Step 1 Create a custom RecyclerView class

public class CustomRecyclerView extends RecyclerView {

  public CustomRecyclerView(@NonNull Context context) {
    super(context);
  }

  public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {

      case MotionEvent.ACTION_MOVE:
        getParent().requestDisallowInterceptTouchEvent(true);
        break;
    }
    return super.dispatchTouchEvent(ev);
  }
}

This solution gives priority of scroll to the RecyclerView.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
saman
  • 86
  • 8
1

Try adding line inside XML of recycleview:

android:nestedScrollingEnabled="true"

Or follow this link

Dharmik Raval
  • 101
  • 10
1

Try something like this :

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <include
        android:id="@+id/mainToolbar"
        layout="@layout/main_toolbar_layout" />
    .
    .
    .
    .
    .
    .
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>

or you can use custom recycler view like below :

public class CustomRecycleriew extends RecyclerView{

    public CustomRecycleriew(Context context) {
        super(context);
    }

    public CustomRecycleriew(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRecycleriew(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                getParent().requestDisallowInterceptTouchEvent(false);
                break;

            case MotionEvent.ACTION_CANCEL:
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.dispatchTouchEvent(event);
    }

}
  • Hi rajinikanth, thanks for the answer. the xml solution solved the scrolling issue, but created a few issues with the navigation drawer - First, I cannot choose items from it now. Second, in order to close it I can no longer drag it, but only click out of it or choosing (not really choosing, just clicking on) one of the navigation drawer's options. Tried also the custom recyclerview solution, but it didn't work. how can I solve the new problem? – Nitzan Daloomy Jul 29 '19 at 17:52
  • Forgot to mention, the third problem is that the navigation drawer is over the toolbar – Nitzan Daloomy Jul 29 '19 at 18:04
0

Briefly saying, RecyclerView is scrollable by default unless inside other Layout or I might say nested RecyclerView. So, if you want your RecyclerView inside the FrameLayout as scrollable, just simply set nestedScrollingEnabled="true" inside RecyclerView. More info: follow this answer

Farhan Ibn Wahid
  • 926
  • 1
  • 9
  • 22
0

If your recyclerview is inside viewpager and if you want to scroll recyclerview then you have to enable nestedscrollview in viewpager.

If your viewpager is inside recyclerview and if you want to scroll viewpager then you have to enable nestedscrollview in recyclerview .

android:nestedScrollingEnabled="true"
AKHIL V S
  • 64
  • 6
0

Try this inside your <RecyclerView> tag :

     android:touchscreenBlocksFocus="true"
     android:nestedScrollingEnabled="false"
Ghazal
  • 123
  • 2
  • 9