Back to project page UpcomingMoviesMVP.
The source code is released under:
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Defi...
If you think the Android project UpcomingMoviesMVP listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.jlmd.android.newfilmsmvp.ui.view; /*from ww w. ja v a2s . c o m*/ import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.HapticFeedbackConstants; import android.view.SoundEffectConstants; import android.view.View; /** * @author lucasr * From https://github.com/lucasr/twoway-view */ public class ClickRecyclerView extends RecyclerView { private TouchListener mTouchListener; private OnItemClickListener mItemClickListener; private OnItemLongClickListener mItemLongClickListener; public ClickRecyclerView(Context context) { super(context); init(); } public ClickRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public ClickRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { mTouchListener = new TouchListener(this); addOnItemTouchListener(mTouchListener); } /** * Register a callback to be invoked when an item in the * RecyclerView has been clicked. * * @param listener The callback that will be invoked. */ public void setOnItemClickListener(OnItemClickListener listener) { mItemClickListener = listener; } /** * Register a callback to be invoked when an item in the * RecyclerView has been clicked and held. * * @param listener The callback that will be invoked. */ public void setOnItemLongClickListener(OnItemLongClickListener listener) { if (!isLongClickable()) { setLongClickable(true); } mItemLongClickListener = listener; } /** * Interface definition for a callback to be invoked when an item in the * RecyclerView has been clicked. */ public interface OnItemClickListener { /** * Callback method to be invoked when an item in the RecyclerView * has been clicked. * * @param parent The RecyclerView where the click happened. * @param view The view within the RecyclerView that was clicked * @param position The position of the view in the adapter. * @param id The row id of the item that was clicked. */ void onItemClick(RecyclerView parent, View view, int position, long id); } /** * Interface definition for a callback to be invoked when an item in the * RecyclerView has been clicked and held. */ public interface OnItemLongClickListener { /** * Callback method to be invoked when an item in the RecyclerView * has been clicked and held. * * @param parent The RecyclerView where the click happened * @param view The view within the RecyclerView that was clicked * @param position The position of the view in the list * @param id The row id of the item that was clicked * @return true if the callback consumed the long click, false otherwise */ boolean onItemLongClick(RecyclerView parent, View view, int position, long id); } private class TouchListener extends ClickItemTouchListener { TouchListener(RecyclerView recyclerView) { super(recyclerView); } @Override boolean performItemClick(RecyclerView parent, View view, int position, long id) { if (mItemClickListener != null) { view.playSoundEffect(SoundEffectConstants.CLICK); mItemClickListener.onItemClick(parent, view, position, id); return true; } return false; } @Override boolean performItemLongClick(RecyclerView parent, View view, int position, long id) { if (mItemLongClickListener != null) { view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); return mItemLongClickListener.onItemLongClick(parent, view, position, id); } return false; } } }