我应该使用RecyclerView.VERTICAL而不是LinearLayoutManager.VERTICAL吗?

Joh*_*ohn 8 android android-layout android-lint android-recyclerview

运行./gradlew lint会向我报告一个令人困惑的错误:

39:必须为以下之一:RecyclerView.HORIZONTAL,RecyclerView.VERTICAL

在源代码中:

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

有什么理由我应该将第39行更改为

linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
Run Code Online (Sandbox Code Playgroud)

Muz*_*ain 8

使用LinearLayoutManager.VERTICAL或 并没有区别,RecyclerView.VERTICAL 因为LinearLayoutManager它们是相同的。

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;
Run Code Online (Sandbox Code Playgroud)

如您所见,该代码段来自LinearLayoutManager


Chi*_*oni 1

您可以LayoutManager从 XML 布局本身提供:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
Run Code Online (Sandbox Code Playgroud)

或者,您可以通过 Java 代码执行此操作:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
Run Code Online (Sandbox Code Playgroud)