一 前言
东西很简单,就是一个 GridView 实现主页面的功能模块分类,减少xml的层级嵌套
二、使用步骤
首先创建主页xml布局文件,其中 numColumns 属性是设置列数,verticalSpacing 属性为行间距,horizontalSpacing 为列间距,
<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"
android:orientation="vertical">
<include layout="@layout/actionbar_layout" />
<GridView
android:id="@+id/gridView_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp" />
</LinearLayout>
然后回到主页,创建一个名为 gridItemArrayLis 的t动态数组,包含一个 HashMap ,其键为String,值为Object ,用以 GridView 的item的数据加载,再创建一个HashMap 用来自定义item的功能模块展示,
private ArrayList<HashMap<String, Object>> gridItemArrayList;
private HashMap<String, Object> hashMap;
接下来就是数据的添加,初始化 gridItemArrayList ,然后一次创建四个 hashMap 用以存入item的功能模块分类展示,然后依次 add 到 gridItemArrayList 当中,加载适配器展示
gridItemArrayList = new ArrayList<HashMap<String, Object>>();
hashMap = new HashMap<String, Object>();
hashMap.put("text", "串口");
Object image = R.mipmap.ic_rk;
hashMap.put("image", image);
Object textImage = R.drawable.shape_1;
hashMap.put("textImage", textImage);
Object layoutBg = R.drawable.shape_5;
hashMap.put("layoutBg", layoutBg);
gridItemArrayList.add(hashMap);
hashMap = new HashMap<String, Object>();
hashMap.put("text", "UI");
image = R.mipmap.ic_ck;
hashMap.put("image", image);
textImage = R.drawable.shape_2;
hashMap.put("textImage", textImage);
layoutBg = R.drawable.shape_6;
hashMap.put("layoutBg", layoutBg);
gridItemArrayList.add(hashMap);
hashMap = new HashMap<String, Object>();
hashMap.put("text", "实验");
image = R.mipmap.ic_yd;
hashMap.put("image", image);
textImage = R.drawable.shape_3;
hashMap.put("textImage", textImage);
layoutBg = R.drawable.shape_7;
hashMap.put("layoutBg", layoutBg);
gridItemArrayList.add(hashMap);
hashMap = new HashMap<String, Object>();
hashMap.put("text", "测试");
image = R.mipmap.ic_pd;
hashMap.put("image", image);
textImage = R.drawable.shape_4;
hashMap.put("textImage", textImage);
layoutBg = R.drawable.shape_8;
hashMap.put("layoutBg", layoutBg);
gridItemArrayList.add(hashMap);
gridAdapter = new GridViewAdapter(gridItemArrayList, MenuActivity.this);
gridView.setAdapter(gridAdapter);
最后就是在适配当中设置数据,
HashMap<String, Object> hashMap = (HashMap<String, Object>) getItem(position);
holder.item_Image.setImageResource((Integer) hashMap.get("image"));
holder.item_Text.setText((CharSequence) hashMap.get("text"));
holder.item_TextBg.setBackgroundResource((Integer) hashMap.get("textImage"));
holder.item_layoutBg.setBackgroundResource((Integer) hashMap.get("layoutBg"));
结尾贴一下完整代码
public class MenuActivity extends BaseActivity {
private GridView gridView;
private GridViewAdapter gridAdapter;
private ArrayList<HashMap<String, Object>> gridItemArrayList;
private TextView actionTv;
private HashMap<String, Object> hashMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
initView();
setTitleText("摸索");
}
protected void initView() {
gridView = findViewById(R.id.gridView_menu);
actionTv = findViewById(R.id.action_tv);
gridItemArrayList = new ArrayList<HashMap<String, Object>>();
hashMap = new HashMap<String, Object>();
hashMap.put("text", "串口");
Object image = R.mipmap.ic_rk;
hashMap.put("image", image);
Object textImage = R.drawable.shape_1;
hashMap.put("textImage", textImage);
Object layoutBg = R.drawable.shape_5;
hashMap.put("layoutBg", layoutBg);
gridItemArrayList.add(hashMap);
hashMap = new HashMap<String, Object>();
hashMap.put("text", "UI");
image = R.mipmap.ic_ck;
hashMap.put("image", image);
textImage = R.drawable.shape_2;
hashMap.put("textImage", textImage);
layoutBg = R.drawable.shape_6;
hashMap.put("layoutBg", layoutBg);
gridItemArrayList.add(hashMap);
hashMap = new HashMap<String, Object>();
hashMap.put("text", "实验");
image = R.mipmap.ic_yd;
hashMap.put("image", image);
textImage = R.drawable.shape_3;
hashMap.put("textImage", textImage);
layoutBg = R.drawable.shape_7;
hashMap.put("layoutBg", layoutBg);
gridItemArrayList.add(hashMap);
hashMap = new HashMap<String, Object>();
hashMap.put("text", "测试");
image = R.mipmap.ic_pd;
hashMap.put("image", image);
textImage = R.drawable.shape_4;
hashMap.put("textImage", textImage);
layoutBg = R.drawable.shape_8;
hashMap.put("layoutBg", layoutBg);
gridItemArrayList.add(hashMap);
gridAdapter = new GridViewAdapter(gridItemArrayList, MenuActivity.this);
gridView.setAdapter(gridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
gridAdapter.setSelection(position);
gridAdapter.notifyDataSetChanged();
}
});
}
class GridViewAdapter extends BaseAdapter {
private ArrayList<HashMap<String, Object>> itemArrayList;
private Context context;
private int selectItem = -1;
public GridViewAdapter(ArrayList<HashMap<String, Object>> itemArrayList, Context context) {
this.itemArrayList = itemArrayList;
this.context = context;
}
public void setSelection(int position) {
selectItem = position;
}
@Override
public int getCount() {
if (null == itemArrayList) {
return 0;
} else {
return itemArrayList.size();
}
}
@Override
public Object getItem(int position) {
return itemArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("unchecked")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.menu_gridview_layout, parent, false);
holder = new ViewHolder();
holder.item_Image = convertView.findViewById(R.id.imageView);
holder.item_Text = convertView.findViewById(R.id.textView);
holder.item_TextBg = convertView.findViewById(R.id.text_bg);
holder.item_layoutBg = convertView.findViewById(R.id.layout_bg);
convertView.setTag(holder);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = holder.item_Text.getText().toString();
if (title.contains("串口")) {
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
} else if (title.contains("UI")) {
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
} else if (title.contains("实验")) {
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
} else if (title.contains("测试")) {
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
}
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, Object> hashMap = (HashMap<String, Object>) getItem(position);
holder.item_Image.setImageResource((Integer) hashMap.get("image"));
holder.item_Text.setText((CharSequence) hashMap.get("text"));
holder.item_TextBg.setBackgroundResource((Integer) hashMap.get("textImage"));
holder.item_layoutBg.setBackgroundResource((Integer) hashMap.get("layoutBg"));
return convertView;
}
private final class ViewHolder {
private ImageView item_Image;
private TextView item_Text;
private TextView item_TextBg;
private LinearLayout item_layoutBg;
}
}
}