package com.cq.expandable.listview01;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListView01Activity extends Activity {
/** Called when the activity is first created. */
ExpandableListView expandablelistview;
public String [] tourism = {"ARRIVAL", "CITY", "SOCIABLE", "ENTERTAINMENT", "HOTEL", "MEDICAL TREATMENT"};
public String [] arrival = {"customs", "passport"};
public String [] city = {"general", "transport", "taxi"};
public String [] sociable = {"personal", "profession", "meeting", "language"};
public String [] entertainment = {"leisure", "theaters", "exhibition", "sports"};
public String [] hotel = {"general", "check-in", "staying", "check-out"};
public String [] medical = {"general", "surgeons", "dentist", "pharmacy"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expandablelistview = (ExpandableListView)findViewById(R.id.ExpandableListView01);
expandablelistview.setAdapter(new TreeViewAdapter(this));
}
public class TreeViewAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private LayoutInflater inflater1;
public TreeViewAdapter(
Context c) {
this.inflater = LayoutInflater.from(c);
this.inflater1 = LayoutInflater.from(c);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View myView = inflater1.inflate(R.layout.child, null);
final int a = groupPosition;
final int b = childPosition;
//myView.setBackgroundResource(R.drawable.monkey32);
TextView textview = (TextView) myView.findViewById(R.id.TextView001);
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ExpandableListView01Activity.this, "Group= "+ Integer.toString(a) + " , Child = " + Integer.toString(b), Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setClass(ExpandableListView01Activity.this, IntentView.class);
startActivity(intent);
}
});
if(groupPosition==0){
textview.setText(arrival[childPosition]);
}else if(groupPosition==1){
textview.setText(city[childPosition]);
}else if(groupPosition==2){
textview.setText(sociable[childPosition]);
}else if(groupPosition==3){
textview.setText(entertainment[childPosition]);
}else if(groupPosition==4){
textview.setText(hotel[childPosition]);
}else if(groupPosition==5){
textview.setText(medical[childPosition]);
}
return myView;
}
@Override
public int getChildrenCount(int groupPosition) {
if(groupPosition==0){
return arrival.length;
}else if(groupPosition==1){
return city.length;
}else if(groupPosition==2){
return sociable.length;
}else if(groupPosition==3){
return entertainment.length;
}else if(groupPosition==4){
return hotel.length;
}else {
return medical.length;
}
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return tourism.length;
}
@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) {
// TODO Auto-generated method stub
View myView = inflater.inflate(R.layout.father, null);
//myView.setBackgroundResource(R.drawable.group);
TextView textview = (TextView) myView.findViewById(R.id.TextView01);
textview.setText(tourism[groupPosition]);
textview.setTextColor(Color.YELLOW);
return myView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
}
*********************************************
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ExpandableListView android:id="@+id/ExpandableListView01"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_x="20dip"
android:layout_y="30dip"
android:groupIndicator="@null"
android:clickable="true"
android:scrollbarAlwaysDrawHorizontalTrack="true">
</ExpandableListView>
<!-- android:childDivider="@drawable/child_divider" -->
</LinearLayout>
*********************************************
Father.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16px"
android:gravity="center">
</TextView>
</LinearLayout>
********
Child.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView android:id="@+id/ImageView01"
android:layout_height="30dp"
android:layout_width="30dp"
android:background="@drawable/monkey32">
</ImageView>
<TextView android:id="@+id/TextView001"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center">
</TextView>
</LinearLayout>
******************************
IntentView.java
package com.cq.expandable.listview01;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class IntentView extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView textview = new TextView (this);
textview.setText("I LOVE TATA");
setContentView(textview);
}
}
沒有留言:
張貼留言