我在片段中实现了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;
}
答案 0 :(得分:16)
更改
private RecyclerView.LayoutManager mLayoutManager;
到
private LinearLayoutManager mLayoutManager;
答案 1 :(得分:5)
创建分隔符时遇到了同样的问题。
您可以根据LinearLayoutManager.VERTICAL
的方向自行设置方向LinearLayoutManager.HORIZONTAL
或RecyclerView
。它对我有用。
答案 2 :(得分:0)
您可以在CardView的XML文件中使用以下代码 不在recyclerView。
android:layout_marginTop =“ 8dp