Get RecyclerView orientation in custom view Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Get RecyclerView orientation in custom view without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

Answer

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");
     }
We are here to answer your question about Get RecyclerView orientation in custom view - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji