[英]How to disable RecyclerView items in Android?
Textviews and Checkboxes are in a Recyclerview. Textview和Checkbox都在Recyclerview中。 Initially all Checkboxes are selected.
最初选中所有复选框。 I want to prevent user changing Checkboxes state but I do not want to prevent Recyclerview scroll.
我想阻止用户更改Checkboxes状态,但我不想阻止Recyclerview滚动。 I would like to do this in my fragment class not in adapter class.
我想在我的片段类中而不是在适配器类中执行此操作。
How can I prevent user changing Checkboxes state? 如何防止用户更改Checkboxes状态?
Below is the sample code written in onBindViewHolder in adapter class. 下面是适配器类中onBindViewHolder编写的示例代码。
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. 我猜这个
optionsoff()
不起作用。 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.
我需要有关第二种方法的帮助,该方法禁用了Recyclerview项目。
Every CheckBox
has method setEnabled
. 每个
CheckBox
都有方法setEnabled
。
In onBindViewHolder
you can get the reference to the checkBox you want and disable it. 在
onBindViewHolder
您可以获得所需的复选框的引用并禁用它。
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. 下面的答案是通过xml的工作解决方案。
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 您可以在您的适配器(扩展BaseAdapter)中以编程方式添加
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 . 类的好例子在这里扩展了BaseAdapter。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.