android - 如何在android中可扩展 ListView 的每个部分添加不同的子项?

标签 android expandablelistview

我想在android中的可扩展列表的每个部分中显示不同的子项。所以我写了以下几行:

//SmplExpandable.java
package com.bogotobogo.android.smplexpandable;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HeterogeneousExpandableList;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Demonstrates expandable lists backed by a Simple Map-based adapter
 */
public class SmplExpandable extends ExpandableListActivity implements HeterogeneousExpandableList{


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>();
        List<List<Map<String, Object>>> childData = new ArrayList<List<Map<String, Object>>>();

            Map<String, Object> curGroupMap = new HashMap<String, Object>();
            curGroupMap = new HashMap<String, Object>();
            curGroupMap.put("Name","Recipe");
            groupData.add(curGroupMap);

            curGroupMap = new HashMap<String, Object>();
            curGroupMap.put("Name","Feedback");
            groupData.add(curGroupMap);

            curGroupMap = new HashMap<String, Object>();
            curGroupMap.put("Name","Images");
            groupData.add(curGroupMap);

            curGroupMap = new HashMap<String, Object>();
            curGroupMap.put("Name","Video");
            groupData.add(curGroupMap);

            List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
            Map<String, Object> curChildMap = new HashMap<String, Object>();
            curChildMap = new HashMap<String, Object>();

            curChildMap.put("MyRecipe",getResources().getString(R.string.My_Recipe));
            children.add(curChildMap);
            childData.add(children);

            children = new ArrayList<Map<String, Object>>();
            curChildMap = new HashMap<String, Object>();
            curChildMap.put("MyFeedback", getResources().getString(R.string.My_Feedback));
            children.add(curChildMap);
            childData.add(children);

            children = new ArrayList<Map<String, Object>>();
            curChildMap = new HashMap<String, Object>();
            curChildMap.put("MyImage", getResources().getDrawable(R.drawable.icon));
            children.add(curChildMap);
            childData.add(children);

            children = new ArrayList<Map<String, Object>>();
            curChildMap = new HashMap<String, Object>();
            curChildMap.put("MyImage", getResources().getDrawable(R.drawable.icon));
            children.add(curChildMap);

            childData.add(children);

        // Set up our adapter

            setListAdapter( new SimpleExpandableListAdapter(
                    this,
                    groupData,
                    android.R.layout.simple_expandable_list_item_1,
                    new String[] {"Name"},            // the name of the field data
                    new int[] { android.R.id.text1 }, // the text field to populate with the field data
                    childData,
                    0,
                    null,
                    new int[] {}
                ) 

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


                        final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
                        if (groupPosition == 0)
                        {
                        // Populate your custom view here

                        ((TextView)v.findViewById(R.id.TextView01)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyRecipe") );
                        }

                        else if (groupPosition == 1)
                        {
                        // Populate your custom view here
                            ((TextView)v.findViewById(R.id.TextView02)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyFeedback") );
                        }

                        else if (groupPosition == 2)
                        {
                        // Populate your custom view here
                             ((ImageView)v.findViewById(R.id.ImageView01)).setImageDrawable((Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyImage"));
                        }

                        else if (groupPosition == 3)
                        {
                        // Populate your custom view here
                             ((ImageView)v.findViewById(R.id.ImageView01)).setImageDrawable((Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("MyImage"));
                        }

                        return v;

                    }

                    @Override
                    public View newChildView(boolean isLastChild, ViewGroup parent) {
                        {

                            return layoutInflater.inflate(R.layout.myrow, null, false);
                        }
                    }
                }

             );
        // Set the width and height of activity.
        WindowManager.LayoutParams params = getWindow().getAttributes();  
        params.height =400;  
        params.width = 400;  
        this.getWindow().setAttributes(params);  

    }

}

文件 myrow.xml:

<?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="wrap_content">
      <ImageView android:id="@+id/ImageView01"
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content">
   </ImageView>

    <TextView android:id="@+id/TextView01"
     android:layout_width="fill_parent"
     android:background="#FFFFFF"
     android:textColor="#FF0000"
     android:textSize="10px"
     android:layout_height="fill_parent"/>

    <TextView android:id="@+id/TextView02"
     android:layout_width="fill_parent"
     android:background="#FFFFFF"
     android:textColor="#FF0000"
     android:textSize="10px"
     android:layout_height="fill_parent"/>



    <VideoView android:id="@+id/VideoView01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content">
    </VideoView>

</LinearLayout>

我的问题是,我能够在所有四个部分中看到文本和图像,而我只想在前两个部分中显示文本,在其他两个部分中显示图像。

谁能告诉我我做错了什么?

谢谢, 内哈

最佳答案

你的问题很简单。

您正在夸大 View

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 

适配器的方法。

但是,这个膨胀 View 同时包含 TextView 和 ImageView 。如果只想显示一个,则需要隐藏另一个。

 if (groupPosition == 0)
  {                    
// set your text hide
// HIDE YOUR IMAGE VIEW
  ImageView iv = ...
  iv.setVisibility(View.GONE);
  }

...

 if (groupPosition == 3)
  {                    
// set your image source
// HIDE YOUR TEXT VIEW
   TextView tv = ...
   t.setVisibility(View.GONE);
  }

这应该可以解决您的问题,您应该能够从这里完成它:)

关于android - 如何在android中可扩展 ListView 的每个部分添加不同的子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4593027/

相关文章:

android - 可扩展 ListView ;添加 itms 会弄乱展开的项目

android - 膨胀类 ImageView 时出错

android - 通过 Python 向应用程序发送推送/更新通知?

android - 不调用 Activity.onResume() 的可能性

java - Android:ExpandableListViews 和复选框

java - Android:在 ExpandableListViewActivity 的子级中访问 TextView

android - 非参数化数组列表的后果?

Android 下载多个文件并在 ListView 中显示进度

android - 可扩展 ListView 底部分隔线

android - 在 android 的嵌套 ExpandableListView 中没有子项可见