android-fragments ViewPager中的RecyclerView无法滚动

vmdwslir  于 2022-11-14  发布在  Android
关注(0)|答案(7)|浏览(204)

在我的应用程序中,我有一个主页面,其中有一个ViewPager。我可以使用drawerLayout在ViewPager中的片段之间导航。其中一个片段是RecyclerView(在FrameLayout中)。问题是我无法滚动RecyclerView。
我以前问过类似的问题,并得到了解决方案,只是它是一个ScrollView而不是RecyclerView,并且ViewPager在一个ConstraintLayout中,现在它必须在一个RelativeLayout中,这样drawerLayout才能存在。
布局代码:
活动_主.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>

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是可滚动的。提前感谢(:

iih3973s

iih3973s1#

我也有同样的问题。在你定义了你的recyclerView.adapter之后
写这个

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

我希望这对其他人有用

e0uiprwp

e0uiprwp2#

步骤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;

plupiseo

plupiseo3#

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

android:nestedScrollingEnabled="true"

或遵循此link

vxbzzdmp

vxbzzdmp4#

请尝试以下操作:

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

}
yqyhoc1h

yqyhoc1h5#

简单地说,RecyclerView默认是可滚动的,除非在其他布局中,或者我可以说是嵌套的RecyclerView。所以,如果你想让FrameLayout中的RecyclerView是可滚动的,只需要在RecyclerView中设置 nestedScrollingEnabled=“true” 即可。更多信息:遵循此answer

hivapdat

hivapdat6#

如果回收视图在视图页内,并且要滚动回收视图,则必须在视图页中启用嵌套滚动视图。
如果您的视图页位于recyclerview中,并且您希望滚动视图页,则必须在recyclerview中启用嵌套的滚动视图。

android:nestedScrollingEnabled="true"
xpszyzbs

xpszyzbs7#

<RecyclerView>标记中尝试以下操作:

android:touchscreenBlocksFocus="true"
     android:nestedScrollingEnabled="false"

相关问题