【问题标题】:RecyclerView in ViewPager won't ScrollViewPager 中的 RecyclerView 不会滚动
【发布时间】:2019-07-28 16:50:25
【问题描述】:

在我的应用程序中,我有我的主页,其中有一个 ViewPager。我可以使用抽屉布局在 ViewPager 中的片段之间导航。其中一个片段是 RecyclerView(在 FrameLayout 中)。问题是我无法滚动 RecyclerView。

我之前问过一个问题,得到了解决方案,只是它是一个ScrollView而不是RecyclerView,并且ViewPager在一个ConstraintLayout中,现在它必须在一个RelativeLayout中,这样drawerLayout才能存在.

布局代码:

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>

我希望 RecyclerView 可以滚动。在此先感谢(:

【问题讨论】:

  • 只是为了确认,请您删除 DrawerLayout 只是为了测试,然后尝试 RecyclerView 仍然不可滚动
  • 尝试将您的 ViewPager layout_height 更改为“match_parent”
  • @GiddyNaya 试过了,没有解决问题。

标签: android android-layout android-fragments android-recyclerview android-viewpager


【解决方案1】:

我也有同样的问题。 在你定义你的recyclerView.adapter

写这个

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

我希望这对其他人有用

【讨论】:

    【解决方案2】:

    尝试在recycleview的XML中添加一行:

    android:nestedScrollingEnabled="true"
    

    或关注此link

    【讨论】:

    • 链接有没有帮助?
    【解决方案3】:

    试试这样的:

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

    或者您可以使用自定义回收器视图,如下所示:

    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);
        }
    
    }
    

    【讨论】:

    • 您好 rajinikanth,感谢您的回答。 xml 解决方案解决了滚动问题,但在导航抽屉中产生了一些问题 - 首先,我现在无法从中选择项目。其次,为了关闭它,我不能再拖动它,而只能单击它或选择(不是真正选择,只是单击)导航抽屉的选项之一。还尝试了自定义 recyclerview 解决方案,但没有奏效。如何解决新问题?
    • 忘了说了,第三个问题是导航抽屉在工具栏上方
    【解决方案4】:

    第 1 步创建自定义 RecyclerView 类

    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);
      }
    }
    

    此解决方案优先考虑滚动到 RecyclerView。

    【讨论】:

      【解决方案5】:

      简单地说,RecyclerView 默认是可滚动的,除非在其他 Layout 中或者我可能会说嵌套 RecyclerView。因此,如果您希望 FrameLayout 中的 RecyclerView 可滚动,只需在 RecyclerView 中设置 nestedScrollingEnabled="true" 即可。 更多信息:关注answer

      【讨论】:

        【解决方案6】:

        如果您的 recyclerview 在 vi​​ewpager 内,并且如果您想滚动 recyclerview,那么您必须在 viewpager 中启用nestedscrollview。

        如果您的 viewpager 在 recyclerview 中,并且如果您想滚动 viewpager,那么您必须在 recyclerview 中启用nestedscrollview。

        android:nestedScrollingEnabled="true"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多