2

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

我已创建使用自定义适配器 一个消耗列表视图,但我想让它像下面

的扩张列表视图包含数据的性别(男性和女性)

如果性别是男的,当我们点击时,子视图应该表现出男性的菜单

如果性别是女性,当我们点击时,子视图应该表现出女性菜单

如何实现它?

这里是我的适配器代码

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

}

回答

2

你需要男,女两性创建两个不同的XML布局,然后在getChildView方法:

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

THX的家伙,我需要从你的答案修改一点点......我会尽快发布我的解决方案...... – DumDum 2014-09-01 04:36:48