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