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;
}
LinearLayoutManager Arranges items in 1 column or 1 row based on orientation. For example, a horizontal LinearLayoutManager will place items from left to right in 1 row. GridLayoutManager Arranges items in a provided number of rows and columns, much like images in the gallery.
A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.
A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results. While LayoutParams belong to the LayoutManager , ViewHolders belong to the adapter.
Change
private RecyclerView.LayoutManager mLayoutManager;
to
private LinearLayoutManager mLayoutManager;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With