Android 可展开列表查看组和孩子的不同图像

Android 可展开列表查看组和孩子的不同图像,android,expandablelistview,expandablelistadapter,Android,Expandablelistview,Expandablelistadapter,我有基本的expandablelistview,我想知道如何让每个孩子和组都有不同的图像。我是否需要以某种方式修改可扩展列表适配器?任何帮助、想法和评论都将不胜感激。谢谢:) 主要活动: public class MainActivity extends Activity{ ..... public static final Integer[] images = { R.drawable.vegetables, R.d

我有基本的
expandablelistview
,我想知道如何让每个孩子和组都有不同的图像。我是否需要以某种方式修改可扩展列表适配器?任何帮助、想法和评论都将不胜感激。谢谢:)

主要活动:

    public class MainActivity extends Activity{
           .....
public static final Integer[] images = {
            R.drawable.vegetables,
            R.drawable.fruits,
            R.drawable.drinks,
            R.drawable.dessert

        };

            @Override
            protected void onCreate(Bundle savedInstanceState) {


        }
}

我的适配器

public class ExpandableListAdapter extends BaseExpandableListAdapter{


    @Override
    public View getChildView(int groupPosition,final int childPosition, boolean isLastChild, View convertView,
            ViewGroup parent) {

         final String childText = (String) getChild(groupPosition, childPosition);

          if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item, null);
            }
          TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.lblListItem);
          txtListChild.setText(childText);

          ImageView imgListChild= (ImageView) convertView
                  .findViewById(R.id.IV_childImage);

// I could store images somehow in two dimensional array and then depending on that draw their reference id.
//          imgListChild.setImageResource();

        return convertView;
    }


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {


         String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }

            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);
            ImageView imgListGroup= (ImageView) convertView
                    .findViewById(R.id.IV_group);

            imgListGroup.setImageResource(MainActivity.images[groupPosition]);

            return convertView;
    }

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










}

list_组
list_项
xml文件中添加
ImageView
,并将图像设置为适配器类中的那些ImageView

就像您将文本设置为
TextView
,同样,您也可以将图像设置为
ImageView

TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.lblListItem);
          txtListChild.setText(childText);
ImageView imgListChild = (ImageView) convertView
                    .findViewById(R.id.image);

            imgListChild.setImageResource(R.drawable.imagename);
您的xml文件应该是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/lblListHeader"
    android:layout_width="wrap_content"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_marginLeft="8dp"
    android:drawableRight="@drawable/ic_launcher"
    android:gravity="center_horizontal"
    android:paddingLeft="32dp"
    android:paddingTop="8dp"
    android:text="Test"
    android:textSize="20sp"
    android:textAlignment="textEnd"
    android:textStyle="bold" >


</CheckedTextView> 

    <ImageView 
    android:id="@+id/IV_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"   
    android:contentDescription="group image"    

        />
</LinearLayout>
将其填充到
prepareListData()
函数中

groupImages= new ArrayList<Integer>();
         groupImages.add(R.drawable.vegetables);
         groupImages.add(R.drawable.fruits);
         groupImages.add(R.drawable.drinks);
         groupImages.add(R.drawable.deserts);

         childImages = new HashMap<Integer, List<Integer>>();
         List<Integer> vegetablesi = new ArrayList<Integer>();
         vegetablesi.add(R.drawable.Tomatoes);
         vegetablesi.add(R.drawable.Potatoes);
         vegetablesi.add(R.drawable.Cucumbers);
         vegetablesi.add(R.drawable.Pumpkins);
         vegetablesi.add(R.drawable.Peppers);
         vegetablesi.add(R.drawable.Onions);
         vegetablesi.add(R.drawable.Garlic);

         List<Integer> fruitsi = new ArrayList<Integer>();
         fruitsi.add(R.drawable.Strawberries);
         fruitsi.add(R.drawable.Blackcurrants);
         fruitsi.add(R.drawable.Redcurrant);
         fruitsi.add(R.drawable.Gooseberry);
         fruitsi.add(R.drawable.Kiwifruit);
         fruitsi.add(R.drawable.Grape);


         List<Integer> drinksi = new ArrayList<Integer>();
         drinksi.add(R.drawable.Vodka);
         drinksi.add(R.drawable.Milk);
         drinksi.add(R.drawable.Water);
         drinksi.add(R.drawable.CocaCola);
         drinksi.add(R.drawable.Sprite);


         List<Integer> desertsi = new ArrayList<Integer>();
         desertsi.add(R.drawable.Vodka);
         desertsi.add(R.drawable.Milk);
         desertsi.add(R.drawable.Water);
         desertsi.add(R.drawable.CocaCola);
         desertsi.add(R.drawable.Sprite);


         childImages.put(groupImages.get(0), vegetablesi);
         childImages.put(groupImages.get(1), fruitsi);
         childImages.put(groupImages.get(2), drinksi);
         childImages.put(groupImages.get(3), desertsi);

