【问题标题】:android how to select only one of button in adapter recycle view at view holder?android如何在viewholder的适配器recyclerview中只选择一个按钮?
【发布时间】:2019-03-22 20:22:39
【问题描述】:

如何从 3 个按钮中选择唯一的 1 个按钮?我设法设置/取消当前选定按钮的背景颜色。但无法选择唯一一个选定的按钮。

例如 btn1、btn2、btn3。 当我选择 btn1 时,btn1 背景颜色改变,btn2 和 btn3 不受影响。之后,当我再次选择 btn2 时,btn1 背景颜色未设置,btn2 背景颜色发生变化。所以此时btn2被选中,btn1和btn3没有被选中。

下面是我的示例代码:

    public class ViewHolder extends RecyclerView.ViewHolder {

    Button mButton;

    public ViewHolder(View v) {
        super(v);
        parentActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mButton= itemView.findViewById(R.id.mButton);
            }
        });
    }
}

       holder.mButton.setOnClickListener(new DebouncedOnClickListener(500) {
            @Override
            public void onDebouncedClick(View v) {
                if(!holder.mButton.isSelected()){
                    holder.mButton.setSelected(true);
                    setSelectedButton(holder);
                }
                else{
                    holder.mButton.setSelected(false);
                    setSelectedButton(holder);
                }
            }
        });



    private void setSelectedButton(ViewHolder holder){
    if(holder.mButton.isSelected()){
        holder.mButton.setBackgroundColor(parentActivity.getResources().getColor(R.color.unread_notification));
    }
    else{
        holder.mButton.setBackgroundColor(parentActivity.getResources().getColor(R.color.white));
    }
}

【问题讨论】:

  • 如果一次只选择一个按钮,您可以使用选择器,而不是手动设置颜色。您可以使用单选按钮
  • 不需要使用按钮只使用视图,您在回收视图中使用。 holder.your 方法名称,所有。希望对您有所帮助。
  • @Prashant 甚至不是按钮,我的意思是如果不是按钮,其他视图(例如,约束布局、相对布局、线性布局)可以执行像按钮一样的点击,如果我选择了这个布局,我设置这个布局的背景颜色只有我们的3个布局。
  • @chetanmahajan 我使用的是线性布局,我以按钮为例
  • 您需要一次选择一个按钮吗?即如果您选择按钮一个,它的颜色会发生变化,而其他按钮颜色保持与默认相同?是这样吗?

标签: android android-studio android-recyclerview android-adapter


【解决方案1】:

您需要在 RecyclerView 中实现选择。经过一番搜索,我发现这个网站可能对你有用

https://medium.com/@maydin/multi-and-single-selection-in-recyclerview-d29587a7dee2

【讨论】:

    【解决方案2】:

    我建议你使用 CheckBox 而不是这样的按钮 -

    <RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/background"
    android:orientation="horizontal">
    
    <RadioButton
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="@drawable/button_background"
        android:button="@null"
        android:gravity="center"
        android:text="Button 1"
        android:textColor="#fff"
        android:checked="true"
        android:textStyle="bold" />
    
    <RadioButton
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="@drawable/button_background"
        android:button="@null"
        android:gravity="center"
        android:text="Button 2"
        android:textColor="#fff"
        android:textStyle="bold" />
    
    <RadioButton
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="@drawable/button_background"
        android:button="@null"
        android:gravity="center"
        android:text="Button 3"
        android:textColor="#fff"
        android:textStyle="bold" />
     </RadioGroup>
    

    对于按钮背景 -

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle" >
            <corners android:radius="50dp"/>
            <solid android:color="#c7c7c7"/>
        </shape>
    </item>
    
    <item android:state_checked="true">
        <shape android:shape="rectangle" >
            <corners android:radius="50dp"/>
            <gradient android:startColor="#79ccff"
                android:endColor="#7900ca"/>
        </shape>
    </item>
    </selector>
    

    而对于背景 -

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="50dp" />
    <solid android:color="#c7c7c7" />
    </shape>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      相关资源
      最近更新 更多