Android gridView item点击后变成另一张图
package com.cdc.jsonaquery;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MainActivity extends Activity {
private GridView gv;
private Menu1Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView) findViewById(R.id.main_gridView);
adapter = new Menu1Adapter(this);
//去除背景色
gv.setSelector(new ColorDrawable(Color.TRANSPARENT));
gv.setAdapter(adapter);
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
adapter.setSeclection(arg2);
adapter.notifyDataSetChanged();
}
});
}
}
Adapter
package com.cdc.jsonaquery;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class Menu1Adapter extends BaseAdapter {
private int clickTemp = -1;
private LayoutInflater layoutInflater;
public Menu1Adapter(Context context) {
layoutInflater = LayoutInflater.from(context);
}
private int icons[] = { R.drawable.yangguangzhengwu,
R.drawable.zhengwugongkai, R.drawable.jicengdangjian,
R.drawable.pufajiangtang, R.drawable.wenhuashuwu,
R.drawable.yuanchengjiaoyu, R.drawable.kuailechufang,
R.drawable.xinmeitipindao, R.drawable.weishengbaojian,
R.drawable.nongzishangqing, R.drawable.nongjizhichuang,
R.drawable.xinnongcun, R.drawable.jiatingyule,
R.drawable.wangluoxinwen, R.drawable.wodekongjian,
R.drawable.ifaboochaoshi, R.drawable.ifabooshequ,
R.drawable.ifabooems, R.drawable.wangzhidaohang,
R.drawable.dianshidaohang, R.drawable.dianshizhibo,
R.drawable.yuqingfenxi, R.drawable.youjiangdiaocha,
R.drawable.tiandiguangbo};
private int icons2[] = { R.drawable.yangguangzhengwuxuanzhong,
R.drawable.zhengwugongkaixuanzhong, R.drawable.jicengdangjianxuanzhong,
R.drawable.pufajiangtangxuanzhong, R.drawable.wenhuashuwuxuanzhong,
R.drawable.yuanchengjiaoyuxuanzhong, R.drawable.kuailechufangxuanzhong,
R.drawable.xinmeitipindaoxuanzhong, R.drawable.weishengbaojianxuanzhong,
R.drawable.nongzishangqingxuanzhong, R.drawable.nongjizhichuangxuanzhong,
R.drawable.xinnongcunxuanzhong, R.drawable.jiatingyulexuanzhong,
R.drawable.wangluoxinwenxuanzhong, R.drawable.wodekongjianxuanzhong,
R.drawable.ifaboochaoshixuanzhong, R.drawable.ifabooshequxuanzhong,
R.drawable.ifabooemsxuanzhong, R.drawable.wangzhidaohangxuanzhong,
R.drawable.dianshidaohangxuanzhong, R.drawable.dianshizhiboxuanzhong,
R.drawable.yuqingfenxixuanzhong, R.drawable.youjiangdiaochaxuanzhong,
R.drawable.tiandiguangboxuanzhong};
@Override
public int getCount() {
// TODO Auto-generated method stub
return icons.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public void setSeclection(int position) {
clickTemp = position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.gridview_item, null);
}
ImageView iv = (ImageView) convertView
.findViewById(R.id.gridview_iv_first_tubiao);
if (clickTemp == position) {
iv.setImageResource(icons2[position]);
} else {
iv.setImageResource(icons[position]);
}
return convertView;
}
}
布局activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<View
android:id="@+id/temp"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<GridView
android:id="@+id/main_gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:gravity="center"
android:layout_below="@id/temp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>
gridview_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<View
android:id="@+id/temp"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<GridView
android:id="@+id/main_gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:gravity="center"
android:layout_below="@id/temp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>