我想在android中的Expandable列表的每个部分中显示不同的子节点。所以我写了以下几行:
//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>
我的问题是,我能够在所有四个部分中看到文本和图像,而我想只在前两个部分显示文本,在其他两个部分显示图像。
有人可以告诉我,我做错了吗?
谢谢, 尼哈
答案 0 :(得分:3)
你的问题很简单。
您正在
中夸大视图public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
适配器的方法。
但是,这个膨胀的视图包含文本视图和图像视图。如果您只想显示一个,则需要隐藏另一个。
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);
}
这应该解决你的问题,你应该能够从这里完成它:)