gpt4 book ai didi

android - 可扩展 ListView android中的不同 subview

转载 作者:行者123 更新时间:2023-12-05 00:22:12 25 4
gpt4 key购买 nike

是否可以在消耗 ListView 的每一行中添加不同的 subview XML?

我使用 创建了一个消耗性 ListView 定制适配器
但我想让它像下面这样

可展开的 ListView 包含数据性别(男性和女性)

如果性别是男性,当我们点击时, subview 应该显示男性菜单

如果性别是女性,当我们点击时, subview 应该显示女性菜单

如何实现呢?

这是我的适配器代码

 package com.example.expandlist;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<Group> groups;

public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

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

Child child = (Child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.country_name);
ImageView iv = (ImageView) convertView.findViewById(R.id.flag);

tv.setText(child.getName().toString());
iv.setImageResource(child.getImage());

return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
return groups.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getName());
return convertView;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}

最佳答案

您需要在 getChildView 中为男性和女性创建两个不同的 XML 布局。方法:

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

Child child = (Child) getChild(groupPosition, childPosition);
if (child.getGender==MALE){
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_item_male, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.country_name);
ImageView iv = (ImageView) convertView.findViewById(R.id.flag);

tv.setText(child.getName().toString());
iv.setImageResource(child.getImage());
}else{
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_item_female, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.country_name);
ImageView iv = (ImageView) convertView.findViewById(R.id.flag);

tv.setText(child.getName().toString());
iv.setImageResource(child.getImage());

return convertView;
}

关于android - 可扩展 ListView android中的不同 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25565560/

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