我正在尝试在警报对话框内容中显示两个RecyclerViews。请帮助我做错了什么?

未调用onCreateViewHolder函数

请注意,RecyclerViews位于xml的ConstraintLayout中。如果那导致他们不显示,应该怎么做?

public class MainActivity extends AppCompatActivity {
private View inflatedView;
private RecyclerView fontList,sizeList;
private String[] fonts = {"normal","sans","serif","monospace"};
private String[] sizes = {"14","16","18","20","22","24"};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    inflatedView = getLayoutInflater().inflate(R.layout.design_style,null);

    builder.setView(inflatedView).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"OK Pressed",Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();

    fontList = inflatedView.findViewById(R.id.fontlist);
    sizeList = inflatedView.findViewById(R.id.sizelist);

    fontList.setLayoutManager(new LinearLayoutManager(this));
    fontList.setAdapter(new MyAdapter(Arrays.asList(fonts)));

    sizeList.setLayoutManager(new LinearLayoutManager(this));
    sizeList.setAdapter(new MyAdapter(Arrays.asList(sizes)));

}

private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

    private List<String> itemList;

    public MyAdapter(List<String> itemList) {
        this.itemList = itemList;
        Log.d("lol","MyAdapter");
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.design_fontcell,viewGroup);
        Log.d("lol","onCreateViewHolder");
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder myViewHolder, int i) {
        myViewHolder.mtextView.setText(itemList.get(i));
        Log.d("lol",itemList.get(i));
    }

    @Override
    public int getItemCount() {
        Log.d("lol","getItemCount: "+itemList.size());
        return itemList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView mtextView;
        MyViewHolder(View cellView){
            super(cellView);
            mtextView = cellView.findViewById(R.id.textView_font);
        }
    }
}
}

最佳答案

像这样更改代码

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    inflatedView = getLayoutInflater().inflate(R.layout.design_style,null);

     //No need of setView() for buttons
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"OK Pressed",Toast.LENGTH_SHORT).show();
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
        }
    });

    fontList = inflatedView.findViewById(R.id.fontlist);
    sizeList = inflatedView.findViewById(R.id.sizelist);

    fontList.setLayoutManager(new LinearLayoutManager(this));
    fontList.setAdapter(new MyAdapter(Arrays.asList(fonts)));

    sizeList.setLayoutManager(new LinearLayoutManager(this));
    sizeList.setAdapter(new MyAdapter(Arrays.asList(sizes)));

    builder.setView(inflatedView); //Set the view at the end

    AlertDialog dialog = builder.create();
    dialog.show();

关于java - 无法在AlertDialog中创建RecyclerView,Alert Dialog中的内容为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52623140/

10-10 20:17