在getChildView方法中,您可以通过方法的签名(变量childPosition和groupPosition)来标识实际的子列表项


因此,您可以使用开关结构加载相关图像。

Ok,但如何实现“将图像设置为适配器类中的那些图像视图”?:)顺便说一句,谢谢你的帮助:)哦。。。您的意思是我应该在
getChildView
getGroupView
中为要添加的子图像和组图像执行此操作吗?:)这看起来很有希望,我会试着给你回复的。是的。你应该在
getChildView
getGroupview
中都这么做。很抱歉,我是新来的,如果我理解正确,我会仔细检查一下。:)有一件事,我的组xml是
CheckedTextView
,我在添加imageView时遇到了一些困难,你能告诉我我做错了什么吗?更新以上同一问题需要帮助您可以帮助吗
groupImages= new ArrayList<Integer>();
         groupImages.add(R.drawable.vegetables);
         groupImages.add(R.drawable.fruits);
         groupImages.add(R.drawable.drinks);
         groupImages.add(R.drawable.deserts);

         childImages = new HashMap<Integer, List<Integer>>();
         List<Integer> vegetablesi = new ArrayList<Integer>();
         vegetablesi.add(R.drawable.Tomatoes);
         vegetablesi.add(R.drawable.Potatoes);
         vegetablesi.add(R.drawable.Cucumbers);
         vegetablesi.add(R.drawable.Pumpkins);
         vegetablesi.add(R.drawable.Peppers);
         vegetablesi.add(R.drawable.Onions);
         vegetablesi.add(R.drawable.Garlic);

         List<Integer> fruitsi = new ArrayList<Integer>();
         fruitsi.add(R.drawable.Strawberries);
         fruitsi.add(R.drawable.Blackcurrants);
         fruitsi.add(R.drawable.Redcurrant);
         fruitsi.add(R.drawable.Gooseberry);
         fruitsi.add(R.drawable.Kiwifruit);
         fruitsi.add(R.drawable.Grape);


         List<Integer> drinksi = new ArrayList<Integer>();
         drinksi.add(R.drawable.Vodka);
         drinksi.add(R.drawable.Milk);
         drinksi.add(R.drawable.Water);
         drinksi.add(R.drawable.CocaCola);
         drinksi.add(R.drawable.Sprite);


         List<Integer> desertsi = new ArrayList<Integer>();
         desertsi.add(R.drawable.Vodka);
         desertsi.add(R.drawable.Milk);
         desertsi.add(R.drawable.Water);
         desertsi.add(R.drawable.CocaCola);
         desertsi.add(R.drawable.Sprite);


         childImages.put(groupImages.get(0), vegetablesi);
         childImages.put(groupImages.get(1), fruitsi);
         childImages.put(groupImages.get(2), drinksi);
         childImages.put(groupImages.get(3), desertsi);
@Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
         String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }

            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);

            ImageView imageView = (ImageView) convertView.findViewById(R.id.IV_group);
            int imageId = this.groupImages.get(groupPosition);
            imageView.setImageResource(imageId);

            return convertView;
    }





@Override
    public View getChildView(int groupPosition,final int childPosition, boolean isLastChild, View convertView,
            ViewGroup parent) {

         final String childText = (String) getChild(groupPosition, childPosition);

          if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_item, null);
            }
          TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.lblListItem);
          txtListChild.setText(childText);

          ImageView imageView = (ImageView) convertView.findViewById(R.id.IV_child);
          int imageId = this.childImages.get(this.groupImages.get(groupPosition)).get(childPosition);
          imageView.setImageResource(imageId);

        return convertView;
    }