请禁用所有其他切换按钮

请禁用所有其他切换按钮

本文介绍了单击回收器视图后,请禁用所有其他切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用recyclerView构建切换按钮列表(例如小键盘),我想实现:

I am using recyclerView to build a list of toggle buttons (like a numpad), I would like to achieve:

  • 当用户通过单击打开"按钮时,关闭"所有其他按钮

我试图在视图持有者类中添加一个onClickListener,然后调用notifyDataSetChanged()以便调用onBindViewHolder(final ViewHolder holder, int position),然后更改按钮的状态.

I have tried to add an onClickListener inside the view holder class, and call notifyDataSetChanged() such that onBindViewHolder(final ViewHolder holder, int position) is called and then change the state of the button.

但是它不起作用,单击的按钮不会更改其状态.

But it doesn't work, the button clicked does not change its state.

ViewHolder

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    ToggleButton button;
    String id;

    public ViewHolder(View itemView) {
        super(itemView);
        button = (ToggleButton) itemView.findViewById(R.id.toggle);
        button.setOnclickListener(this);
    }

    @Override
    public void onClick(View view) {
        ((ToggleButton) view).setChecked(true);
        int pos = getAdapterPosition();
        if (pos != RecyclerView.NO_POSITION) {
            selected = pos;  //store the position of the user clicked button
            notifyDataSetChanged();
        }
    }

我的adpater类中的onBindViewHolder()

public void onBindViewHolder(final ViewHolder holder, int position) {
    String data = mData.get(position);
    holder.id = data;
    holder.button.setText(data);
    holder.button.setTextOn(data);
    holder.button.setTextOff(data);

    if (position != selected) { //if the button is not the user chosen one
        if (holder.button.isChecked()) { // and it's in 'ON' state
            holder.button.toggle(); // toggle it to switch 'OFF'
        }
    }

推荐答案

您做错了几件事. ViewHolder应该仅包含视图,而不能包含单击处理程序或字符串等.(线索在名称中).将ViewHolder绑定到数据类时,可以添加处理程序并操纵视图.

There are a couple of things you are doing incorrectly. The ViewHolder should contain only views and not click handlers or strings etc. (The clue is in the name). You add the handlers and manipulate the views when you bind the ViewHolder to your data class.

根据我的判断,您需要一个简单的切换按钮列表.当一个按钮打开时,其他按钮应关闭.我在下面为您创建的测试适配器证明了这一点.

From what I can determine, you want a simple list of toggle buttons. When one button is on, the others should turn off. The test adapter I created for you below demonstrates that.

理想情况下,您应该避免使用notifyDataSetChanged并使用基于行的版本,但是每次按下切换开关时,它都会影响其他所有行,因此在此用例中您别无选择,除非您跟踪所选内容行.

Ideally, you would avoid notifyDataSetChanged and use the row based versions but as each time the toggle is pressed, it affects every other row, you do not really have a choice in this use case unless you keep track of the selected row.

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.VH> {

public static class MyData {
    public boolean Selected = false;
    public String Text;

    public MyData(String text) {
        Text = text;
    }
}

public List<MyData> items = new ArrayList<>();

public TestAdapter() {
    this.items.add(new MyData("Item 1"));
    this.items.add(new MyData("Item 2"));
    this.items.add(new MyData("Item 3"));
}

@Override
public TestAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
    return new VH((
            LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.test_layout, parent, false))
    );
}

@Override
public void onBindViewHolder(TestAdapter.VH holder, int position) {
    final MyData itm = items.get(position);

    holder.button.setChecked(itm.Selected);
    holder.text.setText(itm.Text);

    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (MyData x: items){
                x.Selected=false;
            }
            itm.Selected = true;
            notifyDataSetChanged();
        }
    });

}

@Override
public int getItemCount() {
    return items.size();
}

public class VH extends RecyclerView.ViewHolder {

    ToggleButton button;
    TextView text;

    public VH(View itemView) {
        super(itemView);
        button = itemView.findViewById(R.id.toggle);
        text = itemView.findViewById(R.id.text1);
    }
}
}

test_layout.xml

test_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">

<ToggleButton
    android:id="@+id/toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

这篇关于单击回收器视图后,请禁用所有其他切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:54