I have two views in my NestedScrollView one is my separate imageview and second is recyclerview showing list of images. Now the problem is recyclerview images scrolls but my seprate imageview stays on its position. I want my separate imageview to scroll along recyclerview images.
I have tried many solutions like in some stackoverflow posts users suggest to use setNestedScrollingEnabled(False) property but it not works. I worked on it for many days but no solution works. I will be so glad if someone assist me!
My XML File:
<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>
My JAVA File:
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);
CodePudding user response:
Before setting nested scrolling enabled as false make sure you set that the recycler view has fixed size.
postRV.setHasFixedSize(true);
postRV.setNestedScrollingEnabled(false);
This makes sure that the scrollview is aware of the actual height of the recycler view and scrolling is contained in the scroll view and not the recyclerView.
<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" />
Also the height of recycler view should be wrap_content and not match_parent. As the height needs to be measured after the items are added and needs to be contained inside the scrollview and not the screen.
CodePudding user response:
Try HorizontalScrollView instead of NestedScrollView and RelativeLayout instead of ConstraintLayout
Try below code to horizontal scroll image with 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>
This code work for me.
