1.先看效果图
2.MainActivity布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:layout_marginTop="100dp"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:numColumns="3"
android:listSelector="@android:color/transparent"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp">
</GridView>
<Button
android:layout_marginTop="100dp"
android:id="@+id/btn_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空"/>
<Button
android:id="@+id/btn_button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="全选"/>
</LinearLayout>
3.item布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/grid_view_ll"
android:layout_margin="3dp"
android:background="@drawable/gridview_background_color"
android:orientation="horizontal"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="45dp">
<ImageView
android:id="@+id/grid_view_iv"
android:layout_marginRight="2dp"
android:layout_width="15dp"
android:layout_height="15dp" />
<TextView
android:id="@+id/grid_view_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
4.创建一个GridView数据构造类:
public class GridViewInfo {
private String name;
private int icon;
private boolean checked;
public GridViewInfo ( int icon, String name, boolean checked){
this.icon = icon;
this.name = name;
this.checked = checked;
}
public String getName () {
return name;
}
public void setName ( String name ) {
this.name = name;
}
public int getIcon () {
return icon;
}
public void setIcon ( int icon ) {
this.icon = icon;
}
public boolean isChecked () {
return checked;
}
public void setChecked ( boolean checked ) {
this.checked = checked;
}
}
5.MainActivity代码:
public class MainActivity extends Activity {
private GridView mGridView;
private GridViewAdapter adapter;
private List<GridViewInfo> mData = new ArrayList<>();
private Button button,button2;
@Override
protected void onCreate ( @Nullable Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView) findViewById(R.id.grid_view);
button = (Button) findViewById(R.id.btn_button);
button2 = (Button) findViewById(R.id.btn_button2);
//初始化数据
String[] merchant = getResources().getStringArray(R.array.supplier_filtrate_text);
TypedArray typedArray2 = getResources().obtainTypedArray(R.array.supplier_filtrate_icon);
for (int i = 0; i < merchant.length; i++){
mData.add(new GridViewInfo(typedArray2.getResourceId(i,0), merchant[i], false));
}
//创建数据适配器并绑定适配器
adapter = new GridViewAdapter(this, mData);
mGridView.setAdapter(adapter);
//设置列表点击事件
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick ( AdapterView<?> parent, View view, int position, long id ) {
//得到选中状态
boolean checked = mData.get(position).isChecked();
if (!checked){
mData.get(position).setChecked(true);
Toast.makeText(MainActivity.this, "选中了第" + position + "个", Toast.LENGTH_SHORT).show();
} else {
mData.get(position).setChecked(false);
Toast.makeText(MainActivity.this, "取消选中第" + position + "个", Toast.LENGTH_SHORT).show();
}
adapter.notifyDataSetChanged();
}
});
//设置清空全部选中
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick ( View v ) {
for (int i = 0; i < mData.size(); i++){
mData.get(i).setChecked(false);
}
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "清空全部选中", Toast.LENGTH_SHORT).show();
}
});
//设置全部选中
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick ( View v ) {
for (int i = 0; i < mData.size(); i++){
mData.get(i).setChecked(true);
}
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "全部选中", Toast.LENGTH_SHORT).show();
}
});
}
}
6.适配器:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
private List<GridViewInfo> mData = new ArrayList<>();
public GridViewAdapter ( Context context, List<GridViewInfo> mData ) {
this.mContext = context;
this.mData = mData;
}
@Override
public int getCount () {
return mData.size();
}
@Override
public Object getItem ( int position ) {
return mData.get(position);
}
@Override
public long getItemId ( int position ) {
return position;
}
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null);
holder.mImageView = (ImageView) convertView.findViewById(R.id.grid_view_iv);
holder.mTextView = (TextView) convertView.findViewById(R.id.grid_view_tv);
holder.mLayout = (LinearLayout) convertView.findViewById(R.id.grid_view_ll);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.mTextView.setText(mData.get(position).getName());
holder.mImageView.setImageResource(mData.get(position).getIcon());
holder.mLayout.setSelected(mData.get(position).isChecked());
return convertView;
}
class ViewHolder {
private TextView mTextView;
private ImageView mImageView;
private LinearLayout mLayout;
}
}
7.在values文件夹array.xml中添加要填充数据:
<resources>
<array name="supplier_filtrate_text">
<item>蜂鸟专送</item>
<item>准时达</item>
<item>会员领红包</item>
<item>品牌商家</item>
<item>食安保</item>
<item>新店</item>
<item>开发票</item>
<item>接受预定</item>
</array>
<array name="supplier_filtrate_icon">
<item>@drawable/icon_selected</item>
<item>@drawable/icon_selected</item>
<item>@drawable/icon_selected</item>
<item>@drawable/icon_selected</item>
<item>@drawable/icon_selected</item>
<item>@drawable/icon_selected</item>
<item>@drawable/icon_selected</item>
<item>@drawable/icon_selected</item>
</array>
</resources>
8.在drawable文件中添加item背景选中和未选中的颜色:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/color_gray" android:state_selected="false"/>
<item android:drawable="@color/colorPrimary" android:state_selected="true"/>
</selector>
如果item背景需要设置圆角代码改为:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false">
<shape android:shape="rectangle">
<!-- 圆角 -->
<corners android:radius="2dp" />
<!--填充颜色为灰色-->
<solid android:color="@color/color_gray" />
<!-- 描边 -->
<stroke android:width="1dp" android:color="@color/color_gray" />
</shape>
</item>
<item android:state_selected="true">
<shape android:shape="rectangle">
<!--圆角-->
<corners android:radius="2dp" />
<!--填充颜色为蓝色-->
<solid android:color="@color/colorPrimary" />
<!--描边-->
<stroke android:width="1dp" android:color="@color/colorPrimary" />
</shape>
</item>
</selector>