AndroidStudio中的RecyclerView不显示条目
创始人
2024-08-20 19:30:13
0

如果你使用RecyclerView在Android Studio中创建了一个列表,但是没有看到任何项出现,可能是由于以下原因:

  1. 没有设置LayoutManager,因为RecyclerView需要一个LayoutManager才能知道如何排列列表项。例如,可以使用LinearLayoutManager来获得垂直滚动列表:

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); recyclerView.setLayoutManager(layoutManager);

  1. 数据集为空,RecyclerView需要一个Adapter来提供数据并创建视图,如果没有提供数据集,它将无法渲染任何内容。例如:

public class MyAdapter extends RecyclerView.Adapter {

private ArrayList mDataset;

public MyAdapter(ArrayList dataset) {
    this.mDataset = dataset;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View contactView = inflater.inflate(R.layout.item_contact, parent, false);

    // Return a new holder instance
    MyViewHolder viewHolder = new MyViewHolder(contactView);
    return viewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    // Get the data model based on position
    String name = mDataset.get(position);

    // Set item views based on your views and data model
    TextView textView = holder.nameTextView;
    textView.setText(name);
}

@Override
public int getItemCount() {
    return mDataset.size();
}

}

  1. Item的高度为0,该问题通常是因为item的高度没有正确配置,可以通过确保item布局高度为wrap_content或使用固定高度解决:

android:layout_height="wrap_content"

android:layout_height="50dp" (自定义高度)

通过解决这些问题,你应该能够正确显示RecyclerView中的项。

相关内容

热门资讯

2分钟辅助!hhpoker透视... 2分钟辅助!hhpoker透视脚本,wepoker有透视吗,教程书教程(有挂头条)1、超多福利:超高...
第七分钟辅助!wepoker透... 第七分钟辅助!wepoker透视app下载,aa poker辅助包,讲义教程(有挂分享)一、wepo...
第八分钟辅助!wepoker怎... 第八分钟辅助!wepoker怎么开辅助,xpoker辅助器,模块教程(的确有挂)1、不需要AI权限,...
一分钟辅助!wepoker有没... 一分钟辅助!wepoker有没有挂,pokemmo脚本辅助器,讲义教程(有挂教学)1、完成pokem...
第一分钟辅助!hhpoker是... 第一分钟辅助!hhpoker是真的还是假的,wepoker辅助器,阶段教程(有挂技巧)1、金币登录送...
8分钟辅助!hhpoker辅助... 8分钟辅助!hhpoker辅助挂,wejoker私人辅助软件,窍要教程(有挂分析)1、玩家可以在we...
第6分钟辅助!xpoker透视... 第6分钟辅助!xpoker透视辅助,wejoker内置辅助,窍门教程(有挂秘诀)该软件可以轻松地帮助...
第六分钟辅助!aapoker公... 第六分钟辅助!aapoker公共底牌,aapoker怎么设置抽水,办法教程(真是有挂)1、在aapo...
9分钟辅助!wepoker辅助... 9分钟辅助!wepoker辅助真的假的,aapoker透视脚本下载,手册教程(有挂解密)1、首先打开...
第7分钟辅助!wepoker底... 第7分钟辅助!wepoker底牌透视脚本,wepoker黑侠破解,模板教程(揭秘有挂)1、wepok...