I have 1 recycler view with each item is full screen . in each item I have a button "NEXT". I don't want user to be able to swipe on recycler view to scroll to new item but can only click on next button to go to next item. Can anyone help me on this issue. or any other solution than recycler view. I tried some ways like set layoutManager canScrollHorizontal to False but it also can't scroll to next item when I click "NEXT" button. tks
3 Answers
Add Custom LinearLayoutManager to your RecyclerView
Custom LinearLayoutManager Code:
public class RecyLinearLayoutManager(var context: Context?) : LinearLayoutManager(context) {
private var isHorizontalScrollEnabled = false
private var isVerticalScrollEnabled = false
fun setHorizontalScrollEnabled(isHorizontalScrollEnabled: Boolean) {
this.isHorizontalScrollEnabled = isHorizontalScrollEnabled
}
override fun canScrollHorizontally(): Boolean {
return isHorizontalScrollEnabled && super.canScrollHorizontally()
}
fun setVerticalScrollEnabled(isVerticalScrollEnabled: Boolean) {
this.isVerticalScrollEnabled = isVerticalScrollEnabled
}
override fun canScrollVertically(): Boolean {
return isVerticalScrollEnabled && super.canScrollVertically()
}
}
Set Layout Manager to Recycler View:
val linearLayoutManage = RecyLinearLayoutManager(this)
linearLayoutManage.setHorizontalScrollEnabled(false);
linearLayoutManage.setVerticalScrollEnabled(false)
recyPdf.layoutManager = linearLayoutManage
Add this code on Click on Next Button
val mLayoutManger = recyPdf.layoutManager as RecyLinearLayoutManager
//To get the total Number of items visible on the screen
val visibleItemCount = mLayoutManger.childCount
if(currentItemVisi == 0){
currentItemVisi = visibleItemCount - 1
}
currentItemVisi = (currentItemVisi + visibleItemCount) - 1
//To get the total items loaded in the RecyclerView
val totalItemCount = mLayoutManger.itemCount
if(currentItemVisi<totalItemCount){
linearLayoutManage.scrollToPosition(currentItemVisi-1)
}
Note: recyPdf is My RecyclerView Name, You can change position in scrollToPosition according to your requirement.
1 Comment
If they're fullscreen items (and it sounds like you're moving horizontally too) you probably want a ViewPager instead. Here's an example that shows you how to get nice animations too:
https://developer.android.com/training/animation/screen-slide-2
ViewPager2 (the one used in the example) also has a setUserInputEnabled method, you can set that to false so the user can't swipe it
Comments
Try this code to disable scroll by user:
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
Another solution can be as you described, but you can temporarily enable scroll when user click Next
public class MyLayoutManager extends LinearLayoutManager {
private boolean isScrollEnabled = true;
public MyLayoutManager (Context context) {
super(context);
}
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
@Override
public boolean canScrollHorizontally() {
return isScrollEnabled && super.canScrollHorizontally();
}
}
Then call setScrollEnabled when you need