经过斗争并最终实现了这一点:RecyclerView onClick我设法使卡片视图可点击和长时间点击。这工作得很好,或者可能太好了,因为我还希望卡片视图中的某个小部件是可点击的,所以当点击它时,卡片视图应该忽略点击,现在发生的是小部件和卡片视图都消耗点击。这是代码:
回收者观点:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:longClickable="true"
android:hapticFeedbackEnabled="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
卡片视图:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cvWhitelist"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="8dp"
card_view:cardBackgroundColor="#99CC00"
card_view:cardElevation="10dp" >
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sEnable"
android:layout_alignParentEnd="true"
android:layout_marginEnd="3dp"
android:clickable="true"
android:layout_above="@+id/tvHours" />
</RelativeLayout>
</android.support.v7.widget.CardView>
开关点击:
holder.sEnable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
...do some stuff
}
});
recyclerview 点击/长按:
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(Utils.context, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
..do some stuff
}
@Override
public void onItemLongClick(View view, final int itemPosition) {
..do some stuff
}
}));
我希望小部件将是第一个被执行的,所以我将能够停止卡片视图点击以进行处理,但相反的情况发生了。我试图捕捉用户点击的小部件,但我没有设法这样做,所有事件都被忽略了。请帮忙!