Android Question

RecyclerView item's X and Y are 0 when clicked

In a RecyclerView, I'm trying to get the clicked item's X and Y position.
This is inside my adapter:

public void onBindViewHolder(AlfabetoViewHolder holder, final int position) {
// setImage(holder.imageButton, images.get(position));

holder.imageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
view.getX(); // This is 0, but it shouldn't be!
view.getY(); // This is also 0.
executaSomAnimacaoLetra(sounds.get(position), Utils.getResourceFromFile(popups.get(position), context), view); // I do some stuff here


Thing is, both X an Y always return 0, and they shouldn't. I need the X an Y of the clicked item to do some animations.

I already tried getting the X and Y position inside a OnGlobalLayoutListener and inside a OnPreDrawListener (both work when you need the X and Y on the activity's onCreate), but they also return 0.

I'm using the GridLayoutManager on my RecyclerView:

recyclerView.setLayoutManager(new GridLayoutManager(Activity_alfabeto.this, 7, LinearLayoutManager.VERTICAL, false));

Any help is appreciated.

Answer Source

Got it working. I had to use this approach:

int[] originalPos = new int[2];
//originalPos[0] is my X
//originalPos[1] is my Y

it seems getX and getY are relative to the view's parent, not their absolute position on the screen.


Could also be used.