当Dialog被解除时,更改drawable ImageView OnClick

时间:2018-04-15 07:45:00

标签: android android-imageview android-dialog

我有一个使用ImageView显示图像的自定义ListView。 当我点击图像时,我显示对话框。我想在点击图像时更改图像ImageView的图像但是在Dialog被解除后。

        @Override
        public void onClick(View v) {
            ImageView imageview = (ImageView) v.findViewById(R.id.imageview);
            int position;
            switch (v.getId()) {
                case R.id.imageview:
                    position=(Integer)v.getTag();
                        imageview.setImageResource(R.drawable.image2);
                    break;
                }
        }

        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
                case dialog:
                    pDialog = new ProgressDialog(this);
                    pDialog.setTitle("Hello");
                    pDialog.setCancelable(true);
                    pDialog.show();
                    return pDialog;
                default:
                    return null;
            }
        }

2 个答案:

答案 0 :(得分:0)

我没有关于您的代码的大量信息,但通常您可以使用类似的内容

class Adapter extends RecyclerView.Adapter<MyViewHolder> {

    List<MyDataModel> items = new ArrayList<>();

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setUp(items.get(position));
    }

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

class MyViewHolder extends RecyclerView.ViewHolder {

    ImageView someImageView;

    public MyViewHolder(View itemView) {
        super(itemView);
    }

    public void setUp(MyDataModel data) {

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Initialize your dialog properties
                ProgressDialog dialog;

                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {

                        data.isDismissed = true;

                        yourRecyclerViewAdapterInstance.notifyItemChanged(getAdapterPosition());

                    }
                });

            }
        });

        if (data.isDismissed) {
            someImageView.setImageResource(data.afterDismissImage);
        } else someImageView.setImageResource(data.normalImage);
    }
}

class MyDataModel {

    int normalImage;
    int afterDismissImage;
    boolean isDismissed = false;
}

答案 1 :(得分:0)

pDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        ImageView imageview = (ImageView)v.findViewById(R.id.imageview);
        imageview.setImageResource(R.drawable.image2); 
adapter.notify();
    }
});

更改图像后应通知适配器