【问题标题】:Recyclerview not scrolling inside Nested ScrollViewRecyclerview 不在嵌套的 ScrollView 内滚动
【发布时间】:2020-09-07 15:20:42
【问题描述】:

我有Recyclerview,它位于Coordinatorlayout > NestedScrollview > ViewPagerViewPager 有3 个片段,一个有在Recyclerview 的帮助下工作的图片库。每当我尝试向上或向下滚动时,它根本不会滚动。我已经设置了photosRecycler.setNestedScrollingEnabled(false);,一旦我删除了这条线,我就只能向下滚动,当我试图向上移动时,它会随着父级Nestedscrollview 向上移动。

我的布局管理器也将 StaggeredGrid 显示为 Grid 我需要获得这种布局

这是我的 Nestedscrollview 父布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:background="?android:attr/colorBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways">

            <androidx.constraintlayout.widget.ConstraintLayout


                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?android:colorBackground">


                <ImageView

                    android:layout_width="match_parent"
                    android:layout_height="500dp"
                    android:src="@color/custom_transparent_colorBlack"
                    android:scaleType="centerCrop"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    />

                <de.hdodenhof.circleimageview.CircleImageView

                    android:layout_width="128dp"
                    android:layout_height="128dp"
                    app:layout_constraintBottom_toBottomOf="@+id/coverPhoto"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/coverPhoto"
                    app:layout_constraintVertical_bias="0.44"
                    tools:srcCompat="@tools:sample/avatars" />



            </androidx.constraintlayout.widget.ConstraintLayout>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
    <androidx.core.widget.NestedScrollView
        android:id="@+id/up_NestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"

        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            >
            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabItemes"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabGravity="fill"
                app:tabPaddingBottom="0dp"
                app:tabPaddingEnd="0dp"
                app:tabPaddingStart="0dp"
                app:tabPaddingTop="0dp"
                app:tabIndicatorHeight="0dp"
                app:tabMode="fixed"
                app:tabTextColor="@color/black"
                app:tabSelectedTextColor="@color/primary"
                app:tabIndicatorColor="@color/primary_light">

                <com.google.android.material.tabs.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="About" />

                <com.google.android.material.tabs.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Media" />

                <com.google.android.material.tabs.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Con" />
            </com.google.android.material.tabs.TabLayout>

            <androidx.viewpager.widget.ViewPager

                android:layout_below="@id/tabItemes"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </RelativeLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

图片库片段

public class UserPhoto_fragment extends Fragment {
    public static final String TAG="### USER PHOTO ####";
    RecyclerView photosRecycler;
    fetchPhoto_Adapter adapter;
    StaggeredGridLayoutManager layoutManager;
    ArrayList<String> ImageList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.userprofile_photos,container,false);
        setRetainInstance(true);
        photosRecycler=view.findViewById(R.id.userPhotos_recycler);
        layoutManager= new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        ImageList=new ArrayList<>();
        SpacesItemDecoration itemDecoration = new SpacesItemDecoration(16);
        photosRecycler.addItemDecoration(itemDecoration);
        photosRecycler.setLayoutManager(layoutManager);
        photosRecycler.setHasFixedSize(true);
        photosRecycler.setNestedScrollingEnabled(false);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: User Photo Fragment "+getView()+ ImageList.size() );
        if (ImageList.size()==0){
            new fetch_photo().execute();
        }
    }

    public class fetchPhoto_Adapter extends RecyclerView.Adapter<fetchPhoto_Adapter.ViewHolder>{

        @NonNull
        @Override
        public fetchPhoto_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            LayoutInflater inflater= (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.userprofile_photogallery,viewGroup,false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(fetchPhoto_Adapter.ViewHolder viewHolder, int i) {
            Glide.with(getActivity()).load(ImageList.get(i)).apply(new RequestOptions().centerCrop()).into(viewHolder.image);
        }

        @Override
        public int getItemCount() {
            if (ImageList!=null && ImageList.size()>0){
                return ImageList.size();
            }else {
                return 0;
            }
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView image;
            public ViewHolder(View itemView) {
                super(itemView);
                image = itemView.findViewById(R.id.UserProfile_photoThumb);
            }
        }

    }

}

图片库的布局文件

<?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"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/userPhotos_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:scrollbars="vertical">

    </androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>

这段代码我面临 2 个问题

  1. Recyclerview 在nestedscrollview 中不起作用。
  2. StaggeredGridLayoutManager 正在显示类似 GridLayoutManager 的图像

请帮我解决这些问题。谢谢

【问题讨论】:

  • 请分享一个重现您所面临问题的示例项目
  • @SahilManchanda 我分享了几乎完整的代码。这是相同的代码,我仍在尝试修复它。
  • 我们可以从协调器布局中删除嵌套滚动视图并直接放置视图分页器吗?
  • 还有一件事,你能把你的项目设为 1,然后检查项目点击监听器是否工作。我遇到了同样的问题,我可能有一个解决方案。
  • @Abdul i 仅依赖视图寻呼机进行滚动事件,然后它会破坏 CoordinatorLayout 的目的

标签: java android android-recyclerview android-scrollview android-gridlayout


【解决方案1】:

