android– 在recyclerview中的Radiogroup

android– 在recyclerview中的Radiogroup,第1张

概述我有一个recyclerview,其中每个列表项都有一个带4个单选按钮的radiogroup.如何正确存储每个放射组的状态.这是我的代码.在向上/向下滚动时,状态是不相关的.谢谢publicclassElementListAdapterextendsRecyclerView.Adapter<ElementListAdapter.ViewHolder>{privateLi

我有一个recyclervIEw,其中每个列表项都有一个带4个单选按钮的radiogroup.如何正确存储每个放射组的状态.
这是我的代码.在向上/向下滚动时,状态是不相关的.谢谢

public class Elementlistadapter extends RecyclerVIEw.Adapter<Elementlistadapter.VIEwHolder> {    private List<Element> elements = new ArrayList<>();    private Context context;    private int[] state;    public Elementlistadapter(Context context, List<Element> elements) {        this.context = context;        this.elements = elements;        this.state = new int[elements.size()];        Arrays.fill(this.state, -1);    }    @OverrIDe    public Elementlistadapter.VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) {        VIEw v = LayoutInflater.from(parent.getContext()).inflate(R.layout.List_item_layout, parent,                false);        return new VIEwHolder(v);    }    @OverrIDe    public voID onBindVIEwHolder(final VIEwHolder holder, final int position) {        final Element ele = elements.get(position);        final String Title = ele.getTitle();        final String description = ele.getDescription();        // Set text        holder.tvTitle.setText(Title);        holder.tvDesciption.setText(description);        if (ele.isheader()) {                            holder.radioGroup.setVisibility(VIEw.GONE);        } else {                           holder.radioGroup.setVisibility(VIEw.VISIBLE);        }        seTradio(holder, this.state[position]);        holder.rb1.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                state[position] = 0;                seTradio(holder, state[position]);            }        });        holder.rb2.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                state[position] = 1;                seTradio(holder, state[position]);            }        });        holder.rb3.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                state[position] = 2;                seTradio(holder, state[position]);            }        });        holder.rb4.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                state[position] = 3;                seTradio(holder, state[position]);            }        });    }    private voID seTradio(final VIEwHolder holder, int selection) {        System.out.println("SELECT:" + selection);        Radiobutton b1 = holder.rb1;        Radiobutton b2 = holder.rb2;        Radiobutton b3 = holder.rb3;        Radiobutton b4 = holder.rb4;        b1.setChecked(false);        b2.setChecked(false);        b3.setChecked(false);        b4.setChecked(false);        if (selection == 0) b1.setChecked(true);        if (selection == 1) b2.setChecked(true);        if (selection == 2) b3.setChecked(true);        if (selection == 3) b4.setChecked(true);    }    @OverrIDe    public int getItemCount() {        return elements.size();    }    public static class VIEwHolder extends RecyclerVIEw.VIEwHolder {        public VIEw vIEw;        public TextVIEw tvTitle;        public TextVIEw tvDesciption;        public RadioGroup radioGroup;        public Radiobutton rb1, rb2, rb3, rb4;        public VIEwHolder(VIEw itemVIEw) {            super(itemVIEw);            vIEw = itemVIEw;            tvTitle = (TextVIEw) itemVIEw.findVIEwByID(R.ID.Title);            tvDesciption = (TextVIEw) itemVIEw.findVIEwByID(R.ID.description);            radioGroup = (RadioGroup) itemVIEw.findVIEwByID(R.ID.radioGroup);            rb1 = (Radiobutton) itemVIEw.findVIEwByID(R.ID.rb1);            rb2 = (Radiobutton) itemVIEw.findVIEwByID(R.ID.rb2);            rb3 = (Radiobutton) itemVIEw.findVIEwByID(R.ID.rb3);            rb4 = (Radiobutton) itemVIEw.findVIEwByID(R.ID.rb4);        }    }}

解决方法:

保存项状态的最佳方法是将状态变量放在列表的项模型中,例如:在您的情况下为“元素”,而不是在onBindVIEwHolder内部根据您的模型设置状态,在您的情况下:

改变这个:seTradio(holder,this.state [position]);

对此:seTradio(holder,elements.get(position).getState());

在onClick方法中

例如:第一个
改变这个:
state [position] = 0;
seTradio(holder,this.state [position]);

对此:
elements.get(位置).setState(0);
seTradio(holder,elements.get(position).getState());

总结

以上是内存溢出为你收集整理的android – 在recyclerview中的Radiogroup全部内容,希望文章能够帮你解决android – 在recyclerview中的Radiogroup所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1115022.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存