以编程方式在recyclerview中拨动开关

以编程方式在recyclerview中拨动开关

本文介绍了以编程方式在recyclerview中拨动开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的RecyclerView中,有很多对象,包括Switch.如果我知道要切换哪个开关的索引(位置),如何在RecyclerView中切换其中一个开关?

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?

以防万一,这是我的RecyclerView的内容

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>

这是上面的视图在其中加载的xml

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>

推荐答案

在列表视图中,您可能刚刚完成((Switch)findViewById(R.id.swCheckIn)).toggle();

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

但是在RecyclerView中,您必须使用ViewHolders.假设您已设置ViewHolder(如果没有,建议您查看RecyclerView教程),则可以在onBindViewHolder中执行以下操作:

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

这篇关于以编程方式在recyclerview中拨动开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:50