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

时间:2018-12-10 05:49:24

标签: android android-layout android-recyclerview android-lint

正在运行。/gradlew皮棉报告了一个令人困惑的错误:

  

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 个答案:

答案 0 :(得分:6)

使用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的代码段中所见。

答案 1 :(得分:1)

您应该在Android的立式回收机视图中使用此

  

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

答案 2 :(得分:0)

您可以从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);