To disable scrolling in a child RecyclerView in Android, you typically need to override the default scrolling behavior of the RecyclerView or use layout managers and touch listeners to prevent user interactions. Here's a detailed approach to achieve this:
RecyclerView ClassYou can create a custom RecyclerView that disables scrolling by overriding its onTouchEvent and onInterceptTouchEvent methods.
RecyclerView Classimport android.content.Context; import android.util.AttributeSet; import androidx.recyclerview.widget.RecyclerView; public class NonScrollableRecyclerView extends RecyclerView { public NonScrollableRecyclerView(Context context) { super(context); } public NonScrollableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public NonScrollableRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean canScrollVertically(int direction) { // Return false to disable vertical scrolling return false; } @Override public boolean canScrollHorizontally(int direction) { // Return false to disable horizontal scrolling return false; } } RecyclerView in Your LayoutReplace your existing RecyclerView in your XML layout file with the custom one:
<com.example.yourpackage.NonScrollableRecyclerView android:id="@+id/your_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content"/>
You can also use a LinearLayoutManager or GridLayoutManager and override its scrolling methods.
import android.content.Context; import androidx.recyclerview.widget.LinearLayoutManager; public class NonScrollableLinearLayoutManager extends LinearLayoutManager { public NonScrollableLinearLayoutManager(Context context) { super(context); } @Override public boolean canScrollVertically() { // Return false to disable vertical scrolling return false; } @Override public boolean canScrollHorizontally() { // Return false to disable horizontal scrolling return false; } } RecyclerViewIn your activity or fragment:
RecyclerView recyclerView = findViewById(R.id.your_recycler_view); NonScrollableLinearLayoutManager layoutManager = new NonScrollableLinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager);
If you want to disable scrolling programmatically, you can add an OnTouchListener to your RecyclerView to consume touch events.
OnTouchListenerRecyclerView recyclerView = findViewById(R.id.your_recycler_view); recyclerView.setOnTouchListener((v, event) -> true); // Return true to consume the touch event
For simple use cases, if the child RecyclerView is inside a ScrollView, setting its height to wrap_content might be sufficient to prevent scrolling.
RecyclerView Height to wrap_contentIn your XML layout:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <RecyclerView android:id="@+id/your_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> </ScrollView>
RecyclerView Class: Override methods to prevent scrolling.LinearLayoutManager or GridLayoutManager.OnTouchListener to consume touch events.RecyclerView height to wrap_content inside a ScrollView if applicable.Choose the method that best fits your use case and application structure.
How to disable scrolling in a child RecyclerView in Android?
onTouchEvent method in a custom RecyclerView to disable scrolling.import android.content.Context; import android.util.AttributeSet; import androidx.recyclerview.widget.RecyclerView; public class NonScrollableRecyclerView extends RecyclerView { public NonScrollableRecyclerView(Context context) { super(context); } public NonScrollableRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public NonScrollableRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onTouchEvent(MotionEvent e) { return false; // Disables scrolling } @Override public boolean onInterceptTouchEvent(MotionEvent e) { return false; // Disables scrolling } } Description: Creates a custom RecyclerView that disables scrolling by overriding onTouchEvent and onInterceptTouchEvent.How to disable vertical scrolling in a child RecyclerView?
RecyclerView's onScrollStateChanged method to disable vertical scrolling.import android.content.Context; import android.util.AttributeSet; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; public class NoVerticalScrollRecyclerView extends RecyclerView { public NoVerticalScrollRecyclerView(Context context) { super(context); } public NoVerticalScrollRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public NoVerticalScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onScrollStateChanged(int state) { if (getLayoutManager() instanceof LinearLayoutManager) { ((LinearLayoutManager) getLayoutManager()).setSmoothScrollbarEnabled(false); } super.onScrollStateChanged(state); } @Override public boolean canScrollVertically() { return false; // Disables vertical scrolling } } Description: Disables vertical scrolling by setting canScrollVertically to false.How to disable horizontal scrolling in a child RecyclerView?
canScrollHorizontally method to prevent horizontal scrolling.import android.content.Context; import android.util.AttributeSet; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; public class NoHorizontalScrollRecyclerView extends RecyclerView { public NoHorizontalScrollRecyclerView(Context context) { super(context); } public NoHorizontalScrollRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public NoHorizontalScrollRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean canScrollHorizontally(int direction) { return false; // Disables horizontal scrolling } } Description: Disables horizontal scrolling by returning false from canScrollHorizontally.How to disable scrolling in a nested RecyclerView using custom LayoutManager?
LayoutManager to disable scrolling.import android.content.Context; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; public class NonScrollingLinearLayoutManager extends LinearLayoutManager { public NonScrollingLinearLayoutManager(Context context) { super(context); } @Override public boolean canScrollVertically() { return false; // Disables vertical scrolling } @Override public boolean canScrollHorizontally() { return false; // Disables horizontal scrolling } } Description: Custom LinearLayoutManager to disable both vertical and horizontal scrolling.How to use a ScrollView to prevent scrolling in a nested RecyclerView?
RecyclerView in a ScrollView and set RecyclerView's scrolling to false.<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <RecyclerView android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none" /> </ScrollView>Description: Disables
RecyclerView scrolling by setting android:scrollbars to none.How to disable RecyclerView scrolling programmatically in Android?
RecyclerView's scrolling flags programmatically.RecyclerView recyclerView = findViewById(R.id.my_recycler_view); recyclerView.setOnTouchListener((v, event) -> true); // Disables all touch eventsDescription: Disables scrolling by overriding touch events with a listener that consumes all events.
How to disable scrolling in RecyclerView only while the user interacts with it?
RecyclerView recyclerView = findViewById(R.id.my_recycler_view); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { recyclerView.scrollBy(0, 0); // Prevents scrolling } }); Description: Uses a scroll listener to forcefully prevent scrolling.How to disable scrolling in RecyclerView while maintaining touch interaction?
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import androidx.recyclerview.widget.RecyclerView; public class CustomRecyclerView extends RecyclerView { public CustomRecyclerView(Context context) { super(context); } public CustomRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onTouchEvent(MotionEvent e) { return false; // Disables scrolling but maintains touch events } @Override public boolean onInterceptTouchEvent(MotionEvent e) { return false; // Disables scrolling but maintains touch events } } Description: Disables scrolling while still allowing other touch interactions.How to disable scrolling in RecyclerView but allow item clicks?
RecyclerView recyclerView = findViewById(R.id.my_recycler_view); recyclerView.setOnTouchListener((v, event) -> { if (event.getAction() == MotionEvent.ACTION_UP) { v.performClick(); // Handles item clicks } return true; // Consumes touch events, disables scrolling }); Description: Disables scrolling by consuming all touch events but allows item clicks.How to conditionally disable scrolling in a RecyclerView based on user actions?
RecyclerView recyclerView = findViewById(R.id.my_recycler_view); // Method to enable or disable scrolling public void setScrollingEnabled(RecyclerView recyclerView, boolean enabled) { recyclerView.setOnTouchListener((v, event) -> !enabled); } // Example usage setScrollingEnabled(recyclerView, false); // Disables scrolling Description: Dynamically controls scrolling based on a boolean flag.echarts nscalendar qfiledialog tweetstream alpha spring-cloud-feign viewgroup alexa-skills-kit calc drupal