我正在尝试使用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++;
}
}
这是我的选择器,我做了所有状态我需要选择正确的一个,但正如我所说的无线电组选择所有项目(如果我滚动horizontalscrollview它们也会被选中)
<?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>
更新:
我试图不删除StateListDrawable,所以我评论了这一行
button.setButtonDrawable(new StateListDrawable());
现在我的textview上有一个复选框,但是当背景与选择不同时,(当我滚动时更改,当我触摸一个按钮时组中的另一个单选按钮改变其选择状态)复选标记是正确的...有一种方法可以将statelist drawable作为Button的背景而不是默认值(在按钮的左侧)