google / flexbox-layout

Flexbox for Android
Apache License 2.0
18.24k stars 1.79k forks source link

Question: How to get the row/line for a view at certain position #565

Open omhack opened 3 years ago

omhack commented 3 years ago

How to get the row/line for a desired view

Is there a way to get a selected view's row/line number?, I want to know what line a view at certain position belongs to.

ruslansharipov commented 3 years ago

It's a very good question. I was struggling with it when tried to create a proper ItemDecoration. Maybe someone knows convenient way of getting the line number, line length and the position in line?

I found package-private method that is used by FlexboxItemDecoration to find out the line number.

int getPositionOnFlexLineIndex(int position)

But it is package-private and can't be used from the outside.

0-99 commented 2 years ago

I found a (dirty) solution for this problem without modifying the classes of this repo manually: In your custom project, create a package with the same package name of FlexboxItemDecoratiom. Then create a new class in this package, for example "PackageProtectionCaller.java". In this class you can define static public methods and can access the package protected methods and variables.

Not a smooth solution, but sometimes the only way to handle data.