【问题标题】:How to create a RecyclerView of Buttons如何创建按钮的 RecyclerView
【发布时间】:2018-07-28 11:47:29
【问题描述】:

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

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

这是结果。

我将在这里留下我的ActionDialog 类代码和适配器类。

这是ActionDialog类:

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 适配器类:

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

}

【问题讨论】:

  • 不要创建自己的独立 ListButtons。 Buttons 正在 Adapter 中创建。您的List 应该是您要在那些Buttons 上设置的数据。
  • 好的,但是为什么呢? Button的数据没有存储?
  • 我不确定你在问什么。
  • 为什么我在另一个类中创建的按钮无法从适配器访问? (我指的是 Button 的数据)
  • 不一定。就是两次创建Buttons 并基本上丢弃其中的一半是没有意义的。您已经在 Adapter 中创建了 Buttons。 List 中只需要设置它们的数据。这通常是Adapters 的工作方式。

标签: android android-recyclerview android-button


【解决方案1】:

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

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

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

设置文本

holder.actionButton.setText(// 这里利用位置);

或者点击监听器。

更新

您可以查看谷歌样本以获取 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 是字符串数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2011-07-03
    • 2014-05-21
    • 2011-07-24
    相关资源
    最近更新 更多