具体代码如下:
main.xml
@H_502_10@复制代码 代码如下:
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
xmlns:tools="http://schemas.androID.com/tools"
androID:background="#000000"
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
tools:context=".MainActivity" >
<GrIDVIEw
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
androID:ID="@+ID/grID"
androID:verticalSpacing="3dp"
androID:horizontalSpacing="3dp"
androID:numColumns="3"
></GrIDVIEw>
</linearLayout>
item.xml
@H_502_10@复制代码 代码如下:
<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:layout_wIDth="match_parent"
androID:layout_height="match_parent"
androID:orIEntation="vertical" >
<ImageVIEw
androID:scaleType="fitXY"
androID:padding="3dp"
androID:layout_height="70dp"
androID:layout_wIDth="70dp"
androID:ID="@+ID/image_item"
/>
</linearLayout>
mainActivity.java
@H_502_10@复制代码 代码如下:
package com.imagevIEw;
import androID.os.Bundle;
import androID.app.Activity;
import androID.vIEw.VIEw;
import androID.Widget.AdapterVIEw;
import androID.Widget.GrIDVIEw;
import androID.Widget.AdapterVIEw.OnItemClickListener;
public class MainActivity extends Activity {
private Adpter adpter;
private GrIDVIEw grIDVIEw;
@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
int [] image={R.drawable.dog1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m5,R.drawable.dog2};
adpter=new Adpter(image,this);
grIDVIEw=(GrIDVIEw) findVIEwByID(R.ID.grID);
grIDVIEw.setAdapter(adpter);
grIDVIEw.setonItemClickListener(new OnItemClickListener() {
@OverrIDe
public voID onItemClick(AdapterVIEw<?> arg0,VIEw arg1,
int @R_301_4612@,long arg3) {
adpter.chiceState(@R_301_4612@);
}
});
}
}
Adpter.java
@H_502_10@复制代码 代码如下:
package com.imagevIEw;
import androID.content.Context;
import androID.graphics.Bitmap;
import androID.graphics.BitmapFactory;
import androID.graphics.drawable.BitmapDrawable;
import androID.graphics.drawable.Drawable;
import androID.graphics.drawable.LayerDrawable;
import androID.util.Log;
import androID.vIEw.LayoutInflater;
import androID.vIEw.VIEw;
import androID.vIEw.VIEwGroup;
import androID.Widget.BaseAdapter;
import androID.Widget.ImageVIEw;
public class Adpter extends BaseAdapter {
private int[] image;
private boolean isChice[];
private Context context;
public Adpter(int[] im,Context context) {
this.image = im;
Log.i("hck",im.length+"lenght");
isChice=new boolean[im.length];
for (int i = 0; i < im.length; i++) {
isChice[i]=false;
}
this.context = context;
}
@OverrIDe
public int getCount() {
return image.length;
}
@OverrIDe
public Object getItem(int arg0) {
return image[arg0];
}
@OverrIDe
public long getItemID(int arg0) {
return arg0;
}
@OverrIDe
public VIEw getVIEw(int arg0,VIEwGroup arg2) {
VIEw vIEw = arg1;
GetVIEw getVIEw=null;
if (vIEw == null) {
vIEw = LayoutInflater.from(context).inflate(R.layout.item,null);
getVIEw = new GetVIEw();
getVIEw.imageVIEw=(ImageVIEw) vIEw.findVIEwByID(R.ID.image_item);
vIEw.setTag(getVIEw);
} else {
getVIEw = (GetVIEw) vIEw.getTag();
}
getVIEw.imageVIEw.setimageDrawable(getVIEw(arg0));
return vIEw;
}
static class GetVIEw {
ImageVIEw imageVIEw;
}
//主要就是下面的代码了
private LayerDrawable getVIEw(int post) {
Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(image[post])).getBitmap();
Bitmap bitmap2=null;
LayerDrawable la=null;
if (isChice[post]== true){
bitmap2 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.editable_mode_checked_tag);
}
if (bitmap2!=null) {
Drawable[] array = new Drawable[2];
array[0] = new BitmapDrawable(bitmap);
array[1] = new BitmapDrawable(bitmap2);
la= new LayerDrawable(array);
la.setLayerInset(0,0); //第几张图离各边的间距
la.setLayerInset(1,65,0);
}
else {
Drawable[] array = new Drawable[1];
array[0] = new BitmapDrawable(bitmap);
la= new LayerDrawable(array);
la.setLayerInset(0,0);
}
return la; // 返回叠加后的图
}
public voID chiceState(int post)
{
isChice[post]=isChice[post]==true?false:true;
this.notifyDataSetChanged();
}
}
以上是内存溢出为你收集整理的android GridView多选效果的实例代码全部内容,希望文章能够帮你解决android GridView多选效果的实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)