0
我在我的學校項目有一些問題。當我點擊其中的ImageButton時,我的RecyclerView的OnItemClick
正在觸發。RecyclerView的OnItemClick觸發之前,它的孩子的OnClick
CODE
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
viewComments(posts.get(position));
}
@Override
public void onItemLongClick(View view, int position) {
}
}));
而這裏的RecyclerItemClickListener代碼:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView.findViewById(R.id.btnRateUp).isPressed() || childView.findViewById(R.id.btnRateDown).isPressed()) {
return false;
}
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.getChildAdapterPosition(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.getChildAdapterPosition(childView));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
這是我想出了檢測,如果孩子獲得點擊,
if(childView.findViewById(R.id.btnRateUp).isPressed() || childView.findViewById(R.id.btnRateDown).isPressed())
但我已經意識到方法isPressed()
需要比RecyclerV更長的時間圖像OnItemClick
進行處理。該代碼僅適用於不快速點擊它的情況。
btnRateUp和btnRateDown是ImageButtons及其OnClick
事件在適配器的onBindViewHolder
正在實施如下:
holder.getBtnRateUp().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doStuff(true);
}
});
holder.getBtnRateDown().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doStuff(false);
}
});