在RecyclerView中对开关进行分组 [英] Grouping Switches inside RecyclerView

查看:57
本文介绍了在RecyclerView中对开关进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在尝试对recylerView内的开关进行分组时遇到困难.我在下面附加了我的代码和屏幕快照. 如您所见,我现在可以切换任何开关,但是在给定的时间点,我只希望打开一个开关.

I am currently facing difficulty trying to group the switches inside recylerView. I have attached my code below and a snapshot of the screen. As you can see, I can now toogle any switch but at a given point in time , I want only one switch to be toggled on.

Item_list.xml:-

Item_list.xml :-

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingBottom="@dimen/row_padding_vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/row_padding_vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="87dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:layout_marginRight="30dp"
card_view:cardCornerRadius="8dp">

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="95dp"
 android:background="#20b2aa"
 android:orientation="vertical">

 <LinearLayout
 android:layout_width="182dp"
 android:layout_height="wrap_content"
 android:layout_marginLeft="30dp"
 android:layout_marginTop="20dp"
 android:layout_marginRight="30dp"
 android:orientation="horizontal">

 <TextView
 android:id="@+id/name"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textColor="#ffff"
 android:textSize="32dp" />


 </LinearLayout>

 <Switch

 android:id="@+id/value"
 android:layout_width="59dp"
 android:layout_height="3dp"
 android:layout_marginLeft="190dp"
 android:layout_marginTop="-35dp"
 android:layout_marginBottom="100dp"
 android:scaleX="1.1"
 android:scaleY="1.1"
 android:theme="@style/SwitchTheme" />

 </LinearLayout>
 </androidx.cardview.widget.CardView>

 </LinearLayout>
 </RelativeLayout>


ItemAdapter:-


ItemAdapter:-

package com.example.loginpage;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;

import java.util.List;

public class ItemAdapter extends 
RecyclerView.Adapter<ItemAdapter.MyViewHolder> {

private int cn=0;
private List<Item> itemList;

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public Switch value;

public MyViewHolder(View view) {
super(view)   
name = (TextView) view.findViewById(R.id.name);
value = (Switch) view.findViewById(R.id.value);
}
}

public ItemAdapter(List<Item> itemList) {
this.itemList = itemList;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int 
viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Item item = itemList.get(position);
holder.name.setText(item.getName());
}

@Override
public int getItemCount() {
return itemList.size();
}

}


我当前的界面如下:... https://www.pastepic.xyz/image/8z5Eh


My current interface looks like this.... https://www.pastepic.xyz/image/8z5Eh

推荐答案

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

    Item item = itemList.get(position);

    holder.name.setText(item.getName());
    holder.value.setChecked(item.isValueChecked());

    holder.value.setOnCheckedChangeListener(new OnCheckedChangeListener(
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {

            for(Item i: itemList){
                i.setValueChecked(false);
            }

            if(buttonView.isChecked()){
                itemList.get(position).setValueChecked(true);
                holder.value.setChecked(true);
            }

            notifyDataSetChanged();

    }

    });
}

尝试此解决方案,它应该可以工作...

Checked Change Listener (已检查的更改侦听器),然后将 itemList 的所有设置为false和current将 value 设置为True并将开关设置为ON,最后调用 notifyDataSetChanged().

Checked Change Listener is set on the switch if any switch is turned ON then all the values of the itemList is made false and current value is made True and switch is set ON, finally, notifyDataSetChanged() is called.

如果任何开关都关闭,则 itemList 中的所有为False,并调用 notifyDataSetChanged(),以便该开关状态得到更新.

If any switch is turned OFF then all the values in the itemList are made False and notifyDataSetChanged() is called so that the switch state gets updated.

  holder.value.setChecked(item.isValueChecked());

这将基于存储在 itemList 中的值来更新开关状态.

This updates the switch state based on the values stored in the itemList.

这篇关于在RecyclerView中对开关进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