Android ListView中的簡單分組(標題含圖片)


思路: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);
        }

        

    }
  
    


}

 

效果如下:

 

 

新手小小思路,請大家多多包涵!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM