的問題是不明確的。但根據我的理解,你想獲得對整個視圖,也爲在同一時間每個項目的Click事件。
獲得了整個視圖中單擊事件
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
// this will be called multiple times for single click - for MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP, and MotionEvent.ACTION_MOVE
// So restricting the call only for ACTION_DOWN,
if(e.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(mContext, "Clicked view", Toast.LENGTH_LONG).show();
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
});
,並得到每個項目的點擊事件,在onBindViewHolder函數添加點擊監聽
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "Clicked Item", Toast.LENGTH_LONG).show();
}
});
}
你是對的,但有一個小問題FOT我是因爲我想獲得的TouchEvent同時爲整個視圖和記者的項目,不只是因爲的onClick事情,我需要做的時候ACTION_DOWN或移動或向上 – AsyncCode