toggle switch in recyclerview programmatically

核能气质少年 提交于 2019-12-12 06:20:13

问题


In my RecyclerView, there's plenty of objects, including Switch. How do I toggle one of the switch in the RecyclerView, if I know the index (location) of which switch I want to toggle?

Just in case, here's the content of my RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cvExpenses"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    card_view:cardCornerRadius="5dp"
    android:layout_margin="5dp"
    card_view:cardBackgroundColor="#009dde"
    >

    <LinearLayout
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="4dip">

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:visibility="gone"
            android:id="@+id/tvGuestID" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:id="@+id/tvGuestName"
            android:textColor="#ffffff" />

        <Switch
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:id="@+id/swCheckIn"
            android:clickable="true"
            android:textOff="no"
            android:textOn="yes" />

    </LinearLayout>
</android.support.v7.widget.CardView>

and here's the xml in which the view above is loaded

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="#ff26b4e9">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvGuestList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/svSearchGuest" />
</RelativeLayout>

回答1:


In a listview you could have just done ((Switch)findViewById(R.id.swCheckIn)).toggle();

but in a RecyclerView you have to use ViewHolders. Assuming you have your ViewHolder set up (if you don't, I recommend looking up a RecyclerView tutorial), you'd do the following in onBindViewHolder:

@Override
public void onBindViewHolder(VH viewHolder, int position)
    ...
    if (viewIsToBetoggled(position)) {
        viewHolder.swCheckin.toggle();
    }
    ...
}


来源:https://stackoverflow.com/questions/30017498/toggle-switch-in-recyclerview-programmatically

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