如果你使用RecyclerView在Android Studio中创建了一个列表,但是没有看到任何项出现,可能是由于以下原因:
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); recyclerView.setLayoutManager(layoutManager);
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();
}
}
android:layout_height="wrap_content"
或
android:layout_height="50dp" (自定义高度)
通过解决这些问题,你应该能够正确显示RecyclerView中的项。