RadioGroup 嵌套 GridView 遇到的坑

RadioGroup 嵌套 GridView 实现九宫格模式,会导致RadioButton的单选效果失效,另外由于之前的代码存在模拟单选效果,因此我决定采用模拟单选来解决RadioGroup 嵌套GridView实现九宫格导致的单选失效问题,当然,该问题,还是建议自定义实现RadioGroup实现流式布局进行处理.

以下是代码贴:
dialog_reload_choice.xml

<?xml version="1.0" encoding="utf-8"?>
<com.zhy.android.percent.support.PercentLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center_vertical"
        android:text="@string/macau_reload_type"
        app:layout_heightPercent="10%h"
        app:layout_marginLeftPercent="1%h"
        app:layout_marginTopPercent="1%h"
        app:layout_textSizePercent="@string/percent_3w" />


    <com.zhy.android.percent.support.PercentLinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_heightPercent="@string/percent_25h">

        <RadioGroup
            android:id="@+id/macau_reload_type"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_marginLeftPercent="10%w"
            app:layout_marginRightPercent="10%w"
            android:gravity="center"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/macau_reload_mop"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:button="@drawable/btn_charge_bg"
                android:checked="true"
                android:gravity="center"
                android:text="@string/macau_reload_mop"
                app:layout_textSizePercent="@string/percent_15w" />

            <RadioButton
                android:id="@+id/macau_reload_rmb"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:button="@drawable/btn_charge_bg"
                android:gravity="center"
                android:text="@string/macau_reload_rmb"
                app:layout_textSizePercent="@string/percent_15w" />
        </RadioGroup>

    </com.zhy.android.percent.support.PercentLinearLayout>


    <TextView
        android:id="@+id/title_reload_charge"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center_vertical"
        app:layout_heightPercent="10%h"
        app:layout_marginLeftPercent="1%h"
        app:layout_marginTopPercent="1%h"
        app:layout_textSizePercent="@string/percent_3w" />


    <com.zhy.android.percent.support.PercentLinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:gravity="center"
        app:layout_heightPercent="@string/percent_66h">

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_marginLeftPercent="5%w"
            app:layout_marginRightPercent="5%w"
            android:layout_gravity="center"
            android:gravity="center"
            app:layout_marginTopPercent="2%h">

            <GridView
                android:id="@+id/reload_choice"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:gravity="center"
                android:horizontalSpacing="10dp"
                android:numColumns="4"
                android:scrollbars="none"
                android:verticalSpacing="10dp" />
        </RadioGroup>

    </com.zhy.android.percent.support.PercentLinearLayout>

</com.zhy.android.percent.support.PercentLinearLayout>

item_reload_value.xml

<?xml version="1.0" encoding="utf-8"?>
<com.zhy.android.percent.support.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_radiobutton"
    android:layout_width="fill_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/item_radioimg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_charge_bg"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/item_radiotext"
        android:layout_marginLeft="26dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        app:layout_textSizePercent="@string/percent_15w"
        android:layout_toRightOf="@+id/item_radioimg" />
</com.zhy.android.percent.support.PercentRelativeLayout>
package com.keytop.TITS.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.IdRes;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.keytop.TITS.BaseActivity;
import com.keytop.TITS.R;
import com.keytop.TITS.data.MyApplication;
import com.keytop.TITS.data.Parameters;
import com.keytop.TITS.event.UIEvent;
import com.keytop.TITS.util.I18NData;
import com.keytop.TITS.util.SmallUtils;
import com.keytop.commons.lang.SizeUtils;

import java.util.ArrayList;
import java.util.HashMap;

import de.greenrobot.event.EventBus;

/**
 * 澳门通充值选项
 * Created by fengwenhua on 2017/4/26.
 */

public class MacauReloadChoice {
    private final static String KEY_MACAU_SELECT="reloadSelect";
    private final static String KEY_MACAU_VALUE="reloadValue";
    private int currentSelectItemId;
    /**当前选中的数据*/
    private SimpleAdapter adapter;
    private GridView gridView;
    private RadioGroup radioGroup;
    private View dialog_reload_choice;
    private TextView title_reload_charge;

    public enum ButtonStatus{
        SELECT,UNSELECT;
    }


