部落格:http://blog.csdn.net/muyang_ren
一、資源檔案
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- group-->
<string-array name="group_values">
<item>hello1</item>
<item>hello2</item>
<item>hello3</item>
<item>hello4</item>
</string-array>
<!-- childs-->
<string-array name="child_values1">
<item>aaaaaaa</item>
</string-array>
<string-array name="child_values2">
<item>bbbbbbb</item>
</string-array>
<string-array name="child_values3">
<item>ccccccc</item>
</string-array>
<string-array name="child_values4">
<item>ddddddd</item>
</string-array>
</resources>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="black">#000000</color>
<color name="white">#ffffff</color>
<color name="transparent">#00000000</color>
<!--header-->
<color name="MyAPP_header_color">#7A9DEB</color>
<color name="back_white_color">#FDFCFD</color>
<!--listview 分隔條顔色-->
<color name="childdivider_color">#D3DEEB</color>
<!--待選擇的-->
<color name="line_color">#6bc78b</color>
<color name="title_bg">#008B45</color>
<color name="setting_item_bg">#dedfe0</color>
<color name="back">#efeff4</color>
<color name="head_text_color">#008B45</color>
<color name="header_color">#ffffff</color>
<color name="btn_pressed_color">#008B00</color>
<color name="item_text_color">#2b2b2b</color>
<color name="item_line_color">#c8c7cc</color>
</resources>
dimens.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<!--header-->
<dimen name="header_height">50dp </dimen>
<dimen name="header_imagebutton_height"> 50dp </dimen>
<dimen name="header_imagebutton_width"> 50dp </dimen>
<dimen name="title_button_width"> 50dp </dimen>
<dimen name="title_textview_width"> 100dp</dimen>
<!-- list view-->
<dimen name="parent_list_height"> 30dp </dimen>
<dimen name="group_list_left">10dp</dimen>
<dimen name="children_list_height"> 30dp </dimen>
<dimen name="children_list_left">15dp</dimen>
</resources>
二、xml布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lianghuiyong.myapplication.MainActivity">
<RelativeLayout
android:id="@+id/header_layout"
android:layout_width="fill_parent"
android:layout_height="@dimen/header_height"
android:background="@color/MyAPP_header_color">
<ImageButton
android:id="@+id/header_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/header_setting"
android:layout_marginLeft="18dp"
android:layout_centerVertical="true"
android:contentDescription="imagebutton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/header_title"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/header_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/header_out"
android:clickable="true"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_below="@+id/header_layout">
<ExpandableListView
android:id="@+id/list_main"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</RelativeLayout>
三、Activity
MainActivity.java
package com.example.lianghuiyong.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ExpandableListView;
import com.example.lianghuiyong.myapplication.myappadpter.doubleListAdapter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ExpandableListView main_list = null;
private List group = new ArrayList();
private List<List<String>> child = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_list =(ExpandableListView)findViewById(R.id.list_main);
//擷取xml的array-string數組,然後轉換成List
group = Arrays.asList(getResources().getStringArray(R.array.group_values));
child.add(Arrays.asList(getResources().getStringArray(R.array.child_values1)));
child.add(Arrays.asList(getResources().getStringArray(R.array.child_values2)));
child.add(Arrays.asList(getResources().getStringArray(R.array.child_values3)));
child.add(Arrays.asList(getResources().getStringArray(R.array.child_values4)));
Log.d("lhy", "group = " + group);
Log.d("lhy", "child = " + child);
main_list.setAdapter(new doubleListAdapter(group, child));
}
}
四、擴充卡
package com.example.lianghuiyong.myapplication.myappadpter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.lianghuiyong.myapplication.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2015/11/26.
*/
public class doubleListAdapter extends BaseExpandableListAdapter {
private List group = new ArrayList();
private List<List<String>> child = new ArrayList<>();
public doubleListAdapter(List group, List<List<String>> child) {
this.group = group;
this.child = child;
Log.d("lhy","group = " + this.group);
Log.d("lhy","child = " + this.child);
Log.d("lhy","group.size() = " + group.size());
}
@Override
public int getGroupCount() {
return group.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return child.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return (child.get(groupPosition).get(childPosition));
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
//執行個體化 groupText 并設定屬性
TextView groupText = new TextView(viewGroup.getContext());
groupText.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
groupText.setPadding(70, 0, 0, 0);
groupText.setTextSize(20);
groupText.setText(getGroup(groupPosition).toString());//添加資料
return groupText;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
//child LinearLayoutshilihua 屬性設定
LinearLayout linearLayout = new LinearLayout(viewGroup.getContext());
//child childText執行個體化并設定屬性
TextView childText = new TextView(viewGroup.getContext());
childText.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
childText.setPadding(70, 0, 0, 0);
childText.setTextSize(20);
childText.setText(getChild(groupPosition,childPosition).toString());
//傳回linearLayout執行個體
linearLayout.addView(childText);
return linearLayout;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}