user6154174
user6154174

Reputation:

How to disable RecyclerView items in Android?

Textviews and Checkboxes are in a Recyclerview. Initially all Checkboxes are selected. I want to prevent user changing Checkboxes state but I do not want to prevent Recyclerview scroll. I would like to do this in my fragment class not in adapter class.

How can I prevent user changing Checkboxes state?

Below is the sample code written in onBindViewHolder in adapter class.

holder.cbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //set your object's last status
            tblFilm.setSelected(isChecked);
        }
    });

The above code used for first mode which allows user clicks on checkbox.

After that I have second mode where I do not want to get clicked on checkbox at all.

For that I have done is below.

optionsoff(recyclerView);
private static void optionsOff(ViewGroup layout) {
        layout.setEnabled(false);
        layout.setClickable(false);
        for (int i = 0; i < layout.getChildCount(); i++) {
            View child = layout.getChildAt(i);
            if (child instanceof ViewGroup) {
                optionsOff((ViewGroup) child);
            } else {
                child.setEnabled(false);
                child.setClickable(false);
            }
        }

I guess this optionsoff() is not working. Because it is not disabling the checkbox. I can still click on checkbox. I need help on this second method which is disabling the Recyclerview items.

Upvotes: 4

Views: 10529

Answers (2)

Eliasz Kubala
Eliasz Kubala

Reputation: 3906

Mayby try something like this?

<CheckBox
        android:id="@+id/server_is_online"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:clickable="false"
        android:text="@string/server_is_online"
        android:textSize="23sp" />

programmatically in your adapter (which extends BaseAdapter) you can add

public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowbuttonlayout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.checkbox.setChecked(true);
        holder.checkbox.setEnabled(false);
}

Good example of class extends BaseAdapter here.

Upvotes: 1

Anton Shkurenko
Anton Shkurenko

Reputation: 4327

Every CheckBox has method setEnabled.

In onBindViewHolder you can get the reference to the checkBox you want and disable it.

Something like this:

public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.checkBox.setEnabled(data.get(position).isEnabled());
}

Also an answer below is working solution through the xml.

Upvotes: 4

Related Questions