在RecyclerView中获取第n项的x/y位置

Res*_*ace 12 android android-recyclerview

我想知道如何在RecyclerView中获取第n项的X或Y位置.如果项目当前在可见范围内,这很容易做到.但是,当视图当前不在屏幕上时(例如,RecyclerView上的当前可见项目范围是4到7,而我感兴趣的项目是,例如,1或10),似乎没有办法得到它值.

这是我尝试过的.我有以下三个字段:

private RecyclerView rv;

/* The Layout Manager of rv */
private LinearLayoutManager llm;

/* The data to be shown in the RecyclerView. */
private String[] data = {"0","1","2",...,"9"};
Run Code Online (Sandbox Code Playgroud)

我在屏幕上显示"2"到"5"的项目,我想获得项目"9"的X/Y位置,该项目当前不在屏幕上.所以我尝试了以下方法:

int index = 9;
View v = rv.getChildAt(index);
View v2 = llm.getChildAt(index);
Run Code Online (Sandbox Code Playgroud)

不幸的是,vv2为空.原因似乎是当大小data为10时,childCount为rvllm屏幕上当前可见的视图数量,如下所示:

int count = rv.getChildCount();
int count2 = llm.getChildCount();
Run Code Online (Sandbox Code Playgroud)

两个变量都是4(当前可见的视图数量;即"2"到"5").由于recyclerView的大小是4,并且我想在索引9处查找项目,因此我在上面返回null.

pal*_*jar 26

我得到了完美xy使用按照回收视图中的每个项目:

int[] originalPos = new int[2];
view.getLocationInWindow(originalPos);
//or view.getLocationOnScreen(originalPos)
int x = originalPos[0];
int y = originalPos[1];
Run Code Online (Sandbox Code Playgroud)