思路:ListView中添加一个SimpleAdapter,SimpleAdapter中动态添加大标题及大标题下的小标题,接下来按照思路来进行。
第一步:建立ListView布局文件list.xml
17 11 12
第二步:建立list_items_menu.xml布局文件
第三步:建立activity类
package com.example.test_menu;import java.util.ArrayList;import java.util.HashMap;import com.example.test_menu.R;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.view.ViewGroup.LayoutParams;import android.widget.AdapterView;import android.widget.ListView;import android.widget.RelativeLayout;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Toast;public class Test extends Activity { ArrayList> listItem; HashMap map ; SimpleAdapter listItemAdapter; ListView list ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置屏幕没有标题 this.requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题栏 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.list); // 绑定Layout里面的ListView list = (ListView) findViewById(R.id.list); // 生成动态数组,加入数据 listItem = new ArrayList >(); String[] a={"管网动态","施工日报","通知公告"}; String[] b={"运行管理","运行监控","路径模拟优化","物料移动作业","阀门开关管理"}; String[] c={"巡检管理","巡检方案","巡检记录","巡检监控"}; String[] d={"专项管理","场站管理","管线管理","管廊管理","阀门管理"}; // 小标题前面的图标 int[] Image_a={R.drawable.ic_launcher,R.drawable.lcmn,R.drawable.lct,R.drawable.ic_launcher}; map(a,Image_a); map(b,Image_a); map(c,Image_a); map(d,Image_a); listItemAdapter = new SimpleAdapter(this, listItem,// 数据源 R.layout.list_items_menu,// ListItem的XML实现 // 动态数组与ImageItem对应的子项 new String[] {"ItemTitle", "ItemImage", "xiaoItemTitle" }, // ImageItem的XML文件里面的一个ImageView,TextView ID new int[] { R.id.ItemTitle,R.id.ItemImage, R.id.xiaoItemTitle }); // 添加并且显示 list.setAdapter(listItemAdapter); // 添加菜单点击 list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { for (int i = 0; i < listItem.size(); i++) { if (arg2 == i) { Toast.makeText(getApplicationContext(), listItem.get(arg2).get("xiaoItemTitle") + " " + arg2, Toast.LENGTH_SHORT) .show(); } } } }); } public void map(String[] a,int[] Image){ for(int i=0;i (); for(int j=0;j
效果如下:
新手小小思路,请大家多多包涵!