2

我有一个包含 cardViews 的 recyclerView,每个 cardView 都包含一个按钮。我想在按下一个按钮时禁用剩余的按钮。我该怎么做?

我尝试在列表中添加按钮以禁用它们,但我没有成功。

这是我的适配器:

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

private LayoutInflater inflater;
List<AgendaInfo> data = Collections.emptyList();

public MyAdapter(Context context, List<AgendaInfo> data) {

    inflater = LayoutInflater.from(context);

    this.data = data;

}

private void showAlert(final Context context, String message, final Button btn) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(message)
            .setTitle("alert")
            .setIcon(R.drawable.alert)
            .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handleButton(btn, context);
                }
            })
            .setNegativeButton("no", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .create().show();
}

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.cards, parent, false);

    MyViewHolder holder = new MyViewHolder(view);

    return holder;

}

@Override
public void onBindViewHolder(final MyAdapter.MyViewHolder holder, final int position) {

    AgendaInfo current = data.get(position);
    holder.code.setText(current.code);
    holder.order.setText(current.order);
    holder.standardTime.setText(current.standardTime);
    holder.btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showAlert(holder.code.getContext(), "start No : " + (position + 1), holder.btn);
        }
    });
}

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

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView code;
    TextView order;
    TextView standardTime;
    Button btn;

    public MyViewHolder(final View itemView) {
        super(itemView);
        code = (TextView) itemView.findViewById(R.id.tv_card_code);
        order = (TextView) itemView.findViewById(R.id.tv_card_order_count);
        standardTime = (TextView) itemView.findViewById(R.id.tv_card_standard_time);
        btn = (Button) itemView.findViewById(R.id.btn_card);

    }

}

public void handleButton(final Button btn, final Context c)
{

    btn.setBackgroundColor(c.getResources().getColor(R.color.colorLightGreen));
    btn.setText("discard");

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            btn.setText("started");
            btn.setBackgroundColor(c.getResources().getColor(R.color.colorGray));
            btn.setEnabled(false);
        }
    }, 2000);
}

}

任何帮助将不胜感激。

4

0 回答 0