<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x

    Android 開發 listview QQ多級列表的實現

    發布時間:2013-9-24 17:36    發布者:reggae
    關鍵詞: android
    [代碼] 主類

    1. package com.android.qu.antking.list;

    2. import android.app.Activity;
    3. import android.content.Context;
    4. import android.os.Bundle;
    5. import android.view.LayoutInflater;
    6. import android.view.View;
    7. import android.view.ViewGroup;
    8. import android.widget.BaseExpandableListAdapter;
    9. import android.widget.ExpandableListView;
    10. import android.widget.ImageView;
    11. import android.widget.TextView;

    12. import java.util.*;



    13. public class MyMain extends Activity {
    14.   //author antkingwei
    15. private List> parentList=new ArrayList>();

    16. private List>> childList = new ArrayList>>();

    17. ExpendAdapter adapter;

    18. ExpandableListView exList;

    19. private String[] listName = new String[]{
    20. "我的好友","高中同學","大學同學","移動開發","網站建設","普通朋友"
    21. };
    22. private String[] childTitle= new String[]{
    23. "丫寧","王八銳","小鳥","連超","董二丫"
    24. };
    25. private String[] childMood= new String[]{
    26. "我喜歡王銳","我就是王八","我也喜歡王銳","上邊一群傻帽","同樓上"
    27. };
    28. private int[] headImage=new int[]{
    29. R.drawable.ning,R.drawable.rui,R.drawable.niao,R.drawable.lianchao,R.drawable.xiaoxiao
    30. };

    31.     public void onCreate(Bundle savedInstanceState) {
    32.         super.onCreate(savedInstanceState);
    33.         setContentView(R.layout.main);

    34.         exList = (ExpandableListView) this.findViewById(R.id.expandableListView1);
    35.         parentList =getParentList();
    36.         childList = getChildList();
    37.        adapter = new ExpendAdapter(MyMain.this, parentList, childList);
    38.       
    39.         exList.setAdapter(adapter);
    40.         exList.setGroupIndicator(null);
    41.         exList.setDivider(null);
    42.         
    43.     }
    44.     public List> getParentList(){
    45.     List> list = new ArrayList>();
    46.     for(int i=0;i
    47.     Map curGroupMap = new HashMap();
    48.              list.add(curGroupMap);
    49.              curGroupMap.put("List", listName[i]);
    50.     }
    51.     return list;
    52.     }
    53.     public List>> getChildList(){
    54.     List>> list1 = new ArrayList>>();
    55.     for (int i = 0; i < listName.length; i++) {
    56.             
    57.                
    58.              List> children = new ArrayList>();
    59.              for (int j = 0; j
    60.                  Map curChildMap = new HashMap();
    61.                  children.add(curChildMap);
    62.                  curChildMap.put("Title", childTitle[j]);
    63.                  curChildMap.put("Mood", childMood[j]);
    64.                  curChildMap.put("Head", headImage[j]);
    65.              }
    66.              list1.add(children);
    67.     }
    68.     return list1;
    69.    
    70.     }
    71.    
    72. }
    復制代碼

    [代碼] 自定義的Adapter

    1. package com.android.qu.antking.list;

    2. import android.content.Context;
    3. import android.view.LayoutInflater;
    4. import android.view.View;
    5. import android.view.ViewGroup;
    6. import android.widget.BaseExpandableListAdapter;
    7. import android.widget.ImageView;
    8. import android.widget.TextView;
    9. import android.widget.Toast;

    10. import java.util.*;

    11. public class ExpendAdapter extends BaseExpandableListAdapter {
    12.       private LayoutInflater layoutInflater;
    13.       
    14.       private Context mContext;
    15.       
    16.       private List> parentList = new ArrayList>();
    17.       
    18.       private List>> childList = new ArrayList>>();
    19.       
    20.       
    21. public ExpendAdapter(Context mContext,List> parentList,List>> childList){


    22. this.mContext = mContext;

    23. this.parentList = parentList;

    24. this.childList = childList;

    25. layoutInflater = LayoutInflater.from(mContext);
    26. }
    27. public Object getChild(int groupPosition, int childPosition) {
    28. // TODO Auto-generated method stub
    29. return childList.get(groupPosition).get(childPosition).get("Title").toString();
    30. }

    31. @Override
    32. public long getChildId(int groupPosition, int childPosition) {
    33. return childPosition;
    34. }

    35. @Override
    36. public View getChildView(int groupPosition, int childPosition,
    37. boolean isLastChild, View convertView, ViewGroup parent) {

    38. if(convertView ==null){
    39. convertView = layoutInflater.inflate(R.layout.childlist, null);

    40. }
    41. final ImageView head=(ImageView)convertView.findViewById(R.id.headImage);
    42.   head.setImageResource(Integer.valueOf(childList.get(groupPosition).get(childPosition).get("Head").toString()));
    43. final TextView title=(TextView)convertView.findViewById(R.id.title);
    44. title.setText(childList.get(groupPosition).get(childPosition).get("Title").toString());

    45.    final TextView mood =(TextView)convertView.findViewById(R.id.mood);
    46.      mood.setText(childList.get(groupPosition).get(childPosition).get("Mood").toString());
    47. return convertView;
    48. }

    49. @Override
    50. public int getChildrenCount(int groupPosition) {
    51. // TODO Auto-generated method stub
    52. return childList.get(groupPosition).size();
    53. }

    54. @Override
    55. public Object getGroup(int groupPosition) {
    56. // TODO Auto-generated method stub
    57. return parentList.get(groupPosition).get("List").toString();
    58. }

    59. @Override
    60. public int getGroupCount() {
    61. // TODO Auto-generated method stub
    62. return parentList.size();
    63. }

    64. @Override
    65. public long getGroupId(int groupPosition) {
    66. // TODO Auto-generated method stub
    67. return groupPosition;
    68. }

    69. @Override
    70. public View getGroupView(int groupPosition, boolean isExpanded,
    71. View convertView, ViewGroup parent) {

    72. if(convertView==null){
    73. convertView=layoutInflater.inflate(R.layout.parentlist, null);

    74. }
    75. final TextView list = (TextView) convertView.findViewById(R.id.list);
    76. list.setText(parentList.get(groupPosition).get("List").toString());
    77. return convertView;
    78. }

    79. @Override
    80. public boolean hasStableIds() {
    81. // TODO Auto-generated method stub
    82. Toast.makeText(mContext,"nihao",Toast.LENGTH_SHORT).show();
    83. return true;
    84. }

    85. @Override
    86. public boolean isChildSelectable(int groupPosition, int childPosition) {
    87. Toast.makeText(mContext, "這是第"+groupPosition+"組,第"+childPosition+"個", Toast.LENGTH_SHORT).show();
    88. return true;
    89. }

    90. }
    復制代碼

    [代碼] 主布局文件


    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.     android:orientation="vertical"
    3.     android:layout_width="fill_parent"
    4.     android:layout_height="fill_parent"
    5.     android:background="@drawable/back1"
    6.     >


    復制代碼

    [代碼] parentList布局


    1.   xmlns:android="http://schemas.android.com/apk/res/android"
    2.   android:layout_width="fill_parent"
    3.   android:orientation="horizontal"
    4.   android:id="@+id/parentList"
    5.   android:layout_height="wrap_content">
    6.   
    7.     android:layout_width="60px"
    8.     android:layout_height="60px"
    9.     android:src="@drawable/user_group"
    10.    />
    11.    
    12.     android:id="@+id/list"
    13.     android:textSize="20px"
    14.     android:layout_width="wrap_content"
    15.     android:layout_height="wrap_content"/>
    16.   
    復制代碼

    [代碼] childList布局


    1.   xmlns:android="http://schemas.android.com/apk/res/android"
    2.   android:layout_width="fill_parent"
    3.   android:layout_height="wrap_content"
    4.   android:id="@+id/childList"
    5.   android:orientation="horizontal"
    6.   >
    7.   
    8.     android:paddingLeft="20px"
    9.     android:id="@+id/headImage"
    10.     android:src="@drawable/icon"
    11.     android:layout_width="50px"
    12.     android:layout_height="50px"
    13.     android:layout_marginBottom="5px"
    14.     android:layout_marginRight="10px"/>
    15.   
    16.    android:orientation="vertical"
    17.    android:layout_width="fill_parent"
    18.    
    19.    android:layout_height="wrap_content">
    20.    
    21.    android:textSize="18px"
    22.    android:layout_width="wrap_content"
    23.    android:layout_height="wrap_content"
    24.    />
    25.    
    26.    android:textSize="16px"
    27.    android:layout_width="wrap_content"
    28.    android:layout_height="wrap_content"/>
    29.    
    30.    
    31.    
    復制代碼

    希望本文對廣大安卓開發者有所幫助,感謝閱讀本文。更多安卓技術問題歡迎加群探討:314230976,驗證碼:eec,不寫驗證不予通過喲~

    本文地址:http://www.portaltwn.com/thread-121310-1-1.html     【打印本頁】

    本站部分文章為轉載或網友發布,目的在于傳遞和分享信息,并不代表本網贊同其觀點和對其真實性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問題,我們將根據著作權人的要求,第一時間更正或刪除。
    您需要登錄后才可以發表評論 登錄 | 立即注冊

    廠商推薦

    • Microchip視頻專區
    • EtherCAT®和Microchip LAN925x從站控制器介紹培訓教程
    • MPLAB®模擬設計器——在線電源解決方案,加速設計
    • 讓您的模擬設計靈感,化為觸手可及的現實
    • 深度體驗Microchip自動輔助駕駛應用方案——2025巡展開啟報名!
    • 貿澤電子(Mouser)專區

    相關視頻

    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