ExpandableListView with icon

This example display icon in ExpandableListView.
ExpandableListView with icon
ExpandableListView with icon
Modify /res/layout/group_layout.xml to add a TextView to display our icon.

<?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="horizontal"
android:padding="8dp" >

<ImageView
android:id="@+id/groupimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>


<TextView
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textStyle="bold" />

</LinearLayout>

Modify getGroupView() in MyBaseExpandableListAdapter.java to set ImageView.
package com.example.androidexpandablelistview;

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

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
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);

//get application resource/drawable not in Activity class, using context
Resources contextResources = context.getResources();
Drawable groupDrawable = contextResources.getDrawable(R.drawable.ic_launcher);
//Set ImageView
ImageView groupImage = (ImageView)convertView.findViewById(R.id.groupimage);
groupImage.setImageDrawable(groupDrawable);

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;
}

}


Remark: Alternatively, you can set android:src in XML directly.

- For MainActivity.java and activity_main.xml, refer to last post "Detect user action on ExpandableListView, with various Listeners".
- For item_layout.xml, refer to the post "ExpandableListView example".

0 Response to "ExpandableListView with icon"

Posting Komentar