您需要NestedScrollableHost 才能滚动RecyclerView

使用以下布局树为我工作

activity.xml

<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:context=".view.MainActivity">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vp_views"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" />

</androidx.constraintlayout.widget.ConstraintLayout>

fragment.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:constraint="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        ...

        <com.anatoli.thebattleofcards.fabric.NestedScrollableHost
            android:id="@+id/rv_items_host"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            constraint:layout_constraintEnd_toEndOf="parent"
            constraint:layout_constraintStart_toStartOf="parent"
            constraint:layout_constraintTop_toTopOf="parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_items"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"      <!-- change orientation if needs -->
                android:paddingStart="@dimen/layout_padding_small"
                android:paddingEnd="@dimen/layout_padding_small"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

        </com.anatoli.thebattleofcards.fabric.NestedScrollableHost>

        ...

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

【讨论】:

    【解决方案2】:

    我认为你应该把你的tabLayout放在appBar中:

    <androidx.coordinatorlayout.widget.CoordinatorLayout 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:background="?android:attr/colorBackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <android.support.design.widget.CollapsingToolbarLayout
            .
            .
            .
            />
    
                <ImageView
                    .
                    .
                    .
                    />
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
            <android.support.design.widget.TabLayout
            .
            .
            .
                 />
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    并将nestedScrolling 放入您的片段中。阅读这篇文章:CollapsingToolbar with ViewPager and fragments

    【讨论】:

    • 它将从 NestedScrollview 中制作 tablayout
    • 我也试过你的建议,但它与recyclerview没有任何不同
    • 请尝试使其高于布局。谢谢
    【解决方案3】:

    也许你的nestedScrollView 没有覆盖整个屏幕。尝试在你的 nestedScrollView 中添加它

    android:fillViewport="true"
    

    【讨论】:

      【解决方案4】:

      设置好适配器后试试把这条线。

      ViewCompat.setNestedScrollingEnabled(recyclerView, false);
      

      【讨论】:

      • recyclerView.setNestedScrollingEnabled(false); 和你的解决方案有什么区别
      【解决方案5】:

      我也遇到过同样的需求,但是用不同的方式解决了,

      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      

      将此属性设置为 CoordinatorLayout 中的主要内容布局。

          <string name="appbar_scrolling_view_behavior" translatable="false">com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior</string>
      

      布局看起来像

          <androidx.coordinatorlayout.widget.CoordinatorLayout >
      
          <com.google.android.material.appbar.AppBarLayout>
      
              <androidx.appcompat.widget.Toolbar>
      
                  <androidx.constraintlayout.widget.ConstraintLayout>
      
                      <ImageView />
                      //Header content
      
                  </androidx.constraintlayout.widget.ConstraintLayout>
      
              </androidx.appcompat.widget.Toolbar>
      
              <com.google.android.material.tabs.TabLayout />
      
          </com.google.android.material.appbar.AppBarLayout>
      
          <LinearLayout
      
              app:layout_behavior="@string/appbar_scrolling_view_behavior">
      
                  <androidx.viewpager.widget.ViewPager
                      android:id="@+id/viewPager"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:background="@color/light_gray">
      
                  </androidx.viewpager.widget.ViewPager>
      
          </LinearLayout>
      
      
      </androidx.coordinatorlayout.widget.CoordinatorLayout>
      

      并且主片段父视图必须是带有app:layout_behavior的嵌套滚动视图

      注意:我的片段包含线性布局,所以我只使用了wrap_content,如果您使用回收器视图,您应该将其展平在片段内

      【讨论】:

      • 如果我将主要内容放入NestedScrollView,那么它就破坏了CoordinatorLayou的目的
      【解决方案6】:
      1. 由于 viewPager 直接位于您的 CoordinatorLayout 而不是您的 recyclerView 中,因此 viewPager 应该有 scrolling_layout_behaviour 而不是您的 recyclerview

        <androidx.viewpager.widget.ViewPager
           android:layout_below="@id/tabItemes"
           android:layout_width="match_parent"
           android:layout_height="match_parent" 
           app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        
      2. 像这样设置你的 StaggeredGridLayoutManager

        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager); // set LayoutManager to RecyclerView
        

      为了让 StaggeredGridLayout 正常工作,您不能将 imageView 的 scaletype 设置为cropCenter,并且包含 imageView 元素的 cardView 的高度应该设置为包裹内容而不是固定的。

      <android.support.v7.widget.cardview 
         android:layout_height="wrap_content" 
         android:layout_width="match_parent"
         app:cardcornerradius="4dp" 
         app:cardusecompatpadding="true">
      
        <linearlayout 
          android:background="@color/colorPrimary" 
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:orientation="vertical">
      
            <imageview
             android:adjustviewbounds="true" 
             android:id="@+id/placePic" 
             android:layout_height="match_parent" 
             android:layout_width="match_parent" 
             android:scaletype="fitXY" />
      
         </linearlayout>
      </android.support.v7.widget.cardview>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-13
        • 1970-01-01
        • 1970-01-01
        • 2016-09-15
        • 2016-10-24
        • 2020-03-26
        • 1970-01-01
        相关资源
        最近更新 更多