1、Activity 类
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private List<Map<String, Object>> dataList;
private SimpleAdapter adapter;
//图标
int icno[] = {R.drawable.ic_launcher_background, R.drawable.ic_launcher_background, R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_background, R.drawable.ic_launcher_background, R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_background, R.drawable.ic_launcher_background, R.drawable.ic_launcher_background, R.drawable.ic_launcher_background};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridview);
//初始化数据
initData();
String[] from = {"img", "text"};
int[] to = {R.id.img, R.id.text};
adapter = new SimpleAdapter(this, dataList, R.layout.gridview_item, from, to);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
Log.d("posi", "==>>" + arg2 + "==>>" + arg3);
GridView gridView = (GridView) view.getParent();
for (int i = 0; i < icno.length; ++i) {
LinearLayout ll = (LinearLayout) gridView.getChildAt(i);
if (i == arg2) {
ll.setBackgroundColor(Color.GREEN);
} else {
ll.setBackgroundColor(Color.RED);
}
}
}
});
}
void initData() {
//图标下的文字
String name[] = {"时钟", "信号", "宝箱", "秒钟", "大象", "FF", "记事本", "书签", "印象", "商店", "主题", "迅雷"};
dataList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < name.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", icno[i]);
map.put("text", name[i]);
dataList.add(map);
}
}
}
2、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity">
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="80dp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform" />
</LinearLayout>
3、gridview_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">
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:text="文字"
android:textColor="#FFF" />
</LinearLayout>
4、选中更换背景的代码在activity中:
5、效果:
参考:
https://blog.csdn.net/sinat_25926481/article/details/70880047
https://blog.csdn.net/wangxueqing52/article/details/80284988
https://blog.csdn.net/xiaocao9903/article/details/51943631