最近感觉GridView的用法比较灵活,比如写一些动态改变的东西,更新显示效果比较明显,所以我写了一个demo,选中球后,点击确定显示选中的球在主页面,点击重置,则选中的球都被清空。这个效果还是比较不错的!希望给大家启迪。项目图片我没有上传,有问题的请留言,想要源码的请留言,转载请标明出处:
http://blog.csdn.net/wdaming1986/article/details/6786444
我的csdn资源下载链接分享给大家,大家可以下载:
http://download.csdn.net/detail/wdaming1986/3611738
程序的主页面: 点击button按钮弹出dialog页面:
点击确定后的效果,点击重置如
选球,球的颜色会发生变化 第二张图片的效果,球没有选中的效果
代码奉上:
com.cn.daming包里面的类:
一、MainActivity.java类中的代码:
package com.cn.daming;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
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.Toast;
public class MainActivity extends Activity {
private GridView redBallGrid;
private GridView blueBallGrid;
private Button okButton;
private Button resetButton;
private Button dialog_button;
private GridView showRedGridView;
private GridView showBlueGridView;
private AlertDialog show_ball_dialog;
private View show_ball_dialog_view;
public static ArrayList<Integer> redBallSelectArr= new ArrayList<Integer>();
public static ArrayList<Integer> blueBallSelectArr= new ArrayList<Integer>();
private int redNumLeft = 35,blueNumLeft = 16;
private Integer[] changeimageid = {
R.drawable.redball_dark01,
R.drawable.redball_dark02,
R.drawable.redball_dark03,
R.drawable.redball_dark04,
R.drawable.redball_dark05,
R.drawable.redball_dark06,
R.drawable.redball_dark07,
R.drawable.redball_dark08,
R.drawable.redball_dark09,
R.drawable.redball_dark10,
R.drawable.redball_dark11,
R.drawable.redball_dark12,
R.drawable.redball_dark13,
R.drawable.redball_dark14,
R.drawable.redball_dark15,
R.drawable.redball_dark16,
R.drawable.redball_dark17,
R.drawable.redball_dark18,
R.drawable.redball_dark19,
R.drawable.redball_dark20,
R.drawable.redball_dark21,
R.drawable.redball_dark22,
R.drawable.redball_dark23,
R.drawable.redball_dark24,
R.drawable.redball_dark25,
R.drawable.redball_dark26,
R.drawable.redball_dark27,
R.drawable.redball_dark28,
R.drawable.redball_dark29,
R.drawable.redball_dark30,
R.drawable.redball_dark31,
R.drawable.redball_dark32,
R.drawable.redball_dark33,
R.drawable.redball_dark34,
R.drawable.redball_dark35,
};
private Integer[] mImageIds =
{
R.drawable.redball_01,
R.drawable.redball_02,
R.drawable.redball_03,
R.drawable.redball_04,
R.drawable.redball_05,
R.drawable.redball_06,
R.drawable.redball_07,
R.drawable.redball_08,
R.drawable.redball_09,
R.drawable.redball_10,
R.drawable.redball_11,
R.drawable.redball_12,
R.drawable.redball_13,
R.drawable.redball_14,
R.drawable.redball_15,
R.drawable.redball_16,
R.drawable.redball_17,
R.drawable.redball_18,
R.drawable.redball_19,
R.drawable.redball_20,
R.drawable.redball_21,
R.drawable.redball_22,
R.drawable.redball_23,
R.drawable.redball_24,
R.drawable.redball_25,
R.drawable.redball_26,
R.drawable.redball_27,
R.drawable.redball_28,
R.drawable.redball_29,
R.drawable.redball_30,
R.drawable.redball_31,
R.drawable.redball_32,
R.drawable.redball_33,
R.drawable.redball_34,
R.drawable.redball_35,
};
private Integer[] blueBallIds =
{
R.drawable.blueball_01,
R.drawable.blueball_02,
R.drawable.blueball_03,
R.drawable.blueball_04,
R.drawable.blueball_05,
R.drawable.blueball_06,
R.drawable.blueball_07,
R.drawable.blueball_08,
R.drawable.blueball_09,
R.drawable.blueball_10,
R.drawable.blueball_11,
R.drawable.blueball_12,
R.drawable.blueball_13,
R.drawable.blueball_14,
R.drawable.blueball_15,
R.drawable.blueball_16,
};
private Integer[] blueBallChangeIds =
{
R.drawable.blueball_dark01,
R.drawable.blueball_dark02,
R.drawable.blueball_dark03,
R.drawable.blueball_dark04,
R.drawable.blueball_dark05,
R.drawable.blueball_dark06,
R.drawable.blueball_dark07,
R.drawable.blueball_dark08,
R.drawable.blueball_dark09,
R.drawable.blueball_dark10,
R.drawable.blueball_dark11,
R.drawable.blueball_dark12,
R.drawable.blueball_dark13,
R.drawable.blueball_dark14,
R.drawable.blueball_dark15,
R.drawable.blueball_dark16,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawBackground();
initSelectedBallArrayList();
initBall_GridView();
initDialog_Button();
}
public void drawBackground()
{
GradientDrawable grad = new GradientDrawable(
Orientation.TL_BR,
new int[] {
Color.rgb(0, 0, 127),
Color.rgb(0, 0, 255),
Color.rgb(127, 0, 255),
Color.rgb(127, 127, 255),
Color.rgb(127, 255, 255),
Color.rgb(255, 255, 255)
}
);
this.getWindow().setBackgroundDrawable(grad);
}
private void initSelectedBallArrayList()
{
for(int i=0;i<35;i++){
redBallSelectArr.add(i,0);
}
for(int i=0;i<16;i++){
blueBallSelectArr.add(i,0);
}
}
private void initBall_GridView()
{
showRedGridView = (GridView)findViewById(R.id.show_red_gridview);
showBlueGridView = (GridView)findViewById(R.id.show_blue_gridview);
}
private void initDialog_Button()
{
dialog_button = (Button)findViewById(R.id.dialog_button);
dialog_button.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
show_ball_dialog = new AlertDialog.Builder(MainActivity.this).create();
show_ball_dialog_view = View.inflate(MainActivity.this, R.layout.ball_grid_dialog, null);
redBallGrid = (GridView)(show_ball_dialog_view).findViewById(R.id.red_ball_grid);
blueBallGrid = (GridView)(show_ball_dialog_view).findViewById(R.id.blue_ball_grid);
okButton = (Button)(show_ball_dialog_view).findViewById(R.id.Ok_Button);
resetButton = (Button)(show_ball_dialog_view).findViewById(R.id.Reset_Button);
redBallGrid.setAdapter(new ImageAdapter(MainActivity.this,MainActivity.this,redBallSelectArr));
blueBallGrid.setAdapter(new ImageAdapter(MainActivity.this,MainActivity.this,blueBallSelectArr));
show_ball_dialog.setView(show_ball_dialog_view);
show_ball_dialog.show();
redBallGrid.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(redNumLeft > 1){
if(redBallSelectArr.get(arg2) == 0){
arg1.setBackgroundResource(changeimageid[arg2]);
redBallSelectArr.set(arg2,arg2+1);
redNumLeft --;
}
else{
arg1.setBackgroundResource(mImageIds[arg2]);
redBallSelectArr.set(arg2, 0);
redNumLeft += 1;
}
}
else{
if(redBallSelectArr.get(arg2) != 0){
arg1.setBackgroundResource(mImageIds[arg2]);
redBallSelectArr.set(arg2, 0);
redNumLeft +=1;
}else{
Toast.makeText(MainActivity.this, "you choose enough",Toast.LENGTH_SHORT).show();
}
}
}
});
blueBallGrid.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(blueNumLeft > 1){
if(blueBallSelectArr.get(arg2) == 0){
arg1.setBackgroundResource(blueBallChangeIds[arg2]);
blueBallSelectArr.set(arg2,arg2+1);
blueNumLeft --;
}
else{
arg1.setBackgroundResource(blueBallIds[arg2]);
blueBallSelectArr.set(arg2, 0);
blueNumLeft += 1;
}
}
else{
if(blueBallSelectArr.get(arg2) != 0){
arg1.setBackgroundResource(blueBallIds[arg2]);
blueBallSelectArr.set(arg2, 0);
blueNumLeft +=1;
}else{
Toast.makeText(MainActivity.this, "you choose enough",Toast.LENGTH_SHORT).show();
}
}
}
});
okButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
showRedGridView.setAdapter(new ShowBallArrayAdapter(MainActivity.this, MainActivity.this, redBallSelectArr));
showBlueGridView.setAdapter(new ShowBallArrayAdapter(MainActivity.this, MainActivity.this, blueBallSelectArr));
show_ball_dialog.dismiss();
}
});
resetButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
redBallSelectArr.clear();
blueBallSelectArr.clear();
initSelectedBallArrayList();
redNumLeft = 36; blueNumLeft = 16;
redBallGrid.setAdapter(new ImageAdapter(MainActivity.this,MainActivity.this,redBallSelectArr));
blueBallGrid.setAdapter(new ImageAdapter(MainActivity.this,MainActivity.this,blueBallSelectArr));
}
});
}
});
}
}
二、ImageAdapter.java类中的代码:
package com.cn.daming;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter
{
MainActivity activity;
private Context mContext;
ImageView imageView;
private Integer[] mImageIds =
{
R.drawable.redball_01,
R.drawable.redball_02,
R.drawable.redball_03,
R.drawable.redball_04,
R.drawable.redball_05,
R.drawable.redball_06,
R.drawable.redball_07,
R.drawable.redball_08,
R.drawable.redball_09,
R.drawable.redball_10,
R.drawable.redball_11,
R.drawable.redball_12,
R.drawable.redball_13,
R.drawable.redball_14,
R.drawable.redball_15,
R.drawable.redball_16,
R.drawable.redball_17,
R.drawable.redball_18,
R.drawable.redball_19,
R.drawable.redball_20,
R.drawable.redball_21,
R.drawable.redball_22,
R.drawable.redball_23,
R.drawable.redball_24,
R.drawable.redball_25,
R.drawable.redball_26,
R.drawable.redball_27,
R.drawable.redball_28,
R.drawable.redball_29,
R.drawable.redball_30,
R.drawable.redball_31,
R.drawable.redball_32,
R.drawable.redball_33,
R.drawable.redball_34,
R.drawable.redball_35,
};
private Integer[] changeimageid = {
R.drawable.redball_dark01,
R.drawable.redball_dark02,
R.drawable.redball_dark03,
R.drawable.redball_dark04,
R.drawable.redball_dark05,
R.drawable.redball_dark06,
R.drawable.redball_dark07,
R.drawable.redball_dark08,
R.drawable.redball_dark09,
R.drawable.redball_dark10,
R.drawable.redball_dark11,
R.drawable.redball_dark12,
R.drawable.redball_dark13,
R.drawable.redball_dark14,
R.drawable.redball_dark15,
R.drawable.redball_dark16,
R.drawable.redball_dark17,
R.drawable.redball_dark18,
R.drawable.redball_dark19,
R.drawable.redball_dark20,
R.drawable.redball_dark21,
R.drawable.redball_dark22,
R.drawable.redball_dark23,
R.drawable.redball_dark24,
R.drawable.redball_dark25,
R.drawable.redball_dark26,
R.drawable.redball_dark27,
R.drawable.redball_dark28,
R.drawable.redball_dark29,
R.drawable.redball_dark30,
R.drawable.redball_dark31,
R.drawable.redball_dark32,
R.drawable.redball_dark33,
R.drawable.redball_dark34,
R.drawable.redball_dark35,
};
private Integer[] blueBallIds =
{
R.drawable.blueball_01,
R.drawable.blueball_02,
R.drawable.blueball_03,
R.drawable.blueball_04,
R.drawable.blueball_05,
R.drawable.blueball_06,
R.drawable.blueball_07,
R.drawable.blueball_08,
R.drawable.blueball_09,
R.drawable.blueball_10,
R.drawable.blueball_11,
R.drawable.blueball_12,
R.drawable.blueball_13,
R.drawable.blueball_14,
R.drawable.blueball_15,
R.drawable.blueball_16,
};
private Integer[] blueBallChangeIds =
{
R.drawable.blueball_dark01,
R.drawable.blueball_dark02,
R.drawable.blueball_dark03,
R.drawable.blueball_dark04,
R.drawable.blueball_dark05,
R.drawable.blueball_dark06,
R.drawable.blueball_dark07,
R.drawable.blueball_dark08,
R.drawable.blueball_dark09,
R.drawable.blueball_dark10,
R.drawable.blueball_dark11,
R.drawable.blueball_dark12,
R.drawable.blueball_dark13,
R.drawable.blueball_dark14,
R.drawable.blueball_dark15,
R.drawable.blueball_dark16,
};
private ArrayList<Integer> filter = new ArrayList<Integer>();
public ImageAdapter(Context c,MainActivity a ,ArrayList<Integer> ballAL )
{
activity = a;
mContext = c;
filter = ballAL;
}
public int getCount()
{
if (filter.size() == 35){
return 35;
}
else{
return 16;
}
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(24, 24));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
else
{
imageView = (ImageView) convertView;
}
if (filter.get(position) == 0 && filter.size()== 35){
imageView.setBackgroundResource(mImageIds[position]);
}
else if (filter.get(position) != 0 && filter.size()== 35){
imageView.setBackgroundResource(changeimageid[position]);
}
else if (filter.get(position) == 0 && filter.size() == 16){
imageView.setBackgroundResource(blueBallIds[position]);
}
else if (filter.get(position) != 0 && filter.size() == 16){
imageView.setBackgroundResource(blueBallChangeIds[position]);
}
return imageView;
}
}
三、ShowBallArrayAdapter.java类中的代码:
package com.cn.daming;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ShowBallArrayAdapter extends BaseAdapter {
MainActivity activity;
private ArrayList<Integer> filterresult;
private Context mContext;
ImageView imageView;
private int[] imageI;
private Integer[] mImageIds = { R.drawable.redball_01,
R.drawable.redball_02, R.drawable.redball_03,
R.drawable.redball_04, R.drawable.redball_05,
R.drawable.redball_06, R.drawable.redball_07,
R.drawable.redball_08, R.drawable.redball_09,
R.drawable.redball_10, R.drawable.redball_11,
R.drawable.redball_12, R.drawable.redball_13,
R.drawable.redball_14, R.drawable.redball_15,
R.drawable.redball_16, R.drawable.redball_17,
R.drawable.redball_18, R.drawable.redball_19,
R.drawable.redball_20, R.drawable.redball_21,
R.drawable.redball_22, R.drawable.redball_23,
R.drawable.redball_24, R.drawable.redball_25,
R.drawable.redball_26, R.drawable.redball_27,
R.drawable.redball_28, R.drawable.redball_29,
R.drawable.redball_30, R.drawable.redball_31,
R.drawable.redball_32, R.drawable.redball_33,
R.drawable.redball_34,R.drawable.redball_35,};
private Integer[] blueImageIds = { R.drawable.blueball_01,
R.drawable.blueball_02, R.drawable.blueball_03,
R.drawable.blueball_04, R.drawable.blueball_05,
R.drawable.blueball_06, R.drawable.blueball_07,
R.drawable.blueball_08, R.drawable.blueball_09,
R.drawable.blueball_10, R.drawable.blueball_11,
R.drawable.blueball_12, R.drawable.blueball_13,
R.drawable.blueball_14, R.drawable.blueball_15,
R.drawable.blueball_16, };
StringBuffer sb = new StringBuffer();
String[] temStr;
Integer j = 0;
String tempS;
public ShowBallArrayAdapter(Context c, MainActivity at, ArrayList<Integer> al) {
mContext = c;
activity = at;
filterresult = al;
for (int i = 0; i < filterresult.size(); i++) {
if (filterresult.get(i) != 0) {
sb.append(filterresult.get(i) + ",");
}
}
;
String s = sb.toString();
if (s.length() != 0) {
tempS = s.substring(0, s.length() - 1);
temStr = tempS.split(",");
imageI = new int[temStr.length];
for (int i = 0; i < temStr.length; i++) {
j = Integer.parseInt(temStr[i]);
imageI[i] = j - 1;
}
for(int k=0;k<temStr.length;k++){
if (filterresult.size() == 35){
imageI[k] = mImageIds[imageI[k]];
}
else if (filterresult.size() == 16){
imageI[k] = blueImageIds[imageI[k]];
}
}
} else {
tempS = s.substring(0, s.length());
}
}
public int getCount() {
if (temStr == null) {
return 0;
} else {
return temStr.length;
}
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setClickable(true);
imageView.setLayoutParams(new GridView.LayoutParams(24, 24));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(imageI[position]);
return imageView;
}
}
Layout布局中的文件:
一、main.xml布局文件中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<View
android:layout_marginLeft="1dip"
android:layout_width="2dip"
android:layout_height="fill_parent"
android:background="#FF909090" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="@string/hello"
android:textSize="10pt"
android:textColor="#FF000000"
/>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF909090" />
<Button
android:id="@+id/dialog_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击弹出Dialog对话框"
android:textSize="10pt"
/>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="红球选中球数"
android:textSize="10pt"
android:textColor="#FF000000"
/>
<GridView
android:id="@+id/show_red_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="6dip"
android:verticalSpacing="16dip"
android:numColumns="8"
android:gravity="center"
/>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF909090" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="蓝球选中球数"
android:textSize="10pt"
android:textColor="#FF000000"
/>
<GridView
android:id="@+id/show_blue_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="6dip"
android:verticalSpacing="16dip"
android:numColumns="8"
android:gravity="center"
/>
<View
android:layout_marginBottom="2dip"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF909090" />
</LinearLayout>
<View
android:layout_marginRight="2dip"
android:layout_width="2dip"
android:layout_height="fill_parent"
android:background="#FF909090" />
</LinearLayout>
二、ball_grid_dialog.xml布局文件中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/gridviewtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/ball_textview"
android:textSize="20dip"
android:background="#00000000"
/>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF909090" />
<GridView
android:id="@+id/red_ball_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dip"
android:verticalSpacing="10dip"
android:numColumns="7"
android:gravity="center" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF909090" />
<GridView
android:id="@+id/blue_ball_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dip"
android:verticalSpacing="10dip"
android:numColumns="7"
android:gravity="center" />
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF909090" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/Ok_Button"
android:text="@string/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
<Button
android:id="@+id/Reset_Button"
android:text="@string/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/Ok_Button">
</Button>
</RelativeLayout>
</LinearLayout>
values目录下的string.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">大明动态改变GridView的值!</string>
<string name="app_name">大明动态GridViewDemo</string>
<string name="ball_textview">大明选中球界面</string>
<string name="ok_button">确定</string>
<string name="reset_button">重置</string>
</resources>
AndroidManifest.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cn.daming"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>