Get x/y positions of nth item in RecyclerView

前端 未结 2 1486
不知归路
不知归路 2020-12-16 13:51

I would like to know how to get the X or Y position of nth item in RecyclerView. This is easy to do if the item is currently within the visible range. However, when the view

相关标签:
2条回答
  • 2020-12-16 14:33

    Pal gajjar's answer is correct... You can not get the coordinates of recycled views by the simple reason they aren't rendered yet, they haven't been created at this point. You have to check if the view exist in the first place using recyclerview.getChildAt(intValueHere)...if it's different from null it means its close to the currently displayed child position and its partially rendered holded in memory...at this point, and only at this point in which views are holded in memory you're able to use the view. You should read how a recyclerview works and how the layout manager recycle the views to understand what's really going on... -> https://developer.android.com/guide/topics/ui/layout/recyclerview

    0 讨论(0)
  • 2020-12-16 14:42

    I got perfect x and y of every item in recycler view using following:

    int[] originalPos = new int[2];
    view.getLocationInWindow(originalPos);
    //or view.getLocationOnScreen(originalPos)
    int x = originalPos[0];
    int y = originalPos[1];
    
    0 讨论(0)
提交回复
热议问题