STEP - 1 : RecyclerView Click
mRecyclerView.addOnItemTouchListener( new RecyclerItemClickListener(getActivity(), mRecyclerView,new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { // do whatever TextView tvUserID = (TextView)view.findViewById(R.id.idUserIDTV); String userID = tvUserID.getText().toString(); // Toast.makeText(getContext(), "Short : "+userID, Toast.LENGTH_SHORT).show(); Button btnRemoveFriend = (Button) view.findViewById(R.id.idRemoveFriendBtn); btnRemoveFriend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Toast.makeText(getContext(), "Add as Friend : "+userID, Toast.LENGTH_SHORT).show(); listener.onFragmentRemoveFriend(UserDefinedKeyword.REMOVE.toString(), userID); } }); Button btnUnShortList = (Button) view.findViewById(R.id.idShortlistBtn); btnUnShortList.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Toast.makeText(getContext(), "Add as Friend : "+userID, Toast.LENGTH_SHORT).show(); listener.onFragmentShortListFriend(UserDefinedKeyword.SHORTLIST.toString(), userID); } }); Button btnViewProfile = (Button) view.findViewById(R.id.idViewProfileBtn); btnViewProfile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Toast.makeText(getContext(), "View Profile : "+userID, Toast.LENGTH_SHORT).show(); if(userID.length()> 0){ prefs.putString(CONSTANTS.OTHER_USERID, userID); Navigator.getClassInstance().navigateToActivity(getActivity(), OtherProfileActivity.class); } } }); } @Override public void onLongItemClick(View view, int position) { // do whatever //Initialize // Toast.makeText(getContext(), "Long", Toast.LENGTH_SHORT).show(); } }) );
STEP - 2 : RecyclerItemClickListener class
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { private OnItemClickListener mListener; public interface OnItemClickListener { void onItemClick(View view, int position); 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 false; } return false; } @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } @Override public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){} }
No comments:
Post a Comment