android – 将图像从drawable设置为recyclerview

前端之家收集整理的这篇文章主要介绍了android – 将图像从drawable设置为recyclerview前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想在底部导航片段中实现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

ScreenShot

最佳答案
您没有调用RecyclerViewAdapter构造函数

通过将此行添加到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;
原文链接:https://www.f2er.com/android/430195.html

猜你在找的Android相关文章