【问题标题】:ImageView not scroll along RecyclerView inside NestedScrollViewImageView 不会在 NestedScrollView 内沿 RecyclerView 滚动
【发布时间】:2022-01-24 15:14:30
【问题描述】:

我的 NestedScrollView 中有两个视图,一个是我单独的 imageview,第二个是显示图像列表的 recyclerview。现在问题是 recyclerview 图像滚动,但我单独的 imageview 保持在它的位置。我希望我的单独 imageview 沿着 recyclerview 图像滚动。

我已经尝试过许多解决方案,例如用户建议使用 setNestedScrollingEnabled(False) 属性的一些 stackoverflow 帖子,但它不起作用。我为此工作了很多天,但没有解决方案有效。如果有人帮助我,我会很高兴!

我的 XML 文件:

  <androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

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

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="120dp"
            android:layout_height="120dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/sadboy" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/postRV"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/imageView6"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

我的 JAVA 文件:

   postRV = findViewById(R.id.postRV);
    list = new ArrayList<>();

    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));
    list.add(new StoryModel(R.drawable.girl));


    StoryAdapter storyAdapter = new StoryAdapter(getApplicationContext(),list);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false);

    postRV.setLayoutManager(layoutManager);
    postRV.setNestedScrollingEnabled(false);
    postRV.setAdapter(storyAdapter);

my code output

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    在将启用嵌套滚动设置为 false 之前,请确保您将回收站视图设置为固定大小。

     postRV.setHasFixedSize(true); 
     postRV.setNestedScrollingEnabled(false);
    

    这确保滚动视图知道回收器视图的实际高度,并且滚动包含在滚动视图而不是回收器视图中。

       <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/postRV"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/imageView6"
            app:layout_constraintTop_toTopOf="parent" />
    

    回收站视图的高度也应该是 wrap_content 而不是 match_parent。因为添加项目后需要测量高度,并且需要包含在滚动视图而不是屏幕中。

    【讨论】:

    • 试过但还是同样的问题
    • 我希望我的 imageview 应该使用 recyclerview 水平滚动,请帮助我,我被困在这里
    • 你能分享你的布局管理器代码吗?您使用的是哪个布局管理器?回收站视图应该垂直滚动,图像应该水平滚动?
    【解决方案2】:

    尝试用HorizontalScrollView 代替NestedScrollViewRelativeLayout 代替ConstraintLayout

    尝试下面的代码使用 recyclerview 水平滚动图像:

    <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="none">
    
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal">
                <ImageView
                    android:id="@+id/imageView6"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    app:srcCompat="@drawable/aa_1" />
    
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/postRV"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:layout_toEndOf="@+id/imageView6"
                    android:nestedScrollingEnabled="false"
                    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
            </RelativeLayout>
        </HorizontalScrollView>
    

    这段代码对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多