简体   繁体   English

在RecyclerView Clickable中切换小部件

[英]Switch Widget Inside RecyclerView Clickable

I'm trying to use a Switch widget inside of a RecyclerView to set the particular component as active or not. 我正在尝试在RecyclerView中使用Switch小部件来将特定组件设置为活动或不活动。 How can I detect the switch toggling from the RecyclerView? 如何检测从RecyclerView切换的开关? My current OnItemClickListener intercepts the click on the Switch widget also. 我当前的OnItemClickListener也拦截了Switch小部件上的点击。

Current Click Listener Code: 当前点击侦听器代码:

mRecyclerView.addOnItemTouchListener(
        new RecyclerItemListener(getApplicationContext(), mRecyclerView,
                new RecyclerItemListener.RecyclerTouchListener() {
                    public void onClickItem(View v, int position) {

                        Toast.makeText(MainActivity.this, "Clicked, position " + position + ". Name: " + GlobalData.totalAlarms.get(position).getAlarmName(), Toast.LENGTH_LONG).show();

                    }
                    public void onLongClickItem(View v, int position) {
                    }
                }));
}

Layout for RecyclerView, including Switch: RecyclerView的布局,包括Switch:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:focusable="true"
    android:clickable="true">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_mainactivityName"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textSize="16sp"
        android:layout_marginTop="15dp"
        android:paddingLeft="20dp" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/recycler_mainactivityTime"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/recycler_mainactivityDays"
        android:paddingLeft="20dp"
        android:textSize="12sp"
        android:layout_marginBottom="5dp" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_mainactivityTime"
        android:paddingTop="2dp"
        android:textSize="12sp"
        android:paddingLeft="20dp"
        android:layout_below="@+id/recycler_mainactivityName"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_mainactivityActive"
        android:paddingRight="20dp"
        android:layout_alignBaseline="@+id/recycler_mainactivityTime"
        android:layout_alignBottom="@+id/recycler_mainactivityTime"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:hapticFeedbackEnabled="true" />

</RelativeLayout>

When I remove the addOnItemTouchListener, the switches do toggle on click rather than being forced to swype.. Beyond that, how can I detect the touch? 当我删除addOnItemTouchListener时,开关会在点击时切换,而不是强制进行swype ..除此之外,我该如何检测触摸? The addOnItemClickListener seems to intercept all clicks (unless I do a long click, then the switches toggle). addOnItemClickListener似乎拦截了所有点击(除非我做了长按,然后是切换切换)。

在recyclelerView适配器中添加交换机的OnCheckChangeListener以获取交换机的状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM