Android - clicking on a widget inside a clickable cardview

戏子无情 提交于 2019-12-11 04:12:42

问题


After fighting and eventually implementing this: RecyclerView onClick I manage to make the cardviews clickable and long-clickable. This is working well, or maybe too well, because I also want a certain widget inside the cardview to be clickable so when clicking on it, the cardview should ignore the click, what happens now is that both the widget and the cardview, consume the click. Here is the code:

The recyclerview:

<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>

The cardview:

<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>

The Switch click:

    holder.sEnable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            ...do some stuff
        }
    });

The recyclerview click/long click:

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
    }
}));

I was hoping the widget will be the first to be executed so I will be able to stop the card view click to be processed but the opposite happens. I tried catching the widget where the user taps but I didn't manage to do so, all the events were ignored. Please help!

来源:https://stackoverflow.com/questions/29486763/android-clicking-on-a-widget-inside-a-clickable-cardview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!