GridView单选效果 点击选中一项时,再点击选中另一项时改变背景,原来选中的项取消换回原来的背景

/**  
*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;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值