recyclerview.adapter重置单选按钮

时间:2015-08-27 17:21:24

标签: java android android-recyclerview

这是我的问题: https://youtu.be/k-N5uthYhYw

这是我的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;
            }
        }
    });

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


}

我似乎无法在RecyclerView滚动条上保留我的单选按钮选择。在滚动时,选择变得不稳定且随机。我知道RecyclerView的一个功能是在离开屏幕时回收行,但是我需要做些什么来保持我的选择?非常感谢。

2 个答案:

答案 0 :(得分:3)

我知道这已经得到了回答,但是如果你们中的一些人仍然在寻找更简单的答案而你的应用程序不依赖于RecyclerView视图回收功能(例如,如果你有一个固定大小的项目清单...... )您始终可以设置您的回收站视图缓存视图大小。这样它就不会回收您的视图,因此它不会回收视图,您将避免将选定值复制到另一个视图...

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

答案 1 :(得分:1)

保存单选按钮的已选中/未选中状态(您应该使用checkbox代替,如果您想允许用户为您的模型选择多个项目)(即您的项目)当onClick事件发生时,列表应该有一个字段。绑定ViewHolder时,请确保将复选框的值设置为模型中保存的值。