Android中GridView中点击Item为选中状态其他为指定默认颜色
一、以下是gridview的适配以及item的内容
private GridView gridview_label;gridview_label = (GridView) findViewById(R.id.gridview_label);gridview_label.setAdapter(new UserLabelAdapter(UserLabelActivity.this, beanDemo)); //标签
gridview_label.setOnItemClickListener(new Gallery.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {gridview_label.getAnimation();//gridview中item添加动画 for (int i = 0; i < parent.getCount(); i++) {View v = parent.getChildAt(i); if (position == i) {//当前选中的Item改变背景颜色// arg1.setBackground(getResources().getDrawable(R.drawable.btn_label_selector)); arg1.setSelected(true); } else {// v.setBackgroundResource(R.drawable.btn_label_selector); v.setSelected(false); } } } });
二.GridView的适配器public class UserLabelAdapter extends BaseAdapter implements View.OnClickListener { private Context context; private BeanDemo beanDemo; private BitmapUtils bitmapUtils; public UserLabelAdapter(Context context, BeanDemo beanDemo) { super(); this.context = context; this.beanDemo = beanDemo; bitmapUtils = new BitmapUtils(context); } @Override public int getCount() { if (beanDemo == null) { return 0; } else { return this.beanDemo.GoodsInfo.size(); } } @Override public Object getItem(int position) { // TODO Auto-generated method stub if (beanDemo == null) { return null; } else { return this.beanDemo.GoodsInfo.get(position); } } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.activity_userlabelitem, null); //获取控件的id viewHolder.btn_label = (Button) convertView.findViewById(R.id.btn_label); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } //设置内容 viewHolder.btn_label.setText(beanDemo.GoodsInfo.get(position).title); return convertView; } @Override public void onClick(View v) { } static class ViewHolder { public Button btn_label; } } 三、适配器中item的xml文件<?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"> <Button android:id="@+id/btn_label" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="@dimen/x10" android:focusable="false" android:clickable="false" android:background="@drawable/btn_label_selector" android:gravity="center" android:text="item"> </Button> </LinearLayout>
四.最好是写一个select文件来控制选中的默认颜色和未被选中的颜色
Button的背景颜色<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/orange_round_bg" android:state_selected="true"/> <item android:drawable="@drawable/orange_border_shape"/> </selector>@drawable/orange_round_bg
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#f19a03"></solid> <size android:width="@dimen/x20" android:height="@dimen/x10"></size> <corners android:radius="3dp"></corners> </shape>@drawable/orange_border_shape
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/white" /> <stroke android:width="1dp" android:color="#ef8d61" /> <size android:width="@dimen/x30" android:height="@dimen/x20" /> <corners android:radius="5dp"></corners> </shape>