Home » Android » android – Should I use RecyclerView.VERTICAL instead of LinearLayoutManager.VERTICAL?

android – Should I use RecyclerView.VERTICAL instead of LinearLayoutManager.VERTICAL?

Posted by: admin June 15, 2020 Leave a comment


Running ./gradlew lint reports me an error which is confusing:

39: Must be one of: RecyclerView.HORIZONTAL, RecyclerView.VERTICAL

In the source code:

    38 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(rootView.getContext());
    39 linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    40 recyclerView.setLayoutManager(linearLayoutManager);
    41 recyclerView.setAdapter(recyclerAdapter);

Is there any reason I should change 39th line to

How to&Answers:

There is no difference in using the LinearLayoutManager.VERTICAL or RecyclerView.VERTICAL
because in LinearLayoutManager they are same.

public class LinearLayoutManager extends RecyclerView.LayoutManager implements
    ItemTouchHelper.ViewDropHandler, RecyclerView.SmoothScroller.ScrollVectorProvider {

private static final String TAG = "LinearLayoutManager";

static final boolean DEBUG = false;

public static final int HORIZONTAL = RecyclerView.HORIZONTAL;

public static final int VERTICAL = RecyclerView.VERTICAL;

As you can see in this code snippet from LinearLayoutManager.


You can provide LayoutManager from XML layout itself:

    app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

Alternatively, you do this from Java code:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);


You should use this one for Vertical recycler view in android

LinearLayoutManager layoutManager = new LinearlayoutManager(this);