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

10

运行./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);

我应该将第39行更改为什么的原因是什么?

linearLayoutManager.setOrientation(RecyclerView.VERTICAL);

3
有理由让我更改第39行吗?——我猜,如果你想要摆脱(有问题的)lint警告,那么没有。它们是相同的值。实际上,LinearLayoutManager的字段被设置为RecyclerView的字段:https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/recyclerview/recyclerview/src/main/java/androidx/recyclerview/widget/LinearLayoutManager.java#52。或者,你可以选择忽略这个警告。如果你把光标放在那一行上,然后按下alt-enter,应该会出现“Suppress...”选项。 - Mike M.
3个回答

10

使用 LinearLayoutManager.VERTICALRecyclerView.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;

正如您在这个代码片段中所看到的 LinearLayoutManager


3
您应该在Android中使用此选项卡来垂直滚动视图。
LinearLayoutManager layoutManager = new LinearlayoutManager(this);
recyclerView.setLayoutManager(layoutManager);

1
您可以直接在XML布局中提供LayoutManager
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接