Search code examples
javaandroidandroid-recyclerviewandroid-custom-view

Get RecyclerView orientation in custom view


I'm trying to create a custom view extending RecyclerView. In my custom view, I have some things to do according to the RecyclerView orientation (horizontal or vertical).

Unfortunately, I don't find any way to know and get the RecyclerView orientation.

Is it even possible to do?

I know that RecyclerView orientation is defined in its LayoutManager. Maybe it is impossible to get this LayoutManager information?

Thank you in advance for any help you can provide.


Solution

  • Ok I just found the solution:

    LayoutManager.getProperties(context, attrs, 0, 0).orientation
    

    This line returns either 0 or 1 if the recyclerView is respectively HORIZONTAL or VERTICAL

    Just in case, here is my customView constructor:

         public CustomRecyclerView(Context context, AttributeSet attrs) {
                super(context, attrs);
    
                if (LayoutManager.getProperties(context, attrs, 0, 0).orientation == 0)
                    Log.d("recycler orientation", "horizontal");
                else if (LayoutManager.getProperties(context, attrs, 0, 0).orientation == 1)
                    Log.d("recycler orientation", "vertical");
         }