Last active
August 29, 2015 14:16
-
-
Save ilwsm/7fc2f6a88482ce66a26b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ///////// ........ mRecyclerView ........... /////////////////// | |
| mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener( mRecyclerView, new RecyclerClickListener() { | |
| @Override | |
| public void onClick(View v, int position) { | |
| Toast.makeText(getActivity(), "onClick " + position, Toast.LENGTH_SHORT).show(); | |
| } | |
| @Override | |
| public void onLongClick(View v, int position) { | |
| Toast.makeText(getActivity(), "onLongClick " + position, Toast.LENGTH_SHORT).show(); | |
| } | |
| })); | |
| ///////// ........ The class ........... /////////////////// | |
| class RecyclerTouchListener implements RecyclerView.OnItemTouchListener { | |
| private final GestureDetector gestureDetector; | |
| RecyclerTouchListener(final RecyclerView recyclerView, final RecyclerClickListener recyclerClickListener) { | |
| gestureDetector = new GestureDetector(recyclerView.getContext(), new GestureDetector.SimpleOnGestureListener(){ | |
| @Override | |
| public boolean onSingleTapUp(MotionEvent e) { | |
| if (recyclerClickListener == null) return false; | |
| View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); | |
| if (child != null){ | |
| recyclerClickListener.onClick(child, recyclerView.getChildPosition(child)); | |
| } | |
| return true; | |
| } | |
| @Override | |
| public void onLongPress(MotionEvent e) { | |
| if (recyclerClickListener == null) return; | |
| View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); | |
| if (child != null){ | |
| recyclerClickListener.onLongClick(child, recyclerView.getChildPosition(child)); | |
| } | |
| } | |
| }); | |
| } | |
| @Override | |
| public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { | |
| gestureDetector.onTouchEvent(e); | |
| return false; | |
| } | |
| @Override | |
| public void onTouchEvent(RecyclerView rv, MotionEvent e) { | |
| } | |
| } | |
| ///////// ........ The listener ........... /////////////////// | |
| public static interface RecyclerClickListener { | |
| void onClick(View v, int position); | |
| void onLongClick(View v, int position); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment