ecyclerView的onBindViewHolder内部调用

ecyclerView的onBindViewHolder内部调用

本文介绍了在RecyclerView的onBindViewHolder内部调用的自定义对话框按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除自定义对话框"的OnClick并更新从RecyclerView单击的项目.

I'm trying to have the OnClick of the Custom Dialog remove and update the item clicked from RecyclerView.

    final Dialog rightDialog = new Dialog(context1);
    ...
    TextView textOKRight = (TextView) rightDialog.findViewById(R.id.text_ok);
    textOKRight.setText("NEXT");
    textOKRight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
     // MainAdapter.ViewHolder ccd = new MainAdapter.ViewHolder(itemView, listener);
     // ccd.removeCaller();
        rightDialog.dismiss();
        }
    });
    rightDialog.show();

MainAdapter

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder>{
..
   private static ViewHolder.IndividualClickListener mListenerCallback;
   private static ViewHolder.IndividualClickListener mListener;
   private MainAdapter mMyAdapter;
   public AnimatorSampleActivity animatorActivity;

public MainAdapter(Context context, ArrayList<Question> dataSet) {
    mContext = context;
    questions = dataSet;
    ...
}


public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

    public IndividualClickListener mListener;
    ...

    public ViewHolder(View itemView, IndividualClickListener listener) {
        super(itemView);
        mListener = listener;
        image = (ImageView) itemView.findViewById(R.id.imageMic);
        imageOne = (ImageView) itemView.findViewById(R.id.imageOne);
        text = (TextView) itemView.findViewById(R.id.text);
        itemView.setOnClickListener(this);
        image.setOnClickListener(this);
    }

    //////THIS IS THE METHOD I WOULD LIKE TO CALL FROM THE DIALOG///////
    public void removeCaller() {
        mListener.remove1(getLayoutPosition());
        Log.d(TAG, "Main Adapter Called remove1 from removeCaller()");
    }

    @Override
    public void onClick(View v) {

        if (v instanceof ImageView) {

        } else {

        }

    }

    public interface IndividualClickListener {

        void onTomato(ImageView callerImage, int position);
        void onPotato(View caller, int position);
        void remove1(int position);
        void onDialogClick(TextView textOKRight);

    }

}

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

}


@Override
public void onBindViewHolder(final MainAdapter.ViewHolder holder, final int position) {
    Question currentQuestion = questions.get(position);
 ///I CREATED A DIALOG HERE, Dialog.Builder AND IM ABLE TO HAVE THE DIALOG DIMISS AND REMOVE THE ITEM, BUT I CANT FIGURE OUT HOW TO DO IT FROM MY CUSTOM DIALOG OUTSIDE MainAdapter////
    Picasso.with(mContext).load(currentQuestion.drawableId).into(holder.getImageView());
    Picasso.with(mContext).load(currentQuestion.iconId).into(holder.getImageView1());

    holder.getImageView().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v instanceof ImageView) {
              //holder.removeCaller();
              //mAdapterCallback.remove1(position);
                mListenerCallback.onTomato((ImageView) v, position);
                Log.d(TAG, "Main Adapter Called remove from OnBindViewHolder"+""+position);

            }else{
                mListenerCallback.onPotato((ImageView) v, position);
            }
      }
});

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.layout_list_item, parent, false);

    final MainAdapter.ViewHolder vh = new ViewHolder(v, new MainAdapter.ViewHolder.IndividualClickListener(){

        @Override
        public void onTomato(ImageView callerImage, int position) {
            Log.d(TAG, "Main Adapter Called OnTomato OnCreateVIewHolder");

        }

        @Override
        public void onPotato(View caller, int position) {

        }
        @Override
        public void remove1(int position) {
            questions.remove(position);
            notifyItemRemoved(position);
            Log.d(TAG, "Main Adapter Called remove1 from OnCreaterViewHolder" + position);

        }

        @Override
        public void onDialogClick(TextView textOKRight) {

        }


    });

    return vh;
}


public interface callerCallBack{
     .....

 }

}

更新

public class NewCustomDialog extends Dialog   {
private static dialogCallerCallBack mdialogCallerCallBack;
private MainAdapter.ViewHolder.IndividualClickListener listener;

public NewCustomDialog(Activity a, dialogCallerCallBack mdialogCallerCallBack, Context dContext, TextView dialogTitleRight, TextView rightMsgRight, TextView rightAnswerRight, TextView textOKRight, ImageView imageRight) {
    super(a);
    this.dialogTitleRight = dialogTitleRight;
    this.rightAnswerRight = rightAnswerRight;
    this.rightMsgRight = rightMsgRight;
    this.textOKRight = textOKRight;
    this.imageRight = imageRight;
    this.dContext = dContext;
    //pass this so it can be used
    this.mdialogCallerCallBack=mdialogCallerCallBack;

   this.c =a;

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //final Dialog rightDialog = new Dialog(context);
    getWindow().setBackgroundDrawable(new ColorDrawable(0));
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mt_alert_right_dialog);
    setCanceledOnTouchOutside(false);
    setCancelable(false);
    TextView dialogTitleRight = (TextView) findViewById(R.id.txt_title);
    TextView rightMsgRight = (TextView) findViewById(R.id.right_msg);
    TextView rightAnswerRight = (TextView) findViewById(R.id.right_answer);
    final TextView textOKRight = (TextView) findViewById(R.id.text_ok);
    ImageView imageRight = (ImageView) findViewById(R.id.image);
    textOKRight.setText("NEXT");
    textOKRight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mdialogCallerCallBack.onDialogClick(((TextView)textOKRight));

        }
    });
}


public interface dialogCallerCallBack{
    void onDialogClick(TextView textOKRight);
}
}

错误

Process: jp.wasabeef.example.recyclerview, PID: 28523
java.lang.NullPointerException at jp.wasabeef.example.recyclerview.NewCustomDialog$1.onClick(NewCustomDialog.java:82)

第82行是

mdialogCallerCallBack.onDialogClick(((TextView)textOKRight));

推荐答案

如果您的问题是使用getFragmentManaget实现的,请执行以下操作:创建

if your problem is using getFragmentManaget to implement do like below:create

FragmentManager fm = getFragmentManager();

片段中的

并将其传递给RecyclerView,然后再次将其传递给ViewHolder

in your Fragment and pass it to your RecyclerView and again pass it to your ViewHolder

注意:pass表示将其作为参数发送给构造函数

note: pass means send it as argument to constructor

这篇关于在RecyclerView的onBindViewHolder内部调用的自定义对话框按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:04