Sunday, September 14, 2014

ExpandableListView example

Example of using ExpandableListView:


Create /res/layout/group_layout.xml, to define the layout of list group:
<?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="#A0A0A0"
    android:orientation="horizontal" >
 
    <ImageView
        android:id="@+id/groupimg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <TextView
        android:id="@+id/group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textStyle="bold" />
 
</LinearLayout>

Create /res/layout/item_layout.xml to define layout of items:
<?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
<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="com.example.androidexpandablelistview.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ExpandableListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Create MyExpandableListAdapter.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 MyExpandableListAdapter extends BaseExpandableListAdapter {

 private Context context;
 private List<String> listGroup;
 private HashMap<String, List<String>> listChild;

 public MyExpandableListAdapter(Context context, List<String> listGroup,
   HashMap<String, List<String>> listChild) {
  this.context = context;
  this.listGroup = listGroup;
  this.listChild = listChild;
 }

 @Override
 public int getGroupCount() {
  return listGroup.size();
 }

 @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 Object getChild(int groupPosition, int childPosition) {
  return listChild.get(listGroup.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 false;
 }

 @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 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 boolean isChildSelectable(int groupPosition, int childPosition) {
  // TODO Auto-generated method stub
  return false;
 }

}

MainActivity.java
package com.example.androidexpandablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;

public class MainActivity extends ActionBarActivity {

 ExpandableListView expandableListView;
 MyExpandableListAdapter myExpandableListAdapter;
 List<String> groupList;
 HashMap<String, List<String>> childMap;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  init();
  expandableListView = (ExpandableListView) findViewById(R.id.mylist);
  myExpandableListAdapter = new MyExpandableListAdapter(this, groupList, childMap);
  expandableListView.setAdapter(myExpandableListAdapter);
 }

 private void init() {
  groupList = new ArrayList<String>();
  childMap = new HashMap<String, List<String>>();

  List<String> groupList0 = new ArrayList<String>();
  groupList0.add("groupList0 - 1");
  groupList0.add("groupList0 - 2");
  groupList0.add("groupList0 - 3");
  
  List<String> groupList1 = new ArrayList<String>();
  groupList1.add("groupList1 - 1");
  groupList1.add("groupList1 - 2");
  groupList1.add("groupList1 - 3");

  List<String> groupList2 = new ArrayList<String>();
  groupList2.add("groupList2 - 1");
  groupList2.add("groupList2 - 2");
  groupList2.add("groupList2 - 3");

  List<String> groupList3 = new ArrayList<String>();
  groupList3.add("groupList3 - 1");
  groupList3.add("groupList3 - 2");
  groupList3.add("groupList3 - 3");
  
  groupList.add("Group List 0");
  groupList.add("Group List 1");
  groupList.add("Group List 2");
  groupList.add("Group List 3");

  childMap.put(groupList.get(0), groupList0);
  childMap.put(groupList.get(1), groupList1);
  childMap.put(groupList.get(2), groupList2);
  childMap.put(groupList.get(3), groupList3);
 }

}

download filesDownload the files.

Next:
- Create groupIndicator for ExpandableListView example
Place groupIndicator of ExpandableListView on right side

No comments: