10

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);
John
  • 1,139
  • 3
  • 16
  • 33
  • 3
    "Is there any reason I should change 39th line" – I guess, if you want to get rid of the (faulty) lint warning. They're the same value. In fact, `LinearLayoutManager`'s field is set to the `RecyclerView` field: https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/recyclerview/recyclerview/src/main/java/androidx/recyclerview/widget/LinearLayoutManager.java#52. Alternatively, you could just suppress the warning. If you put your cursor on that line, and hit alt-enter, there should be a "Suppress..." option. – Mike M. Dec 10 '18 at 05:55

3 Answers3

10

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.

Muzammil Husnain
  • 1,218
  • 1
  • 10
  • 24
3

You should use this one for Vertical recycler view in android

LinearLayoutManager layoutManager = new LinearlayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
Tejas Shukla
  • 144
  • 7
1

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);
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174