我在片段中实现了
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;
当我创建分隔线时,我遇到了同样的问题。 您可以根据您的
LinearLayoutManager.VERTICAL
的方向,自己输入方向,LinearLayoutManager.HORIZONTAL
或 RecyclerView
。它对我有用。
您可以在 CardView 的 XML 文件中使用下面的代码 不在recyclerView。
android:layout_marginTop =“8dp”
也许layoutmanager是一个LinearLayoutManager。这取决于片段中是否使用 LinearLayout。您可以安全地转换为 LinearLayoutManager 并检查方向。如果安全转换失败,您需要使用其他逻辑提供 LinearLayoutManager.VERTICAL 或 LinearLayoutManager.HORIZONTAL。