我試圖實現在Google Play上看到的功能,CardView具有可點擊的選項菜單,您可以點擊卡片查看卡片上的更多詳細信息。Android RecyclerView點擊
我在我的RecyclerView上實現了一個OnItemTouch偵聽器,它可以正常工作,並在觸摸時響應,帶我到另一個活動。但是,當我現在嘗試將點擊偵聽器添加到我在RecyclerView項(它是CardView)中添加的選項圖標時,會調用OnItemTouch偵聽器。誰能幫忙?
活動
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(getApplicationContext(),
TransactionDetailActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("transaction_key", mTransactionList.get(position));
intent.putExtras(bundle);
startActivity(intent);
}
})
); mRecyclerView.setAdapter(mAdapter);
點擊監聽器
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@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.getChildPosition(childView));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}