【发布时间】:2018-07-28 11:47:29
【问题描述】:
我正在创建一个名为ActionDialog 的AlertDialog 自定义类,它将包含一个包含按钮的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();
}
}
【问题讨论】:
-
不要创建自己的独立
List或Buttons。Buttons 正在Adapter中创建。您的List应该是您要在那些Buttons 上设置的数据。 -
好的,但是为什么呢? Button的数据没有存储?
-
我不确定你在问什么。
-
为什么我在另一个类中创建的按钮无法从适配器访问? (我指的是 Button 的数据)
-
不一定。就是两次创建
Buttons 并基本上丢弃其中的一半是没有意义的。您已经在Adapter中创建了Buttons。List中只需要设置它们的数据。这通常是Adapters 的工作方式。
标签: android android-recyclerview android-button