可扩展列表视图中的不同子视图android

时间:2014-08-29 09:37:02

标签: android android-layout android-listview android-adapter custom-adapter

是否可以在可消耗列表视图的每一行中添加不同的子视图XML?

我使用自定义适配器创建了一个可消耗列表视图 但我想把它变成以下

可展开列表视图包含数据性别(男性和女性)

如果性别是男性,当我们点击时,子视图应显示男性菜单

如果性别是女性,当我们点击时,子视图应显示女性菜单

如何实现它?

这是我的适配器代码

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

}

1 个答案:

答案 0 :(得分:2)

您需要在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;
}