Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

android - OnLongItemClick in RecyclerView

I am starting to use RecyclerView in Android. Things work fine until I implement touch listener for my adapter.

From this topic:

https://stackoverflow.com/a/26826692/2923403

I can implement the listener for childview correctly. However, I would like to implement both OnItemClick and OnItemLongClick, then problem appear. They are always fired at the same time. OnItemClick first and OnItemLongClick later, which renders my function useless.

Here is my code for the custom listener (based on the code above, thanks to Fouad):

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

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

    public void onItemLongClick(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 childView = recyclerView.findChildViewUnder(e.getX(), e.getY());

            if (childView != null && mListener != null) {
                mListener.onItemLongClick(childView, recyclerView.getChildPosition(childView));
            }
        }

    });
}

@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.getChildPosition(childView));
        return true;
    }
    return false;
}

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

And for the implementation:

mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(),   mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
 public boolean onItemClick(View view, int position) {
//This is fired 1st
}
 @Override
 public void onItemLongClick(View view, int position) {
//This comes later
}));

Do you have any solution for that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add listeners in your custom adapter implementation. It will be something like:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    public interface OnItemClickListener {
        public void onItemClicked(int position);
    }

    public interface OnItemLongClickListener {
        public boolean onItemLongClicked(int position);
    }

    private Fragment mFragment;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View v;

        public ViewHolder(View v) {
            super(v);
            this.v = v;
        }
    }

    public RecyclerViewAdapter(Fragment fragment) {
        mFragment = fragment;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFragment.onItemClicked(position);
            }
        });
        holder.v.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mFragment.onItemLongClicked(position);
                return true;
            }
        });
    }
    . . .
}

Note interfaces definition at the beginning. This way you're passing onClick and onLongClick events to your fragment for handling. Pretty much convenient, you know


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...