自定义ListView与RadioButton单选

时间:2014-03-25 09:31:06

标签: android android-listview radio-button

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

我有一个自定义列表视图 2个textviews和1个单选按钮。

我想把它作为一个选择,但每次我都是 点击列表视图上的项目,它不会删除 "检查"从另一个单选按钮。

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

感谢您的帮助

1 个答案:

答案 0 :(得分:5)

试试这样:

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