【问题标题】:Adding RadioButtons from inflated listview layout into RadioGroup将膨胀的列表视图布局中的 RadioButtons 添加到 RadioGroup 中
【发布时间】:2013-06-26 02:00:13
【问题描述】:

我有一个列表视图(它被布局膨胀)和一个扩展 BaseAdapter 的自定义适配器。列表视图的每一行都包含一个单选按钮、一个文本视图和一个图像视图。

我的问题是我无法将所有单选按钮添加到 RadioGroup 中。我想控制单选按钮,以便随时只能选择一个。

有人可以帮忙吗?

我的listview布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/lv_activity_object_setup_final_objects"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

我的 XML 曾经膨胀过:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RadioButton
    android:id="@+id/rbn_activity_object_setup_final_list_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="22dp"
    android:layout_marginTop="22dp"
    android:text="" android:button="@drawable/radio_button"/>

<ImageView
    android:id="@+id/img_activity_object_setup_final_list_layout_div_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="22dp"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@id/rbn_activity_object_setup_final_list_layout"
    android:src="@drawable/table_divider_vertical" />

<TextView
    android:id="@+id/txv_activity_object_setup_final_list_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="22dp"
    android:layout_toRightOf="@id/img_activity_object_setup_final_list_layout_div_vertical"
    android:text="Object"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/img_activity_object_setup_final_list_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="16dp"
    android:src="@drawable/object_icon_keys" />

我的自定义适配器代码:

    class ObjectAdapter extends BaseAdapter {
    Context context;
    List<ObjectType> objectTypeData;
    LayoutInflater inflater;

    public ObjectAdapter(Context context, List<ObjectType> objectTypeList) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.objectTypeData = objectTypeList;
    }

    @Override
    public int getCount() {
        return objectTypeData.size();
    }

    @Override
    public Object getItem(int position) {
        return objectTypeData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewGroup vg;

        if (convertView != null) {
            vg = (ViewGroup) convertView;
        } else {
            vg = (ViewGroup) inflater.inflate(R.layout.activity_object_setup_final_list_layout, null);
        }

        ObjectType objectType = objectTypeData.get(position);

        RadioButton radioButton = (RadioButton) vg.findViewById(R.id.rbn_activity_object_setup_final_list_layout);
        radioButton.setButtonDrawable(getResources().getDrawable(R.drawable.radio_button));
        if (objectType.getRadioButtonImage() == strDisabled) {
            radioButton.setEnabled(false);
            radioButton.setChecked(true);
        }
        else if (objectType.getRadioButtonImage() == strChecked) {
            radioButton.setEnabled(true);
            radioButton.setChecked(true);
        }
        else {
            radioButton.setEnabled(true);
            radioButton.setChecked(false);
        }

        TextView objectName = (TextView) vg.findViewById(R.id.txv_activity_object_setup_final_list_layout);
        objectName.setText(objectType.getTitle());

        ImageView objectImage = (ImageView) vg.findViewById(R.id.img_activity_object_setup_final_list_layout);
        if (objectType.getObjectTypeImage() == getString(R.string.strKeys)) {
            objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_keys));
        }
        else if (objectType.getObjectTypeImage() == getString(R.string.strWallet)) {
            objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_wallet));
        }
        else if (objectType.getObjectTypeImage() == getString(R.string.strBag)) {
            objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_bag));
        }
        else if (objectType.getObjectTypeImage() == getString(R.string.strLaptop)) {
            objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_laptop));
        }
        else {
            objectImage.setImageDrawable(getResources().getDrawable(R.drawable.object_icon_default));
        }

        return vg;
    }
}

【问题讨论】:

  • 为什么需要 RadioGroup?您是否尝试过从您的适配器控制单次检查? ObjectType 中包含哪些数据?
  • 哦,我想让控制自动化,这样我就不必编写检查代码了。 ObjectType 是包含 3 项的模态数据层: public String radioButtonImage;公共字符串标题;公共字符串对象类型图像;有关如何在适配器中对其进行编码的任何线索?
  • 您可以尝试跟踪当前检查的(例如其索引)项目并在选择另一个项目时更新它。在适配器中,您需要检查 position == checkedIndex 并相应地设置项目状态。
  • 谢谢!我已经按照下面的方法完成了。

标签: android android-listview


【解决方案1】:
  1. 将单选按钮添加到 OnClickListener。
  2. 在 OnClick 事件中,保存 selectedItem 索引。
  3. 调用适配器的notifyDataSetChanged方法。
  4. 在适配器中,检查 selectedItem==position 是否应决定是否应检查单选按钮。

radioButton.setChecked(selectionIndex==position);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2017-09-18
    相关资源
    最近更新 更多