looping-layout icon indicating copy to clipboard operation
looping-layout copied to clipboard

Is there a findFirstVisibleItemPosition() funtion?

Open khrsantiago opened this issue 3 years ago • 9 comments

:key: Use case

I want to use this funtion to find the item position in the center of my Horizontal RecyclerView

:hammer: Proposal

N/A

:wrench: Alternative

N/A

:memo: Other info

I'm using a custom smoothScroller to center the items: https://stackoverflow.com/a/53756296)

:bee: Request for assignment

If possible, I would like to implement this.
I'm just requesting this feature. I don't want to implement it.

khrsantiago avatar Nov 19 '21 14:11 khrsantiago

Hello, thank's for putting up a feature request! It's awesome to see people using the library :D

I want to make sure I'm understanding your request correctly. Your smooth scroller is already working, and scrolling items to the center of the RecyclerView's layout? Now you just want to find the adapter index associated with the item at the center of the RecyclerView's layout?

BeksOmega avatar Nov 19 '21 15:11 BeksOmega

Hello, thanks to you for creating this library, it saved me a lot problems, because I was trying to implement a RecyclerView with loop.

  1. Yes, it's working and center to the RecyclerView.
  2. Yes, I can find the position when I click on the item, but not when I scroll to the item.

I'm going to add a GIF with the RecyclerView https://user-images.githubusercontent.com/82970886/142652409-205eb5e9-73fd-46b7-911f-8ed49e9cbcbd.mp4

khrsantiago avatar Nov 19 '21 15:11 khrsantiago

I was traying to do somthing like this: https://github.com/adityagohad/HorizontalPicker But this library dosen't have loop.

khrsantiago avatar Nov 19 '21 19:11 khrsantiago

Hmm @IndustriasBJT do you think taking the average of the topLeftIndex and the bottomRightIndex could work for you?

BeksOmega avatar Nov 21 '21 15:11 BeksOmega

I think so, how can I do that?

khrsantiago avatar Nov 22 '21 14:11 khrsantiago

You should just be able to do:

val midIndex = (myLayoutManager.topleftIndex + myLayoutManager.bottomRightIndex) / 2

If that doesn't work give me a ping and we can try to work out a different solution =)

BeksOmega avatar Nov 22 '21 21:11 BeksOmega

It work!, but I made some changes:

Java Code:

int topLeftIndex = layoutManager.getTopLeftIndex();
int bottomRightIndex = layoutManager.getBottomRightIndex();
int centerIndex = (topLeftIndex + bottomRightIndex) / 2;

if (topLeftIndex == dataList.size() - 1) centerIndex = 0; // To get the first element
if (bottomRightIndex == 0) centerIndex = dataList.size() - 1; // To get the last element

Toast.makeText(context,
    "Left: " + topLeftIndex +
    "\nRight: " + bottomRightIndex +
    "\nCenter: " + centerIndex  , /* Item Position */
    Toast.LENGTH_SHORT).show();

khrsantiago avatar Nov 23 '21 20:11 khrsantiago

It work!, but I made some changes:

Java Code:

int topLeftIndex = layoutManager.getTopLeftIndex();
int bottomRightIndex = layoutManager.getBottomRightIndex();
int centerIndex = (topLeftIndex + bottomRightIndex) / 2;

if (topLeftIndex == dataList.size() - 1) centerIndex = 0; // To get the first element
if (bottomRightIndex == 0) centerIndex = dataList.size() - 1; // To get the last element

Toast.makeText(context,
    "Left: " + topLeftIndex +
    "\nRight: " + bottomRightIndex +
    "\nCenter: " + centerIndex  , /* Item Position */
    Toast.LENGTH_SHORT).show();

dataList it's a simple ArrayList of numbers, and I want to know is there any way to use any funtion like onStopClickListerner o onItemClikckListener, because I get the position of the element by clicking on the TextView of the adapter class.

BTW: Thanks for the solution 😃

khrsantiago avatar Nov 23 '21 20:11 khrsantiago

Heya @IndustriasBJT :D Happy to help! I'm glad you found a solution that works for you. :sunflower:

I want to know is there any way to use any funtion like onStopClickListerner o onItemClikckListener, because I get the position of the element by clicking on the TextView of the adapter class.

I'm not sure if I'm understanding your question correctly. Are you asking if the LoopingLayoutManager supports those functions?

BeksOmega avatar Nov 23 '21 20:11 BeksOmega