安卓回收商视图不显示项目

2022-09-01 16:58:16

我想在我的回收商视图中显示这些项目,但它根本没有显示,我看不到错误。也许你们可以帮我。

主要活动.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);

回收器查看适配器:

    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);
    }

}

项目视图持有者:

  public class RecViewHolder extends RecyclerView.ViewHolder {

    public TextView menuTeXT;

    public RecViewHolder(View itemView){
        super(itemView);

        menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT);
    }
}

以及我想放入回收器视图的数据(未显示的数据):

    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"));
    }

}

提前致谢


答案 1

在主活动中

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);

回收器查看适配器

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类。上面的代码应该工作正常。


答案 2

正如上面在注释中提到的,问题可能在于未指定 的属性。layoutManagerRecyclerView

可以在 XML 文件中指定,也可以在 Java 代码中动态指定。layoutManager

上面答案中的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"/>

这对我来说解决了问题。


推荐