1

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 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

it worked with ScrollToPosition but not work with smothScrollToPosition . do you know why?
1

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

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.