java - 单击一个后禁用回收器 View 中的所有其他切换按钮

标签 java android android-recyclerview toggle

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

  • 当用户通过单击某个按钮“打开”该按钮时,会“关闭”所有其他按钮

我尝试在 View 持有者类中添加一个onClickListener,并调用notifyDataSetChanged(),这样onBindViewHolder(final ViewHolderholder, int position) 被调用,然后更改按钮的状态。

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

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 应该只包含 View ,而不包含点击处理程序或字符串等(线索就在名称中)。当您将 ViewHolder 绑定(bind)到数据类时,您可以添加处理程序并操作 View 。

据我所知,您需要一个简单的切换按钮列表。当一个按钮打开时,其他按钮应该关闭。我在下面为您创建的测试适配器演示了这一点。

理想情况下,您应该避免 notifyDataSetChanged 并使用基于行的版本,但每次按下切换按钮时,它都会影响所有其他行,在此用例中您实际上没有选择,除非您跟踪所选行。

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

<?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>

关于java - 单击一个后禁用回收器 View 中的所有其他切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520390/

相关文章:

android - Recycler View 添加删除项目动画未显示在 swapCursor 中

java - Netty:我应该创建新的 ChannelBuffer 来向每个客户端发送数据吗?

java - LDAPContext.search() 返回空结果

android - 扩展 RecyclerView : Error Inflating Class com. example.MyRecyclerView

Android SurfaceView Canvas 层

android - FileExplorer eclipse 作为 root

android - NestedScrollView Android 中的 RecyclerView

java - 在 for 循环中使用 continue : the order it produces

java - 简单的 Java 网络服务

android - Firebase token 刷新