ExpandableListView example
android.widget.ExpandableListView display two-level ListView: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.Example:
ExpandableListView example |
Create our custom MyBaseExpandableListAdapter extends BaseExpandableListAdapter, MyBaseExpandableListAdapter.java
package com.example.androidexpandablelistview;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listGroup;
private HashMap<String, List<String>> listChild;
public MyBaseExpandableListAdapter(Context c, List<String> lg,
HashMap<String, List<String>> lc) {
context = c;
listGroup = lg;
listChild = lc;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return listChild.get(listGroup.get(groupPosition)).get(childPosition);
}
@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) {
LayoutInflater infalInflater =
(LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_layout, null);
}
TextView textViewItem =
(TextView)convertView.findViewById(R.id.item);
String text = (String)getChild(groupPosition, childPosition);
textViewItem.setText(text);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return listChild.get(listGroup.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return listGroup.get(groupPosition);
}
@Override
public int getGroupCount() {
return listGroup.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition,
boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
String textGroup = (String)getGroup(groupPosition);
TextView textViewGroup = (TextView)convertView.findViewById(R.id.group);
textViewGroup.setText(textGroup);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
/res/layout/group_layout.xml, layout of view of groups.
<?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="wrap_content"
android:background="#000000"
android:orientation="vertical"
android:padding="8dp" >
<TextView
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
/res/layout/item_layout.xml, the layout of the view of children.
<?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="55dip"
android:orientation="vertical" >
<TextView
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="italic" />
</LinearLayout>
/res/layout/activity_main.xml, main layout of our activity, with <ExpandableListView>.
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com"
android:textStyle="bold" />
<ExpandableListView
android:id="@+id/myexpandablelistview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
package com.example.androidexpandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ExpandableListView;
public class MainActivity extends Activity {
MyBaseExpandableListAdapter myBaseExpandableListAdapter;
ExpandableListView myExpandableListView;
List<String> myListForGroup;
HashMap<String, List<String>> myMapForChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myExpandableListView = (ExpandableListView)
findViewById(R.id.myexpandablelistview);
initData();
myBaseExpandableListAdapter = new
MyBaseExpandableListAdapter(this, myListForGroup, myMapForChild);
myExpandableListView.setAdapter(myBaseExpandableListAdapter);
}
private void initData() {
myListForGroup = new ArrayList<String>();
myMapForChild = new HashMap<String, List<String>>();
List<String> listGroupA = new ArrayList<String>();
listGroupA.add("A - 1");
listGroupA.add("A - 2");
listGroupA.add("A - 3");
List<String> listGroupB = new ArrayList<String>();
listGroupB.add("B - 1");
List<String> listGroupC = new ArrayList<String>();
listGroupC.add("C - 1");
listGroupC.add("C - 2");
myListForGroup.add("Group A");
myListForGroup.add("Group B");
myListForGroup.add("Group C");
myMapForChild.put(myListForGroup.get(0), listGroupA);
myMapForChild.put(myListForGroup.get(1), listGroupB);
myMapForChild.put(myListForGroup.get(2), listGroupC);
}
}
Next: Detect user action on ExpandableListView, with various Listeners
0 komentar:
Posting Komentar