ImageView未在NestedScrollView中沿RecyclerView滚动



我的NestedScrollView中有两个视图,一个是我单独的imageview,另一个是显示图像列表的recyclerview。现在的问题是回收查看的图像滚动,但我的seprate图像视图停留在它的位置。我希望我的单独图像视图能够沿着可循环查看的图像滚动

我已经尝试过许多解决方案,比如在一些stackoverflow帖子中,用户建议使用setNestedScrollingEnabled(False(属性,但它不起作用。我研究了很多天,但没有任何解决方案。如果有人帮助我,我会很高兴的

我的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);

我的代码输出

在将嵌套滚动设置为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。因为添加项目后需要测量高度,并且需要包含在滚动视图中,而不是屏幕中。

尝试HorizontalScrollView而不是NestedScrollView,尝试RelativeLayout而不是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>

这个代码对我有效。

最新更新