我想在底部导航片段中实现RecyclerView,其中包含带有TextView和ImageView的CardView.我在每张打开新活动的卡上都有一个onClickListener.但我的recyclerview没有显示任何东西,并给出零点异常Logcat.我不知道我在这里缺少什么.
模型类
class Data {
public int imageId;
public String txt;
Data( int imageId,String text) {
this.imageId = imageId;
this.txt = text;
}
}
RecyclerView适配器
public class RecyclerViewAdapter extends RecyclerView.Adapter
主要活动
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
private Listrequired empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_status,container,false);
recyclerView = rootView.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
ArrayList
最佳答案
您没有调用RecyclerViewAdapter构造函数:
原文链接:https://www.f2er.com/android/430195.html通过将此行添加到Fragment adapter = new RecyclerViewAdapter(getActivity(),data);
RecyclerViewAdapter adapter;
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
adapter = new RecyclerViewAdapter (getActivity(),data);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootView;