RecyclerView行onClick和子按钮onClick

时间:2016-08-31 08:50:28

标签: java android android-recyclerview

我正在使用RecyclerView onClick中的方法在recyclerview中执行项目点击方法。但问题是如何处理recyclerview中的子按钮?我在适配器中执行了onclick方法,但仍然无法正常工作,它总是首先执行recyclelerview onitemclicklistener然后只从适配器执行onclick方法。如何妥善处理?

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;


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

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

    public void onLongItemClick(View view, int position);
  }

  GestureDetector mGestureDetector;

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

        @Override
        public void onLongPress(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (child != null && mListener != null) {
                mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
            }
        }
    });
}

  @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 true;
    }
    return false;
  }

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

  @Override
  public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}

1 个答案:

答案 0 :(得分:1)

我在适配器部分实现了OnClick ...请参阅我的适配器类的以下代码

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private Context context;
    List<Video_Details> video;

    public DataAdapter(List<Video_Details> video, Context context) {
        super();
        this.context = context;
        this.video = video;
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final Video_Details videoDetails =  video.get(position);
        String url;
        final String VideoID;

        holder.title.setText(video.get(position).getTitle());

        VideoID= video.get(position).getV_id();
        url = video.get(position).getThumb();

         Glide.with(context)
                 .load(url)
                 .override(150,70)
                 .into(holder.thumb);
       //viewHolder.thumb.setText(android.get(i).getVer());
      //  viewHolder.tv_api_level.setText(android.get(i).getApi());

        holder.vm.setOnClickListener(new View.OnClickListener() {
                                         @Override
                                         public void onClick(View v) {
                                             Toast.makeText(v.getContext(), "You Clicked"+video.get(position).getV_id(), Toast.LENGTH_SHORT).show();
                                             Intent intent = new Intent(v.getContext(),Play_Video.class);
                                             intent.putExtra("VideoId",(video.get(position).getV_id()));
                                             intent.putExtra("Title",(video.get(position).getTitle()));
                                             v.getContext().startActivity(intent);


                                         }
                                     }

        );
    }

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




    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView title;
        public ImageView thumb;
        public String videoid;
        public View vm;
        public ViewHolder(View view) {
            super(view);
            vm = view;
            title = (TextView)view.findViewById(R.id.title);
            thumb = (ImageView) view.findViewById(R.id.thumb);



            //tv_version = (TextView)view.findViewById(R.id.tv_version);
            //tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);

        }
    }


}