    /**重置当前选项是否选中*/
    @SuppressWarnings("unchecked")
    private void resetBackground(SimpleAdapter adapter, int selectedItem,ButtonStatus status) {
        HashMap<String, Object> map = (HashMap<String, Object>)adapter.getItem(selectedItem);
        if (status.name().equalsIgnoreCase(ButtonStatus.UNSELECT.name())) {
            map.put(KEY_MACAU_SELECT, R.drawable.btn_charge_false);
        }else {
            map.put(KEY_MACAU_SELECT, R.drawable.btn_charge_true);
        }
        adapter.notifyDataSetChanged();
    }

    public interface MacauReloadCallback{
        void reloadFail(String errorTips);
        void reloadSuccess();
    }

    public void displayMacauReload(Activity activity, final MacauReloadCallback macauReloadCallback){
        currentSelectItemId = 0;//初始化
        dialog_reload_choice = activity.getLayoutInflater().inflate(R.layout.dialog_reload_choice,null);
        title_reload_charge = (TextView) dialog_reload_choice.findViewById(R.id.title_reload_charge);
        radioGroup = (RadioGroup) dialog_reload_choice.findViewById(R.id.macau_reload_type);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.macau_reload_rmb:
                        Parameters.reloadType = Parameters.ReloadStatus.RMB.name();
                        title_reload_charge.setText(String.format(I18NData.getI18NString(R.string.macau_reload_charge),Parameters.ReloadStatus.RMB.name()));
                        break;
                    default://case R.id.macau_reload_mop:
                        Parameters.reloadType = Parameters.ReloadStatus.MOP.name();
                        title_reload_charge.setText(String.format(I18NData.getI18NString(R.string.macau_reload_charge),Parameters.ReloadStatus.MOP.name()));
                        break;
                }
            }
        });
        adapter = getRadioButtonAdapter(activity);
        gridView = (GridView) dialog_reload_choice.findViewById(R.id.reload_choice);
        gridView.setAdapter(adapter);
        //初始化
        Parameters.reloadValue = SmallUtils.obtainMacauReloadValue(MyApplication.instance.getmKtTitsConfig().getMacauReloadValues()[currentSelectItemId]);
        resetBackground(adapter,currentSelectItemId,ButtonStatus.SELECT);//选中标志
        title_reload_charge.setText(String.format(I18NData.getI18NString(R.string.macau_reload_charge),Parameters.ReloadStatus.MOP.name()));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position!=currentSelectItemId){
                    resetBackground(adapter,currentSelectItemId,ButtonStatus.UNSELECT);//重置状态标志
                    currentSelectItemId = position;
                    resetBackground(adapter,position,ButtonStatus.SELECT);//选中标志
                    Parameters.reloadValue = SmallUtils.obtainMacauReloadValue(MyApplication.instance.getmKtTitsConfig().getMacauReloadValues()[position]);
                }
            }
        });
        AlertDialog dialog = new AlertDialog.Builder(activity).setTitle(R.string.macau_reload_tips).setView(dialog_reload_choice).setPositiveButton(I18NData.getI18NString(R.string.activity_charge_confirm), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (Parameters.reloadValue>0) {
                    UIEvent reloadEvent = new UIEvent(Parameters.MSG_MACAU_RELOAD_CONFIRM);
                    reloadEvent.setToastMsg(String.format(I18NData.getI18NString(R.string.macau_reload_confirm), SizeUtils.moneyFenToYuan(Parameters.reloadValue)));
                    EventBus.getDefault().post(reloadEvent);
                }else {
                    macauReloadCallback.reloadFail(I18NData.getI18NString(R.string.macau_reload_value_illegal));
                }
            }
        }).setNegativeButton(I18NData.getI18NString(R.string.cancel),null).show();

        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (BaseActivity.SCREEN_HEIGHT * 0.6); // 高度设置为屏幕的0.5
        p.width = (int) (BaseActivity.SCREEN_WIDTH * 0.45); // 宽度设置为屏幕的0.45
        dialogWindow.setAttributes(p);
    }


    /**生成Adapter*/
    public SimpleAdapter getRadioButtonAdapter(Activity activity) {
        ArrayList<HashMap<String, Object>> data = new ArrayList<>();
        for (int i = 0; i < MyApplication.instance.getmKtTitsConfig().getMacauReloadValues().length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put(KEY_MACAU_VALUE, MyApplication.instance.getmKtTitsConfig().getMacauReloadValues()[i]);
            data.add(map);
        }
        SimpleAdapter simperAdapter = new SimpleAdapter(activity, data, R.layout.item_reload_value, new String[] {KEY_MACAU_SELECT,KEY_MACAU_VALUE}, new int[] {R.id.item_radioimg, R.id.item_radiotext});
        return simperAdapter;
    }
}

模拟效果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容