Android 在水平滚动视图中,所有单选按钮都被选中,而只有一个

Android 在水平滚动视图中,所有单选按钮都被选中,而只有一个,android,horizontalscrollview,radio-group,Android,Horizontalscrollview,Radio Group,我试图扩展一个内部有一个RadioGroup的HorizontalScrollView,当我填充RadioGroup时,所有按钮都会在单击时被选中,而不是只选择我单击的按钮 this is my class: package widgets; import java.util.ArrayList; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.d

我试图扩展一个内部有一个RadioGroup的HorizontalScrollView,当我填充RadioGroup时,所有按钮都会在单击时被选中,而不是只选择我单击的按钮

this is my class:

package widgets;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.HorizontalScrollView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class HorizontalListView extends HorizontalScrollView {

    ArrayList<String> lista = new ArrayList<String>();
    ArrayList<RadioButton> listaButton = new ArrayList<RadioButton>();
    int selected = -1;
    Drawable itemSelector;
    Context context;
    android.widget.CompoundButton.OnCheckedChangeListener listener;
    RadioGroup group;

    public HorizontalListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();

    }

    public HorizontalListView(Context context, ArrayList<String> stringhe, Drawable itemBackground) {
        super(context);
        this.context = context;
        this.lista = stringhe;
        this.itemSelector = itemBackground;
        initView();

    }

    @SuppressLint("NewApi")
    private synchronized void initView() {
        this.removeAllViews();
        group = new RadioGroup(getContext());
        group.setOrientation(RadioGroup.HORIZONTAL);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(group, params);
        int i = 0;
        for (String string : lista) {
            RadioButton button = new RadioButton(getContext());
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                button.setBackgroundDrawable(itemSelector);

            } else {
                button.setBackground(itemSelector);
            }

                button.setButtonDrawable(new StateListDrawable());

            button.setText(string);

            button.setTag(i);
            LayoutParams paramsButton = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            button.setPadding(20, 0, 20, 0);
            listaButton.add(button);
            group.addView(button, paramsButton);
            i++;

        }




    }

现在我的文本视图上有了复选框,但是当背景与选择不同步时,滚动时会改变,组中的另一个单选按钮会在我按下一个按钮时改变其选择状态复选标记是正确的。。。有一种方法可以将statelist drawable作为按钮的背景,而不是按钮左侧的默认值

您可以演示如何将选择器实例化为drawable吗?我在上面的代码中没有看到任何问题。虽然在代码中创建填充时,我会将这些值乘以屏幕密度,因此使用的是与密度无关的单位,而不是像素。listaGiorni.setDrawablegetResources.getDrawableR.drawable.button_giorni_selector;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:state_pressed="false"><shape xmlns:android="http://schemas.android.com/apk/res/android">
            <stroke android:width="1dp" android:color="@color/sinottica_button_border" />

            <solid android:color="@color/sinottica_button_bg_selected" />

            <padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
        </shape></item>
    <item android:state_checked="false" android:state_pressed="false"><shape xmlns:android="http://schemas.android.com/apk/res/android">
            <stroke android:width="1dp" android:color="@color/sinottica_button_border" />

            <solid android:color="@color/sinottica_button_bg" />

            <padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
        </shape></item>
    <item android:state_checked="true" android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android">
            <stroke android:width="1dp" android:color="@color/sinottica_button_border" />

            <solid android:color="@color/sinottica_button_bg_selected" />

            <padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
        </shape></item>
    <item android:state_checked="false" android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android">
            <stroke android:width="1dp" android:color="@color/sinottica_button_border" />

            <solid android:color="@color/sinottica_button_bg" />

            <padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
        </shape></item>

</selector>
 button.setButtonDrawable(new StateListDrawable());