标签:
思路:ListView中添加一个SimpleAdapter,SimpleAdapter中动态添加大标题及大标题下的小标题,接下来按照思路来进行。
第一步:建立ListView布局文件list.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 tools:context=".MainActivity" > 7 <ListView 8 android:id="@+id/list" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" > 11 </ListView> 12 </LinearLayout>
第二步:建立list_items_menu.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="4sp" android:paddingLeft="12sp" android:paddingRight="12sp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/ItemTitle" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="left" android:layout_marginBottom="4dp" android:layout_marginTop="7dp" android:text="标题" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/ItemImage" android:layout_width="40sp" android:layout_height="40sp"/> <TextView android:id="@+id/xiaoItemTitle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="小标题" android:textSize="15sp" /> </LinearLayout> </RelativeLayout>
第三步:建立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<HashMap<String, Object>> listItem; HashMap<String, Object> 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<HashMap<String, Object>>(); 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<a.length;i++){ map = new HashMap<String, Object>(); for(int j=0;j<Image.length;j++){ if(i==0){ map.put("ItemTitle", a[0]); }else if(i==(j+1)){ map.put("ItemImage", Image[j]);// 图像资源的ID map.put("xiaoItemTitle", a[i]); } } listItem.add(map); } } }
效果如下:
新手小小思路,请大家多多包涵!
标签:
原文地址:http://www.cnblogs.com/osy-cd/p/4272188.html