最近做一个项目,有很多项需要单选,但android上RadioGroup不能实现RadioButton自动换行的效果,于是通过另一种方式来实现类似效果.
package com.dq.slidemenu;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class SearchMovieActivity extends Activity {
private GridView gridViewType;
private GridView gridViewArea;
private GridView gridViewTime;
private TextView title;
private Button back;
private Button rightBT;
private OnClickListener MyOnClickListener;
private SimpleAdapter adater1;
private SimpleAdapter adater2;
private SimpleAdapter adater3;
private ArrayList> mList1;
private ArrayList> mList2;
private ArrayList> mList3;
private static final int listContent1[] = {R.string.search_type_all,R.string.search_type_love,
R.string.search_type_biography,R.string.search_type_cartoon,R.string.search_type_action,
R.string.search_type_ancient,R.string.search_type_child,R.string.search_type_dance,
R.string.search_type_family,R.string.search_type_frightened,R.string.search_type_terror,
R.string.search_type_fiction,R.string.search_other};
private static final int listContent2[] = {R.string.search_type_all,R.string.search_area_china,
R.string.search_area_china_hk,R.string.search_area_usa,R.string.search_area_europe,
R.string.search_area_kj,R.string.search_area_tailand,R.string.search_other};
private static final int listContent3[] = {R.string.search_time_all,R.string.search_time_2013,
R.string.search_time_2012,R.string.search_time_2011,R.string.search_time_2010,
R.string.search_time_2000,R.string.search_time_90,R.string.search_time_other};
private static final int TYPE_NUM = 13;
private static final int AREA_NUM = 8;
private static final int TIME_NUM = 8;
private int typeLastItem = 0;
private int areaLastItem = 0;
private int timeLastItem = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initMainUI();
initMainUIListener();
}
private void initMainUI() {
setContentView(R.layout.activity_search2);
title = (TextView)findViewById(R.id.title_activity);
if (title != null) {
title.setText(R.string.search_movie);
}
back = (Button)findViewById(R.id.back);
if (back != null) {
back.setVisibility(View.VISIBLE);
}
rightBT = (Button)findViewById(R.id.rightBT);
if (rightBT != null) {
rightBT.setVisibility(View.VISIBLE);
rightBT.setText(R.string.search);
}
initGridViewOne();
initGridViewTwo();
initGridViewThree();
}
private void initGridViewOne() {
gridViewType = (GridView)findViewById(R.id.search_type);
mList1 = new ArrayList>();
for (int i = 0; i < TYPE_NUM; i++) {
HashMap map = new HashMap();
map.put("radioIcon", R.drawable.radiobutton_off);
map.put("radioText", getString(listContent1[i]));
mList1.add(map);
}
adater1 = new SimpleAdapter(getApplicationContext(), mList1, R.layout.search_grid_item,
new String[]{"radioIcon","radioText"}, new int[]{R.id.item_RadioImg,R.id.item_RadioText});
if (gridViewType != null) {
gridViewType.setAdapter(adater1);
gridViewType.requestFocus();
gridViewType.setOnItemClickListener(new MyTypeOnItemClick());
}
}
private void initGridViewTwo() {
gridViewArea = (GridView)findViewById(R.id.search_area);
mList2 = new ArrayList>();
for (int i = 0; i < AREA_NUM; i++) {
HashMap map = new HashMap();
map.put("radioIcon", R.drawable.radiobutton_off);
map.put("radioText", getString(listContent2[i]));
mList2.add(map);
}
adater2 = new SimpleAdapter(getApplicationContext(), mList2, R.layout.search_grid_item,
new String[]{"radioIcon","radioText"}, new int[]{R.id.item_RadioImg,R.id.item_RadioText});
if (gridViewArea != null) {
gridViewArea.setAdapter(adater2);
gridViewArea.requestFocus();
gridViewArea.setOnItemClickListener(new MyAreaOnItemClick());
}
}
private void initGridViewThree() {
gridViewTime = (GridView)findViewById(R.id.search_time);
mList3 = new ArrayList>();
for (int i = 0; i < TIME_NUM; i++) {
HashMap map = new HashMap();
map.put("radioIcon", R.drawable.radiobutton_off);
map.put("radioText", getString(listContent3[i]));
mList3.add(map);
}
adater3 = new SimpleAdapter(getApplicationContext(), mList3, R.layout.search_grid_item,
new String[]{"radioIcon","radioText"}, new int[]{R.id.item_RadioImg,R.id.item_RadioText});
if (gridViewTime != null) {
gridViewTime.setAdapter(adater3);
gridViewTime.requestFocus();
gridViewTime.setOnItemClickListener(new MyTimeOnItemClick());
}
}
private void goToSearch() {
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.search_result, null);
setContentView(layout);
title = (TextView)layout.findViewById(R.id.title_activity);
if (title != null) {
title.setText(R.string.search_result);
}
back = (Button)layout.findViewById(R.id.back);
if (back != null) {
back.setVisibility(View.VISIBLE);
back.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
private void initMainUIListener() {
MyOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.back:
finish();
break;
case R.id.rightBT:
goToSearch();
break;
default:
break;
}
}
};
if (back != null) {
back.setOnClickListener(MyOnClickListener);
}
if (rightBT != null) {
rightBT.setOnClickListener(MyOnClickListener);
}
}
private void changeItemImg(SimpleAdapter sa, int selectedItem, boolean isOn) {
HashMap map = (HashMap)sa.getItem(selectedItem);
if (isOn) {
map.put("radioIcon", R.drawable.radiobutton_on);
} else {
map.put("radioIcon", R.drawable.radiobutton_off);
}
sa.notifyDataSetChanged();
}
private class MyTypeOnItemClick implements OnItemClickListener {
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
System.out.println("search_type");
if (typeLastItem != position) {
if (typeLastItem >= 0) {
changeItemImg(adater1, typeLastItem, false);
}
}
typeLastItem = position;
changeItemImg(adater1, position, true);
HashMap map = (HashMap)adater1.getItem(position);
String typeStr = (String)map.get("radioText");
System.out.println("Type string:"+typeStr);
}
}
private class MyAreaOnItemClick implements OnItemClickListener {
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
System.out.println("search_area");
if (areaLastItem != position) {
if (areaLastItem >= 0) {
changeItemImg(adater2, areaLastItem, false);
}
}
areaLastItem = position;
changeItemImg(adater2, position, true);
HashMap map = (HashMap)adater2.getItem(position);
String areaStr = (String)map.get("radioText");
System.out.println("Area string:"+areaStr);
}
}
private class MyTimeOnItemClick implements OnItemClickListener {
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
System.out.println("search_time");
if (timeLastItem != position) {
if (areaLastItem >= 0) {
changeItemImg(adater3, timeLastItem, false);
}
}
timeLastItem = position;
changeItemImg(adater3, position, true);
HashMap map = (HashMap)adater3.getItem(position);
String timeStr = (String)map.get("radioText");
System.out.println("Time string:"+timeStr);
}
}
}