如何创建按钮的 RecyclerView

How to create a RecyclerView of Buttons

我正在创建一个名为 ActionDialogAlertDialog 自定义 class,它将包含一个包含按钮的 RecyclerView。我有一个按钮列表,我在自定义 class ActionDialog 中填充(现在我只是填充无用的 Button 只是为了尝试使用它,除了我在另一个 [= =42=]).

问题是,当我创建 AlertDialog 时,所有按钮都显示为空,但没有 text/no clicklistener(如下图所示)。 (我在另一个 class 中的 Button 添加了自定义 ActionListener,然后将其作为 ActionDialog class 中的参数。它会丢失 ActionListener?)

这是结果。

我会在这里留下我的 ActionDialog class 代码和适配器 class.

这是ActionDialog class:

public class ActionDialog extends AlertDialog{

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private Button actionButtons;
private List<Button> buttons;
private Activity context;

public ActionDialog(@NonNull Activity context, Button actionButtons) {
    super(context);

    this.context = context;
    this.actionButtons = actionButtons;
    buttons = new ArrayList<>();

    initButton();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //requestWindowFeature(Window.FEATURE_NO_TITLE);



}

private void initButton(){

    initZoneButton();

    //TODO init all buttons

    Button b1 = new Button(context);
    b1.setText("ExampleButton1");

    Button b2 = new Button(context);
    b2.setText("ExampleButton2");
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String a;
        }
    });

    buttons.add(b1);
    buttons.add(b2);
}

private void initZoneButton(){

    buttons.add(actionButtons); //this button is created in another class and give as parameter in this class

    }


public void createDialog(){

    Builder mBuilder = new Builder(context);
    View view = context.getLayoutInflater().inflate(R.layout.dialog_actionbuttons_layout, null);

    mRecyclerView = view.findViewById(R.id.dialog_actionbuttons_rv);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(context);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new ActionButtonsAdapter(buttons);

    mRecyclerView.setAdapter(mAdapter);

    mBuilder.setView(view);
    mBuilder.create().show();

}
}

这是 RecyclerView 适配器 class:

public class ActionButtonsAdapter extends RecyclerView.Adapter<ActionButtonsAdapter.ViewHolder>{

private List<Button> dataButtons;

static class ViewHolder extends RecyclerView.ViewHolder {

    Button actionButton;


    ViewHolder(View v) {
        super(v);

        actionButton = v.findViewById(R.id.action_button_rv);

    }

}

public ActionButtonsAdapter(List<Button> dataButtons){

    this.dataButtons = dataButtons;

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder.actionButton = dataButtons.get(position);
    //i think the problem is here, maybe
}

@Override
public ActionButtonsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_actionbutton_layout, parent, false);
    return new ViewHolder(v);

}

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

}

我认为在 onBindViewHolder 方法中你应该用你的按钮做任何你想做的事。

这里也不需要按钮列表。列出您需要保存在 Buttons RecyclerView 中的数据。

我有一个 RecyclerView 可以显示餐厅的流派,所以我将创建一个字符串列表来保存这些流派名称(鸡肉、肉类等..)

设置其文本

holder.actionButton.setText(// Make use of position here);

或者点击听众。

更新

您可以检查 google 个 recyclerview 样本 here

@Override

    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        Log.d(TAG, "Element " + position + " set.");

        // Get element from your dataset at this position and replace the contents of the view
        // with that element
        viewHolder.getTextView().setText(mDataSet[position]);
}

其中 mDataset 是字符串数组。