我是使用recycviewview的新手,将它与cardview一起使用,有人可以告诉我如何初始化它吗?
我看到我必须以某种方式将它放在onCreateView中,而不是在ViewCreated中,我的代码:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_genres, container, false);
Run Code Online (Sandbox Code Playgroud)
以及我在Activity上使用的回收器代码,但现在我想在片段中触发它
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
Run Code Online (Sandbox Code Playgroud)
数据痕迹:
JSON>get>DataModel>Adapter>Recycler>CardView
Run Code Online (Sandbox Code Playgroud)
当我在onCreatedView中使用它时,LogCat说:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
Run Code Online (Sandbox Code Playgroud)
像这样
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_genres, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return v;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4195 次 |
| 最近记录: |