Ryan Newquist
Ryan Newquist

Reputation: 153

LinearLayoutManager cannot resolve getOrientation()

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;
}

Upvotes: 15

Views: 6195

Answers (4)

Imre Nuijen
Imre Nuijen

Reputation: 1

Maybe the layoutmanager is a LinearLayoutManager. It depends on if LinearLayout is used in the fragment. You can safe cast to LinearLayoutManager and check the orientation. If the safe cast fails you need to supply LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL using other logic.

Upvotes: 0

Nasir
Nasir

Reputation: 1

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

android:layout_marginTop="8dp

Upvotes: 0

Zin Win Htet
Zin Win Htet

Reputation: 2575

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.

Upvotes: 9

Marcin Mrugas
Marcin Mrugas

Reputation: 1064

Change

private RecyclerView.LayoutManager mLayoutManager;

to

private LinearLayoutManager mLayoutManager;

Upvotes: 22

Related Questions