1

I am trying to create a list to scroll on either direction horizonally. Based on the solutions on How do I create a circular (endless) RecyclerView? I was able to make it go horizontally to the right endlessly but that does not work if I go on the left.

public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
   firstVisibleItemPos = ListLayoutManager.findFirstVisibleItemPosition();
 if (firstVisibleItemPos != 0 && firstVisibleItemPos % items.size() == 0) {


      recyclerView.getLayoutManager().scrollToPosition(firstVisibleItemPos % 
      items.size());
    }else if(firstVisibleItemPos == 0 && items.size() > 0){
       int newPos =  items.size() / 2;
       recyclerView.getLayoutManager().scrollToPosition(newPos);
    }

}



@Override
public int getItemCount() {
    return Items== null ? 0 : Items.size() * 2;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
            position = position % categoryViewItems.size();
           ... some code for displaying here

}

I tried using Integer.MAX_VALUE but that resulted in wrong positions when I click on the item.

Is there a way that i go on the left 0...n-5,n-4, n-3,n-2, n-1,n, 0,1,2,3,4,5,...n

****************EDIT ************************

Tried

@Override
public int getItemCount() {
     return Items== null ? 0 : Integer.MAX_VALUE;
}

public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    firstVisibleItemPos = 
    ListLayoutManager.findFirstVisibleItemPosition();
    if (firstVisibleItemPos != 0 && firstVisibleItemPos % items.size() == 0) 
      {

          recyclerView.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2);

     }else if(firstVisibleItemPos == 0 && items.size() > 0){


        recyclerView.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2);
     }

 }



  itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Item selectedItem = Items.get(getAdapterPosition());
                    currentlySelectedItem = selectedItem ;

                    notifyDataSetChanged();
                } catch (ArrayIndexOutOfBoundsException e) {
                    e.printStackTrace();
                } catch (Exception exc) {
                    exc.printStackTrace();
                }
            }
        });
    }

getAdapterPosition() is returning the wrong position, onBindViewHolder() is still usign the same logic for position.

Prens
  • 420
  • 1
  • 5
  • 15
  • In your first `if`-block the expression `.scrollToPosition(firstVisibleItemPos % items.size());` can be simplified to `.scrollToPosition(0);`. If I understand the linked solution correctly, your `newPos` should be something big like `Integer.MAX_VALUE / 2`. Did you try that? You did not describe where exactly you used `Integer.MAX_VALUE`. – hamena314 May 30 '18 at 08:03
  • yes I did try Integer.MAX_VALUE / 2 but when I click on the item its not giving me the right position in the array – Prens May 30 '18 at 16:36

1 Answers1

1

I was able to solve this by adding onClickListner in onBindViewHolder(). This returned the right position when compared to in viewholder and using getAbsoluteposition().

Prens
  • 420
  • 1
  • 5
  • 15