RadioGroup 嵌套 GridView 实现九宫格模式,会导致RadioButton的单选效果失效,另外由于之前的代码存在模拟单选效果,因此我决定采用模拟单选来解决RadioGroup 嵌套GridView实现九宫格导致的单选失效问题,当然,该问题,还是建议自定义实现RadioGroup实现流式布局进行处理.
以下是代码贴:
dialog_reload_choice.xml
item_reload_value.xml
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 map = (HashMap)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> data = new ArrayList<>();
for (int i = 0; i < MyApplication.instance.getmKtTitsConfig().getMacauReloadValues().length; i++) {
HashMap map = new HashMap();
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;
}
}