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.