/**
*xml Item部分
*/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageview"
android:padding="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="@drawable/icon_checkbox_normal"
android:clickable="true"/>
</RelativeLayout>
/**
*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"
android:gravity="center_horizontal"
tools:context=".MainActivity" >
<GridView android:id="@+id/gridview_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4"
android:padding="10dp"
android:horizontalSpacing="4dp"
android:verticalSpacing="4dp"
android:gravity="center"/>
</RelativeLayout>
/**
*java部分
*/
public class MainActivity extends Activity {
private int icon[] = { R.drawable.icon_checkbox_normal,
R.drawable.icon_checkbox_normal, R.drawable.icon_checkbox_normal,
R.drawable.icon_checkbox_normal, R.drawable.icon_checkbox_normal,
R.drawable.icon_checkbox_normal, R.drawable.icon_checkbox_normal,
R.drawable.icon_checkbox_normal };
MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 定义Adapter,不用每次new
myAdapter = new MyAdapter();
GridView gridview_radio = (GridView) findViewById(R.id.gridview_radio);
gridview_radio.setAdapter(myAdapter);
gridview_radio.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
myAdapter.setSeclection(position);
myAdapter.notifyDataSetChanged();
}
});
}
class MyAdapter extends BaseAdapter {
private int clickTemp = -1;
// 标识选择的Item
public void setSeclection(int position) {
clickTemp = position;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return icon.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return icon[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Holder 是设计个静态类,缓存一下,避免Listview更新的时候,重新操作。
Holder holder = null;
if (convertView == null) {
holder = new Holder();
convertView = View.inflate(MainActivity.this,
R.layout.activity_main_item, null);
holder.imageView = (ImageView) convertView
.findViewById(R.id.imageview);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
if (clickTemp == position) {
holder.imageView
.setImageResource(R.drawable.icon_checkbox_checked);
} else {
holder.imageView
.setImageResource(R.drawable.icon_checkbox_normal);
}
return convertView;
}
}
static class Holder {
ImageView imageView;
}
}