简体   繁体   English

recyclerview.adapter 重置单选按钮

[英]recyclerview.adapter resets radio-buttons

This is my problem: https://youtu.be/k-N5uthYhYw这是我的问题: https : //youtu.be/k-N5uthYhYw

and this is my onBindViewHolder() method.这是我的 onBindViewHolder() 方法。

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element

    holder.specName.setText(specList.get(position).getSpecName());

    // Assign a tag number to later identify what radio-button
    holder.specRadioBtn.setTag(new Integer(position));

    /* Event listenr for longClick - we prob. won't use it, but it's here just in case */
    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            Toast.makeText(context, "Long press", Toast.LENGTH_SHORT).show();

            return false;
        }
    });

    /* Little hack to select its Radio Button when a specific row is tapped */
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Turn rowSelectedFlag to true since the user selected this row
            rowSelectedFlag = true;

            // When the user taps on a row select that row's radio button
            holder.specRadioBtn.setChecked(true);

            // I'm not sure why, but locally the interface needs to be started by pointing it
            // to where it should drive the data (to send the params)
            tempInterface = new AdminUserSpecialty();

            // Call the interface to send the data (row spec-name and id) back to AdminUserSpecialty
            tempInterface.activateSpecSelect(specList.get(position).getSpecName().toString(),
                    specList.get(position).getSpecId().toString(), rowSelectedFlag);

            int clickedPos = ((Integer) holder.specRadioBtn.getTag());

            // Check if the radio button is already selected
            if (holder.specRadioBtn.isChecked()) {

                if (lastCheckedBtn != null) {

                    // Don't deselect if user taps on the same row several times
                    if (lastCheckedBtn == holder.specRadioBtn) {

                        // do nothing
                    }

                    // Otherwise do deselect the previously selected radio button
                    else {

                        lastCheckedBtn.setChecked(false);
                    }
                }

                lastCheckedBtn = holder.specRadioBtn;
                lastCheckedPos = clickedPos;
            }

            // If radio is not checked set the lastCheckedBtn to null (reset counter)
            else {
                lastCheckedBtn = null;
            }
        }
    });

    /* ----------------------------------------------------------------------*/


}

I can't seem to preserve my radio-button selection on RecyclerView scroll.我似乎无法在 RecyclerView 滚动上保留我的单选按钮选择。 On scroll the selection becomes erratic and random.在滚动时,选择变得不稳定和随机。 I understand that one of RecyclerView's features is to recycle rows as they leave the screen, but what do I need to do to keep my selection?我知道 RecyclerView 的功能之一是在行离开屏幕时对其进行回收,但是我需要做什么才能保留我的选择? Thanks much.非常感谢。

I know that this was answered already but if some of you are still looking for an easier answer and your application does not rely on the RecyclerView view recycling feature much (for example if you have a fixed size list of items...) you can always set your recycler view cache view size.我知道这已经得到了回答,但是如果你们中的一些人仍在寻找更简单的答案,并且您的应用程序不太依赖 RecyclerView 视图回收功能(例如,如果您有固定大小的项目列表...),您可以始终设置您的回收站视图缓存视图大小。 That way it would not recycler your views hence it would not recycler the views and you will avoid copy selected values to another views...这样它就不会回收您的视图,因此它不会回收视图,您将避免将选定的值复制到另一个视图...

yourRecyclerView..setItemViewCacheSize(yourItemList.size());

Save the checked / unchecked status of the radio button ( you should use checkbox instead if you want to allow the user to select multiple items ) to your model (ie your items in the list should have a field for this) when the onClick event happens.onClick事件发生时,将单选按钮的选中/未选中状态(如果您想允许用户选择多个项目,您应该使用复选框)保存到您的模型(即列表中的项目应该有一个字段) . When you bind the ViewHolder, make sure you set checkbox's value to whatever you saved in your model.当您绑定 ViewHolder 时,请确保将复选框的值设置为您保存在模型中的任何值。

It's happen because of the Recycling mechanism这是因为回收机制

(PS: its the same for the ListView or RecyclerView ). (PS:它与ListViewRecyclerView相同)。

To fix that:要解决这个问题:

  • 1) Add a booelan variable to your model to save the state of the RadioButton 1) 向您的模型添加一个booelan变量以保存RadioButton的状态

  • 2) Update your RadioButton state in onBindViewHolder() method from this boolean in the model. 2) 从模型中的这个boolean更新onBindViewHolder()方法中的RadioButton状态。

  • 3) Add setOnCheckedChangeListener() to your RadioButton to listen to his state (checked/unchecked) and to update the boolean in your model when the state changes. 3)将setOnCheckedChangeListener() ) 添加到您的RadioButton以侦听他的状态(选中/未选中)并在状态更改时更新模型中的布尔值。

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

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