Hi mscwd01, It is works great, thanks very much. but what about extends BaseExpandableListAdapter ? Maybe i should provide a ImageProvider for the dynamic image binding.
Thanks again :) On Dec 18, 8:10 pm, mscwd01 <mscw...@gmail.com> wrote: > Hey Luke, > > Sure, I managed to solve it myself after several tedious hours ;) > It took a trip to the Android source code to get a working solution > though, here it goes. > > What I wanted to achieve was to have my own layout for each child in > an expandable list. E.g. I wanted to define two TextViews and one > ImageView and I wanted to tell Android what view to bind my data to > (e.g. String 1 to TextView 1, String 2 to TextView 2, Drawable 1 to > the ImageView). > > Here is the solution (I will include the whole src file and xml layout > as im not sure how well you understand Expandable Lists): > > Java: > ==================== > > import java.util.ArrayList; > import java.util.HashMap; > import java.util.List; > import java.util.Map; > > import android.app.ExpandableListActivity; > import android.content.Context; > import android.content.res.Resources; > import android.graphics.drawable.Drawable; > import android.os.Bundle; > import android.view.View; > import android.view.ViewGroup; > import android.widget.BaseExpandableListAdapter; > import android.widget.ExpandableListAdapter; > import android.widget.ImageView; > import android.widget.SimpleExpandableListAdapter; > import android.widget.TextView; > > /** > * Demonstrates expandable lists using a custom {...@link > ExpandableListAdapter} > * from {...@link BaseExpandableListAdapter}. > */ > public class Media extends ExpandableListActivity { > > ExpandableListAdapter mAdapter; > > private static final String NAME = "NAME"; > private static final String NUM_PHOTOS = "NUM_PHOTOS"; > private Resources res; > private Drawable photo, photo2; > private List<Drawable> albumCovers; > > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.media_layout); > > // Create a List of Drawables to insert into the Expandable > List > // This can be completely dynamic > res = this.getResources(); > photo = (Drawable) res.getDrawable(R.drawable.photo_album); > photo2 = (Drawable) res.getDrawable(R.drawable.photo_album2); > albumCovers = new ArrayList<Drawable>(); > albumCovers.add(photo); > albumCovers.add(photo2); > > // The following code simply generates the Expandable Lists > content > (Strings) > List<Map<String, String>> groupData = new > ArrayList<Map<String, > String>>(); > List<List<Map<String, String>>> childData = new > ArrayList<List<Map<String, String>>>(); > > Map<String, String> curGroupMap = new HashMap<String, > String>(); > groupData.add(curGroupMap); > curGroupMap.put(NAME, "Photos"); > curGroupMap.put(NUM_PHOTOS, ""); > > List<Map<String, String>> children = new ArrayList<Map<String, > String>>(); > Map<String, String> curChildMap = new HashMap<String, > String>(); > children.add(curChildMap); > > curChildMap.put(NAME, "Profile Pictures"); > curChildMap.put(NUM_PHOTOS, "20 Photos"); > > curChildMap = new HashMap<String, String>(); > children.add(curChildMap); > > curChildMap.put(NAME, "Vancouver 2008"); > curChildMap.put(NUM_PHOTOS, "32 Photos"); > > childData.add(children); > > curGroupMap = new HashMap<String, String>(); > groupData.add(curGroupMap); > > curGroupMap.put(NAME, "Videos"); > curGroupMap.put(NUM_PHOTOS, ""); > > children = new ArrayList<Map<String, String>>(); > > curChildMap = new HashMap<String, String>(); > children.add(curChildMap); > > curChildMap.put(NAME, "Floatplane Takeoff"); > curChildMap.put(NUM_PHOTOS, "2:26"); > > childData.add(children); > > // Set up our adapter > mAdapter = new MyExpandableListAdapter( > this, > groupData, > R.layout.media_list_parent, > new String[] { NAME, NUM_PHOTOS }, > new int[] { R.id.rowText1, R.id.rowText2, > R.id.photoAlbumImg }, > childData, > R.layout.media_list_child, > new String[] { NAME, NUM_PHOTOS }, > new int[] { R.id.rowText1, R.id.rowText2, > R.id.photoAlbumImg } > ); > setListAdapter(mAdapter); > registerForContextMenu(getExpandableListView()); > } > > /** > * A simple adapter which allows you to bind data to specific > * Views defined within the layout of an Expandable Lists children > * (Implement getGroupView() to define the layout of parents) > */ > public class MyExpandableListAdapter extends > SimpleExpandableListAdapter { > > private List<? extends List<? extends Map<String, ?>>> > mChildData; > private String[] mChildFrom; > private int[] mChildTo; > > public MyExpandableListAdapter(Context context, > List<? extends Map<String, ?>> groupData, int > groupLayout, > String[] groupFrom, int[] groupTo, > List<? extends List<? extends Map<String, > ?>>> childData, > int childLayout, String[] childFrom, int[] > childTo) { > super(context, groupData, groupLayout, groupFrom, > groupTo, > childData, childLayout, childFrom, > childTo); > > mChildData = childData; > mChildFrom = childFrom; > mChildTo = childTo; > > } > > public View getChildView(int groupPosition, int childPosition, > boolean isLastChild, View convertView, > ViewGroup parent) { > > View v; > if (convertView == null) { > v = newChildView(isLastChild, parent); > } else { > v = convertView; > } > bindView(v, > mChildData.get(groupPosition).get(childPosition), > mChildFrom, > mChildTo, groupPosition, > childPosition); > return v; > } > > // This method binds my data to the Views specified in the > child xml > layout > private void bindView(View view, Map<String, ?> data, > String[] from, int[] to, int groupPosition, > int childPosition) { > int len = to.length - 1; > // Apply TextViews > for (int i = 0; i < len; i++) { > TextView v = (TextView) > view.findViewById(to[i]); > if (v != null) { > v.setText((String) data.get(from[i])); > } > > } > // Apply ImageView > ImageView imgV = (ImageView) view.findViewById(to[2]); > if (imgV != null) { > if(childPosition % 2 == 0) > imgV.setImageDrawable(albumCovers.get > (0)); > else > imgV.setImageDrawable(albumCovers.get(1)); > } > } > } > > } > > Layout: media_layout.xml > (For the main activity holding the Expandable List) > ======================================== > > <?xml version="1.0" encoding="utf-8"?> > > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ > android" > android:orientation="vertical" android:layout_width="fill_parent" > android:layout_height="fill_parent" android:paddingTop="104px"> > > <ExpandableListView android:id="@+id/android:list" > android:layout_width="fill_parent" > android:layout_height="fill_parent" > android:layout_weight="1" android:drawSelectorOnTop="false" > android:background="#3f3f3f" /> > > <TextView android:id="@+id/android:empty" > android:layout_width="fill_parent" > android:layout_height="fill_parent" android:text="No Media" /> > > </LinearLayout> > > Layout: media_list_parent.xml > (Defines the layout for parents in the Expandable List) > ======================================== > > <?xml version="1.0" encoding="utf-8"?> > > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ > android" > android:orientation="horizontal" android:layout_width="fill_parent" > android:layout_height="75sp" android:background="#dedede" > android:gravity="center_vertical"> > > <TextView android:id="@+id/rowText1" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > android:paddingLeft="40sp" > style="@style/ListParent" /> > > <TextView android:id="@+id/rowText2" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > style="@style/ListParent" /> > > </LinearLayout> > > Layout: media_list_child.xml > (Defines the layout for children in the Expandable List) > ========================================= > > <?xml version="1.0" encoding="utf-8"?> > > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ > android" > android:orientation="horizontal" android:layout_width="fill_parent" > android:layout_height="70sp" android:gravity="center_vertical"> > > <ImageView android:id="@+id/photoAlbumImg" > android:layout_width="70px" > android:layout_height="70px" /> > > <TextView android:id="@+id/rowText1" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > android:paddingLeft="10sp" > style="@style/ListChild" /> > > <TextView android:id="@+id/rowText2" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > android:paddingLeft="95sp" > style="@style/ListChild" /> > > </LinearLayout> > > ================ > > There you have it, this will allow you to have an Expandable list > which uses views completely designed by yourself. > > Took me a while to solve it, hope it helps you. Let me know if it > does! > > -Miles > > On Dec 18, 10:03 am, "Luke w" <hit...@gmail.com> wrote: > > > Hi mscwd01, > > How about the result ? > > could you give some comment ? > > > BR, > > Luke Wang > > > Jay London - "I was going to buy a book on hair loss, but the pages kept > > falling out." > > > On Tue, Dec 16, 2008 at 12:11 AM, mscwd01 <mscw...@gmail.com> wrote: > > > > Brilliant! Thanks for taking the time to show me this. > > > > I'll have a play and see what I come up with :) > > > > On Dec 15, 2:01 pm, "Gnanesh Radhakrishnan" <gnanesh....@gmail.com> > > > wrote: > > > > Sorry... you include dynamic images also to the textview. > > > > Please refer to setCompoundDrawablesWithIntrinsicBounds method. This > > > might > > > > help. > > > > >http://code.google.com/android/reference/android/widget/TextView.html.. > > > .) > > > > > Gyan. > > > > > On Mon, Dec 15, 2008 at 7:18 PM, Gnanesh Radhakrishnan < > > > > > gnanesh....@gmail.com> wrote: > > > > > > Hey, > > > > > > You can use a SimpleExpandableListAdapter to achieve this. > > > > > > SimpleExpandableListAdapter expListAdapter = > > > > > new SimpleExpandableListAdapter( > > > > > this, > > > > > createGroupList(), // groupData describes the > > > > > first-level entries > > > > > R.layout.group_row, // Layout for the first-level > > > > > entries > > > > > new String[] { "colorName" }, > > ... > > read more » --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---