[代碼] 主類
- package com.android.qu.antking.list;
-
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.ImageView;
- import android.widget.TextView;
-
- import java.util.*;
-
-
-
- public class MyMain extends Activity {
- //author antkingwei
- private List
復制代碼
[代碼] 自定義的Adapter
- package com.android.qu.antking.list;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import java.util.*;
-
- public class ExpendAdapter extends BaseExpandableListAdapter {
- private LayoutInflater layoutInflater;
-
- private Context mContext;
-
- private List> parentList = new ArrayList>();
-
- private List
>> childList = new ArrayList>>();
-
-
- public ExpendAdapter(Context mContext,List> parentList,List
>> childList){
-
-
- this.mContext = mContext;
-
- this.parentList = parentList;
-
- this.childList = childList;
-
- layoutInflater = LayoutInflater.from(mContext);
- }
- public Object getChild(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return childList.get(groupPosition).get(childPosition).get("Title").toString();
- }
-
- @Override
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
-
- @Override
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
-
- if(convertView ==null){
- convertView = layoutInflater.inflate(R.layout.childlist, null);
-
- }
- final ImageView head=(ImageView)convertView.findViewById(R.id.headImage);
- head.setImageResource(Integer.valueOf(childList.get(groupPosition).get(childPosition).get("Head").toString()));
- final TextView title=(TextView)convertView.findViewById(R.id.title);
- title.setText(childList.get(groupPosition).get(childPosition).get("Title").toString());
-
- final TextView mood =(TextView)convertView.findViewById(R.id.mood);
- mood.setText(childList.get(groupPosition).get(childPosition).get("Mood").toString());
- return convertView;
- }
-
- @Override
- public int getChildrenCount(int groupPosition) {
- // TODO Auto-generated method stub
- return childList.get(groupPosition).size();
- }
-
- @Override
- public Object getGroup(int groupPosition) {
- // TODO Auto-generated method stub
- return parentList.get(groupPosition).get("List").toString();
- }
-
- @Override
- public int getGroupCount() {
- // TODO Auto-generated method stub
- return parentList.size();
- }
-
- @Override
- public long getGroupId(int groupPosition) {
- // TODO Auto-generated method stub
- return groupPosition;
- }
-
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
-
- if(convertView==null){
- convertView=layoutInflater.inflate(R.layout.parentlist, null);
-
- }
- final TextView list = (TextView) convertView.findViewById(R.id.list);
- list.setText(parentList.get(groupPosition).get("List").toString());
- return convertView;
- }
-
- @Override
- public boolean hasStableIds() {
- // TODO Auto-generated method stub
- Toast.makeText(mContext,"nihao",Toast.LENGTH_SHORT).show();
- return true;
- }
-
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- Toast.makeText(mContext, "這是第"+groupPosition+"組,第"+childPosition+"個", Toast.LENGTH_SHORT).show();
- return true;
- }
-
- }
復制代碼
[代碼] 主布局文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/back1"
- >
-
復制代碼
[代碼] parentList布局
-
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:orientation="horizontal"
- android:id="@+id/parentList"
- android:layout_height="wrap_content">
-
- android:layout_width="60px"
- android:layout_height="60px"
- android:src="@drawable/user_group"
- />
-
- android:id="@+id/list"
- android:textSize="20px"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
-
復制代碼
[代碼] childList布局
-
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/childList"
- android:orientation="horizontal"
- >
-
- android:paddingLeft="20px"
- android:id="@+id/headImage"
- android:src="@drawable/icon"
- android:layout_width="50px"
- android:layout_height="50px"
- android:layout_marginBottom="5px"
- android:layout_marginRight="10px"/>
-
- android:orientation="vertical"
- android:layout_width="fill_parent"
-
- android:layout_height="wrap_content">
-
- android:textSize="18px"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
-
- android:textSize="16px"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
-
-
-
復制代碼
希望本文對廣大安卓開發者有所幫助,感謝閱讀本文。更多安卓技術問題歡迎加群探討:314230976,驗證碼:eec,不寫驗證不予通過喲~
|