我在一个片段中实现了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;
}

最佳答案

改变

private RecyclerView.LayoutManager mLayoutManager;


private LinearLayoutManager mLayoutManager;

10-07 22:37