Android

리사이클러뷰 아이템 클릭 이벤트

Machine_웅 2018. 7. 26. 15:00
728x90
반응형

리사이클러뷰는 리스트뷰와는 다르게 엑티비티에서  아이템 클릭 이벤트 처리가 어렵다  그래서 클릭 이벤트를 처리해줄 클래스를 만들어서 처리한다.

 

 

< 클래스 생성 >

package com.team.project.wat_show.main_activity;

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

public class video_content_itemClickListener implements RecyclerView.OnItemTouchListener{

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

private OnItemClickListener mListener;
private GestureDetector mGestureDetector;

public video_content_itemClickListener(Context context,final RecyclerView rView,OnItemClickListener clickListener){

mListener = clickListener;
mGestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e)
{
return true;
}

@Override
public void onLongPress(MotionEvent e)
{
View childView = rView.findChildViewUnder(e.getX(), e.getY());

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



@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View childView = rv.findChildViewUnder(e.getX(), e.getY());

if(childView != null && mListener != null && mGestureDetector.onTouchEvent(e))
{
mListener.onItemClick(childView, rv.getChildPosition(childView));
}
return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}
}


 

<< 클릭 리스너를 사용할 곳 >>


// 아이템 클릭 이벤트
public void ItemClick(){
videoContent_RecyclerView.addOnItemTouchListener(new video_content_itemClickListener(getActivity(),videoContent_RecyclerView,
new video_content_itemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// Toast.makeText(getContext(), ""+position, Toast.LENGTH_SHORT).show();

}

@Override
public void onItemLongClick(View view, int position) {

}
}));
}

 

 

728x90
반응형