recyclerview点击事件在警报对话框中不起作用

时间:2016-12-20 04:55:21

标签: android android-recyclerview alertdialog

我正在使用自定义对话框视图和recyclerview,我成功地在我的recyclerview中获取数据,但是当我设置点击监听器到我的recyclerview时,它无法正常工作。

@Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            pDialog.dismiss();
            final AlertDialog.Builder builderSingle = new AlertDialog.Builder(mContext);
            final AlertDialog OptionDialog = builderSingle.create();
            builderSingle.setTitle("Select Card");

            View view = ((Activity)mContext).getLayoutInflater().inflate(R.layout.activity_payment, null);

            RecyclerView list1 = (RecyclerView) view.findViewById(R.id.recyclerview);
            final LinearLayoutManager mLayoutManager;
            mLayoutManager = new LinearLayoutManager(mContext);
            list1.setLayoutManager(mLayoutManager);
            list1.setItemAnimator(new DefaultItemAnimator());
            AdapterDialogCard adapter = new AdapterDialogCard(mContext, list);
            list1.setAdapter(adapter);


            list1.addOnItemTouchListener(new RecyclerTouchListener(mContext, list1, new RecyclerTouchListener.ClickListener() {
                @Override
                public void onClick(View view, int position) {
                    CreditCard item = list.get(position);
                    cardid      = item.getId();
                    OptionDialog.dismiss();
                }

                @Override
                public void onLongClick(View view, int position) {

                }
            }));

            builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            builderSingle.setView(view);
            builderSingle.show();
        }
    }

这就是我得到的结果。

enter image description here

所以我的问题是如何在警告对话框中设置点击监听器。

4 个答案:

答案 0 :(得分:0)

您正在使用RecycleView意味着您也在使用RecycleAdapter。 如果你在Adapter' onBindViewHolder中添加你的监听器,它将起作用,如下所示:

@Override 
  public void onBindViewHolder(final ViewHolder holder, int position) {
    CreditCard item = list.get(position);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           ///do your code here//
        }
    });
}

如果有效,请告诉我。

答案 1 :(得分:0)

您需要扩展ItemTouchListener

 public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;

public interface OnItemClickListener {
    void onItemClick(View view, int position);
}

private GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
        mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}
 }

并调用此类使用此代码

 rv.addOnItemTouchListener(
            new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                    dialog.dismiss();
                }
            })
    );

答案 2 :(得分:0)

创建界面 -

public interface AdapterClickListener {
    void onClick(int position, View view);
}

将该接口的对象传递给适配器类的构造函数:

AdapterDialogCard adapter = new AdapterDialogCard(mContext, list, new AdapterClickListener() {
        @Override
        public void onClick(final int position, View view) {
           //Your code
        }
    });

现在,在Adapter Class Constructor中:

public AdapterClass(Your Other objects, AdapterClickListener adapterClickListener){
      this.adapterClickListener = adapterClickListener;
      //Other Code
}

现在,在适配器类中实现持有者中的单击侦听器:

itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (adapterClickListener != null) {
                        adapterClickListener.onClick(getAdapterPosition(), v);
                    }
                }
            });

答案 3 :(得分:0)

对不起伙计们。这是一个愚蠢的错误,但我终于解决了它。我只是将我的ItemTouchListener与@Newbie android答案进行比较。我发现在我的课堂上我使用了dérecated方法来获取视图的位置。

@Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildPosition(child));
            }
            return false;
        }

这里我的rv.getChildPostion()已被弃用,所以我只需将其更改为rv.getChildAdapterPosition()并发生奇迹。我从未想过这会在对话中造成麻烦。