gpt4 book ai didi

android - 带有RadioButton单选的自定义ListView

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:44 24 4
gpt4 key购买 nike

我希望你们中的一些人可以帮助我:

我有一个自定义 ListView 2 个 TextView 和 1 个单选按钮。

我想把它作为一个单一的选择,但每次我单击 ListView 上的项目,它不会删除从另一个单选按钮“检查”。

我的 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:

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()

@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));
}
}
}
}

关于android - 带有RadioButton单选的自定义ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22630339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com