14

I have a RecyclerView implemented in a fragment and I'm trying to add dividers to the code. I have the RecylcerView working as intended, but the LinearLayoutManager cannot resolve the getOrientation() function.

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;
}
nynohu
  • 1,580
  • 11
  • 12
Ryan Newquist
  • 143
  • 1
  • 4
  • 1
    I was under the assumption that because I was able to initialize mLayoutManager as a LinearLayoutManager that I would be able to use its functions. Changing RecyclerView.LayoutManager mLayoutManager to LinearLayoutManager mLayoutManager resolved the issue. – Ryan Newquist Jan 10 '17 at 00:05
  • 1
    LayoutManager does not declare a getOrientation() method, LinearLayoutManager does. – BladeCoder Feb 19 '17 at 01:37

3 Answers3

21

Change

private RecyclerView.LayoutManager mLayoutManager;

to

private LinearLayoutManager mLayoutManager;
Marcin Mrugas
  • 649
  • 6
  • 13
9

I got the same issue when I create a divider. You can simply put the orientation yourself, LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL, according to the orientation your RecyclerView. It works for me.

Zin Win Htet
  • 2,142
  • 4
  • 26
  • 44
0

You can use code below at CardView at XML file Not at recyclerView.

android:layout_marginTop="8dp

Nasir
  • 1