gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:16:04 26 4
gpt4 key购买 nike

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

//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 的每个部分添加不同的 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4593027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com