本文介绍了带有 RadioButton 单选的自定义 ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望你们中的一些人可以帮助我:
I hope that some of you can help me with this:
我有一个自定义列表视图2 个文本视图和 1 个单选按钮.
I got a custom listview with 2 textviews and 1 radio button.
我想把它作为一个单一的选择,但每次我单击列表视图上的项目,它不会删除从另一个单选按钮检查".
我的xml代码:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvMaterialName"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:padding="5dp"
android:text="MaterialName"
android:textSize="35px" />
<TextView
android:id="@+id/tvMaterialNo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:text="MaterialNo"
android:textSize="1px"
android:visibility="invisible" />
</LinearLayout>
<RadioButton
android:id="@+id/rdBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:checked="false" />
</LinearLayout
还有我的 onItemClickListener:
And my onItemClickListener:
searchList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
LinearLayout item_view = (LinearLayout) v;
RadioButton itemcheck = (RadioButton) item_view
.findViewById(R.id.rdBtn);
itemcheck.setChecked(true);
if (itemcheck.isChecked()) {
itemcheck.setChecked(true);
} else {
itemcheck.setChecked(false);
}
}
});
我的适配器中的 getView()
My getView() in my adapter
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final Materials materialItem = getItem(position);
parent.setClickable(true);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.d, null, false);
viewHolder = new ViewHolder();
viewHolder.tvMaterialName = (TextView) convertView
.findViewById(R.id.tvMaterialName);
viewHolder.tvMaterialNo = (TextView) convertView
.findViewById(R.id.tvMaterialNo);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvMaterialName.setText(materialItem.getMaterialName());
viewHolder.tvMaterialNo.setText(materialItem.getMaterialNo());
return convertView;
感谢您的帮助
推荐答案
试试这样:
public class CheckedLinearLayout extends RelativeLayout implements
Checkable {
private boolean isChecked;
private List<Checkable> checkableViews;
public CheckedLinearLayout (Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
initialise(attrs);
}
public CheckedLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
initialise(attrs);
}
public CheckedLinearLayout (Context context, int checkableId) {
super(context);
initialise(null);
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
for (Checkable c : checkableViews) {
c.setChecked(isChecked);
}
}
public void toggle() {
this.isChecked = !this.isChecked;
for (Checkable c : checkableViews) {
c.toggle();
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final int childCount = this.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(this.getChildAt(i));
}
}
/**
* Read the custom XML attributes
*/
private void initialise(AttributeSet attrs) {
this.isChecked = false;
this.checkableViews = new ArrayList<Checkable>(5);
}
/**
* Add to our checkable list all the children of the view that implement the
* interface Checkable
*/
private void findCheckableChildren(View v) {
if (v instanceof Checkable) {
this.checkableViews.add((Checkable) v);
}
if (v instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) v;
final int childCount = vg.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(vg.getChildAt(i));
}
}
}
}
这篇关于带有 RadioButton 单选的自定义 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!