我想在我的recyclerview中显示这些项目,但它根本没有显示,我看不到错误。也许你们可以帮助我。
MainActivity.java
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.rec);
List<MenuData> list = new ArrayList<>();
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
MenuRecAdapter menuRecAdapter = new MenuRecAdapter(list);
recyclerView.setAdapter(menuRecAdapter);
RecyclerView适配器:
public class MenuRecAdapter extends RecyclerView.Adapter<RecViewHolder>{
private List<MenuData> mList;
Activity context;
public MenuRecAdapter(List<MenuData> mList){
this.mList = mList;
}
public int getItemCount(){
return mList.size();
}
public RecViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.menuitem, viewGroup, false);
RecViewHolder pvh = new RecViewHolder(v);
return pvh;
}
public void onBindViewHolder(RecViewHolder holder, int i){
holder.menuTeXT.setText(mList.get(i).text);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
项目的ViewHolder:
public class RecViewHolder extends RecyclerView.ViewHolder {
public TextView menuTeXT;
public RecViewHolder(View itemView){
super(itemView);
menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT);
}
}
以及我想要放入我的recyclerview的数据(未显示的内容):
class MenuData {
String text;
MenuData(String text){
this.text = text;
}
private List<MenuData> list;
private void initializeData(){
list = new ArrayList<>();
list.add(new MenuData("Featured"));
list.add(new MenuData("Categories"));
list.add(new MenuData("Sell"));
list.add(new MenuData("Settings"));
list.add(new MenuData("Logout"));
}
}
提前致谢
答案 0 :(得分:52)
在MainActivity中
ArrayList<String> list = new ArrayList<>();
list.add("something1");
list.add("something2");
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.rec);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
MenuRecAdapter menuRecAdapter = new MenuRecAdapter(list);
recyclerView.setAdapter(menuRecAdapter);
RecyclerView适配器
public class MenuRecAdapter extends RecyclerView.Adapter<RecViewHolder>{
private ArrayList<String> mList = new ArrayList<>();
Activity context;
public MenuRecAdapter(ArrayList<String> mList){
this.mList = mList;
}
public int getItemCount(){
return mList.size();
}
public RecViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.menuitem, viewGroup, false);
RecViewHolder pvh = new RecViewHolder(v);
return pvh;
}
public void onBindViewHolder(RecViewHolder holder, int i){
holder.menuTeXT.setText(mList.get(i));
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
和ViewHolder保持不变......
public class RecViewHolder extends RecyclerView.ViewHolder {
public TextView menuTeXT;
public RecViewHolder(View itemView){
super(itemView);
menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT);
}
}
也摆脱了MenuData类。上面的代码应该可以正常工作。
答案 1 :(得分:1)
正如上面评论中提到的,问题可能在于未指定 layoutManager
的 RecyclerView
属性。
layoutManager
可以在 XML 文件中指定,也可以在 Java 代码中动态指定。
以上答案中的 Java 代码示例:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
要通过 XML 添加 layoutManager 使用适当的属性:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
这对我来说是固定的问题。
答案 2 :(得分:0)
由于您没有获得任何数据,我想问题是在您将列表传递到适配器时发生的。我看到你没有复制你的列表,所以你将列表的引用直接传递给适配器。我建议你试试
MenuRecAdapter menuRecAdapter = new MenuRecAdapter(new Arraylist(list));