这篇博客呢主要是写gridview的多选以及单选的功能,并且获取选中的值。
首先呢,我做一下声明,这个小程序呢是我借鉴某位大神的部分代码,按照自己的需求重新编写了一下。本来想注上链接,可是应该是之前看的,所以实在找不到了。
下面是我要实现的功能。这个布局里面有两个gridview。红色的球可以进行多选,蓝色的球只能是单选。item的布局里面放了三个imageview,分别存放灰色,红色和蓝色的圆形图片,蓝色和红色的图片设置gone的属性。还有一个textView就是显示圆圈里的数字。
选球之后点击下一步,就会显示下面的页面,上面显示的数字就是我之前选种的号码。
下面的我给大家贴一下核心的代码
/**
* 继承MultiChoiceModeListener接口实现gridview的单选和多选,并获取相应的数据*/
public class HomeActivity extends Activity implements MultiChoiceModeListener {
private GridView mGridView_red; //红球的gridview
private GridAdapter mGridAdapterRed; //红球的适配器
private GridView gridview_bule; //蓝球的gridview
private GridAdapter mGridAdapterBlue; //蓝球的适配器
private Button button; //下一步按钮
private Map<Integer, Boolean> mSelectMapRed = new HashMap<Integer, Boolean>(); //存放选择红球的数字的集合
private Map<Integer, Boolean> mSelectMapBlue = new HashMap<Integer, Boolean>(); //存放选择蓝球的数字的集合
Map<Integer, Integer> mImgIds_red = new HashMap<Integer,Integer>(); //存放红球的集合
Map<Integer, Integer> mImgIds_blue = new HashMap<Integer,Integer>(); //存放蓝球的集合
/** 程序从此开始运行 */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//显示33个红球区域的图片
for (int i = 0; i < 33; i++) {
mImgIds_red.put(i,R.drawable.ball);
}
//显示8个蓝球区域的图片
for (int i = 0; i < 8; i++) {
mImgIds_blue.put(i,R.drawable.ball);
}
mGridView_red = (GridView) findViewById(R.id.gridview_red); //初始化红球的gridview
button = (Button) findViewById(R.id.button); //初始化下一步按钮
mGridView_red.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);// 设置为多选模式
mGridAdapterRed = new GridAdapter(this); //为红球new一个适配器
mGridView_red.setAdapter(mGridAdapterRed);// 红球数据适配
mGridView_red.setMultiChoiceModeListener(this);// 设置红球多选模式监听器
gridview_bule = (GridView)findViewById(R.id.gridview_bule); //初始化蓝球的gridview
gridview_bule.setChoiceMode(GridView.CHOICE_MODE_SINGLE);// 设置为多选模式
mGridAdapterBlue = new GridAdapter(this,"blue"); //为蓝球new一个适配器并且传入蓝球的标识
gridview_bule.setAdapter(mGridAdapterBlue);// 数据适配
gridview_bule.setMultiChoiceModeListener(this);//设置蓝球单选模式监听器
//监听按钮
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String select_red_ball = ""; //红球的数字
String select_blue_ball = ""; //蓝球的数字
List<Integer> selectednum_red = new ArrayList<Integer>(); //存放选中红球数字的list
SparseBooleanArray selectedposition_red;
selectedposition_red = mGridView_red.getCheckedItemPositions();
for (int i = 0; i < mImgIds_red.size(); i++) {
if(selectedposition_red.get(i)){
selectednum_red.add(i+1);
}
}
for (int i = 0; i < selectednum_red.size(); i++) {
select_red_ball=select_red_ball+selectednum_red.get(i)+" ";
}
List<Integer> selectednum_blue = new ArrayList<Integer>(); //存放选中蓝球的数字
SparseBooleanArray selectedposition_blue;
selectedposition_blue = gridview_bule.getCheckedItemPositions();
for (int i = 0; i < mImgIds_blue.size(); i++) {
if(selectedposition_blue.get(i)){
selectednum_blue.add(i+1);
}
}
for (int i = 0; i < selectednum_blue.size(); i++) {
select_blue_ball=""+selectednum_blue.get(i)+" ";
}
//跳转到下一个页面,并显示数据
Intent intent = new Intent();
intent.setClass(HomeActivity.this, ShowNumberActivity.class);
intent.putExtra("select", select_blue_ball+select_red_ball); //传数据
startActivity(intent);
}
});
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mGridAdapterRed.notifyDataSetChanged();
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
mSelectMapRed.put(position, checked);/* 放入选中的集合中 */
mode.invalidate();
}
//适配器
private class GridAdapter extends BaseAdapter {
private Context mContext;
private String mType = "red";
public GridAdapter(Context ctx) {
mContext = ctx;
}
public GridAdapter(Context ctx,String type) {
mContext = ctx;
mType = type;
}
@Override
public int getCount() {
return (mType.equals("red") ? mImgIds_red:mImgIds_blue).size();
}
@Override
public Integer getItem(int position) {
return Integer.valueOf((mType.equals("red")?mImgIds_red:mImgIds_blue).get(position).toString());
}
@Override
public long getItemId(int position) {
return position;
}
@Override
/* 得到View */
public View getView(int position, View convertView, ViewGroup parent) {
GridItem item;
if (mType.equals("red")) {
if (convertView == null) {
item = new GridItem(mContext);
item.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
} else {
item = (GridItem) convertView;
}
item.setImgResId(getItem(position));
item.setItemText(String.valueOf(position+1));
item.setChecked(mSelectMapRed.get(position) == null ? false
: mSelectMapRed.get(position));
}else {
if (convertView == null) {
item = new GridItem(mContext,"blue");
item.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
} else {
item = (GridItem) convertView;
}
item.setImgResId(getItem(position));
item.setItemText(String.valueOf(position+1));
item.setChecked(mSelectMapBlue.get(position) == null ? false
: mSelectMapBlue.get(position));
}
return item;
}
}
}
还有一个设置点击图片的效果的自定义view
/**
* 自定义View继承Checable接口*/
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class GridItem extends RelativeLayout implements Checkable {
private Context mContext;
private boolean mChecked;//判断该选项是否被选上的标志量
private ImageView mImgView = null; //点击前的图片
private ImageView mSecletView = null; //点击红球的图片
private ImageView mSecletBlue = null; //点击篮球的图片
private TextView mItemText = null; //显示的数字
private String mType = "red"; //标识是篮球还是红球的点击
public GridItem(Context context) {
this(context, null, 0);
}
public GridItem(Context context,String mType) {
this(context, null, 0);
this.mType = mType;
}
public GridItem(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GridItem(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
LayoutInflater.from(mContext).inflate(R.layout.grid_item, this); //初始化item布局
mImgView = (ImageView) findViewById(R.id.img_view); //初始化原始布局id
mSecletView = (ImageView) findViewById(R.id.select_red); //红球的id
mItemText=(TextView) findViewById(R.id.itemText); //号码的id
mSecletBlue = (ImageView)findViewById(R.id.select_blue); //篮球的id
}
@Override
public void setChecked(boolean checked) {
// TODO Auto-generated method stub
mChecked = checked;
if (mType.equals("red")) {
mSecletView.setVisibility(checked ? View.VISIBLE : View.GONE);//选上了图片变成红色
}else {
mSecletBlue.setVisibility(checked ? View.VISIBLE : View.GONE);//选上了图片变成蓝色
}
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
public void setImgResId(int resId) {
if (mImgView != null) {
mImgView.setBackgroundResource(resId);//设置背景
}
}
public void setItemText(String itemText) {
mItemText.setText(itemText); //设置文字
}
}
这样呢我们就能够做出这样的效果了。