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

Questions:

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

linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
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.

Answer:

You can provide LayoutManager from XML layout itself:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

Alternatively, you do this from Java code:

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

Answer:

You should use this one for Vertical recycler view in android

LinearLayoutManager layoutManager = new LinearlayoutManager(this);
recyclerView.setLayoutManager(layoutManager);