PopUpWindow for a RecyclerView Item not positioned normally

Viewed 153

Ok, I will try to describe the problem as best as I can.

I have a RecyclerView and a bunch of ViewHolder items.

Each ViewHolder has a button.

My goal is, to display a PopUpWindow right under the button that I click.

I set up the listeners, and I know 100% that I am touching the item that I intended to touch.

The problem is when I touch items that are way down the RecyclerView things get really weird.

Nothing is positioned within the screen boundaries, so when I call myPopUp.showAsDropDown(myButton) so if the item that I clicked on is the 1st or 2nd one let's say (where I have 2 ViewHolders occupying the screen at one time) I'm able to see the PopUpWindow normally. But, if I click on an item that isn't visible unless if I scroll down, the PopUpWindow is getting displayed but it's off the screen boundaries, which I kinda understand why but I don't know how to fix it, is this a bug?

Any help would be appreciated, I hope I was clear, If I wasn't I will expand, thanks!

UPDATE:

I Logged the coordinates of the View item when scrolling it into the screen in the middle, it is like 1200 y-value, where my screen is only like 1400.

So I guess my question can be rephrased into, "how can I make sure that view items in the recycler view give me accurate absolute coordinates based on where they are on the screen, instead of where they are in the recycler view?

Ok so after more testing, I see that when I click on the item, it gives correct absolute coordinates, then when after scrolling upwards where the 2nd item is now the 1st (visually) I click on it, and it gives a similar absolute y-value, which is good. But when I scroll to the 3rd item it gives me the original y-value of that item which is way higher than expected.

1 Answers

The view you pass for the popup will decide the popup location. Now try with

myPopUp.showAsDropDown(myButton, x, y)

with x and y got from location calculation:

int[] originalPos = new int[2];
itemView.getLocationOnScreen(originalPos);
//or view.getLocationOnScreen(originalPos)
int x = originalPos[0];
int y = originalPos[1];
Related