LinearLayoutManager无法解析getOrientation()

时间:2017-01-09 23:57:57

标签: android android-recyclerview linearlayoutmanager

我在片段中实现了RecyclerView,我试图在代码中添加分隔符。我RecylcerView按预期工作,但LinearLayoutManager无法解析getOrientation()功能。

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View root = (View) inflater.inflate(R.layout.fragment_settings, null);
    getActivity().setTitle("Settings");

    mRecyclerView = (RecyclerView) root.findViewById(R.id.recycler_view_settings);

    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(getContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new SettingsAdapter(getResources().getStringArray(R.array.setting_list));
    mRecyclerView.setAdapter(mAdapter);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), mLayoutManager.getOrientation());
    mRecyclerView.addItemDecoration(dividerItemDecoration);
    return root;
}

3 个答案:

答案 0 :(得分:16)

更改

private RecyclerView.LayoutManager mLayoutManager;

private LinearLayoutManager mLayoutManager;

答案 1 :(得分:5)

创建分隔符时遇到了同样的问题。 您可以根据LinearLayoutManager.VERTICAL的方向自行设置方向LinearLayoutManager.HORIZONTALRecyclerView。它对我有用。

答案 2 :(得分:0)

您可以在CardView的XML文件中使用以下代码 不在recyclerView。

android:layout_marginTop =“ 8dp