2016-02-25 3 views
0

В моем Расширяемом Listview есть три группы. Я хочу добавить три разных изображения для этой группы. Я пробовал много кодов, но не работал.Как добавить другое изображение для каждой группы в расширяемом Listview

Мой код:

Основная деятельность:

HashMap<String, List<String>> rightDrawerListDetail = getData(); 
List<String> rightDrawerListTitle = new ArrayList<String>(rightDrawerListDetail.keySet()); 
    adapterR = new CustomExpandableListAdapter(this, rightDrawerListTitle,rightDrawerListDetail); 
    mRightDrawerList.setAdapter(adapterR); 


private HashMap<String, List<String>> getData(){ 

    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>(); 
    ArrayList<String> arrayList = mydb.getAllAddress(); 

    List asRider = new ArrayList(); 
    asRider.add("hello"); 

    List asRidee = new ArrayList(); 
    asRidee.add("hai"); 

    List recent = new ArrayList(); 
    recent.add("Success"); 

    expandableListDetail.put("As a Rider",asRider); 
    expandableListDetail.put("As a Ridee",asRidee); 
    expandableListDetail.put("My Recent Activities",recent); 


    return expandableListDetail; 
} 

CustomExpandableListAdapter.java:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter { 

private Context mContext; 
private List<String> mGroups; 
private LayoutInflater mInflater; 
private HashMap<String, List<String>> mexpandableListDetail; 

public CustomExpandableListAdapter(Context context, List<String> groups,HashMap<String, List<String>> expandableListDetail) { 
    mContext = context; 
    mGroups = groups; 
    mexpandableListDetail = expandableListDetail; 
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

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

@Override 
public int getChildrenCount(int groupPosition) { 
    return mexpandableListDetail.get(mGroups.get(groupPosition)).size(); 
} 

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

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    return mexpandableListDetail.get(mGroups.get(groupPosition)).get(childPosition); 
} 

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

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

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

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.right_drawer_group, null); 
    } 

    // Get the group item 
    String listTitle = (String) getGroup(groupPosition); 
    // Set group name 
    TextView textView = (TextView) convertView.findViewById(R.id.textGroup); 
    textView.setText(listTitle); 
    ImageView indicator = (ImageView) convertView.findViewById(R.id.groupIndicator); 
    if (isExpanded) { 
     indicator.setImageResource(R.drawable.arrowup); 
    } else { 
     indicator.setImageResource(R.drawable.arrowdown); 
    } 

    return convertView; 
} 

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


    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.right_drawer_child, null); 
    } 

    // Get child name 
    String children = (String) getChild(groupPosition, childPosition); 

    // Set child name 
    TextView text = (TextView) convertView.findViewById(R.id.textChild); 
    text.setText(children); 

    /*convertView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(mContext, children, Toast.LENGTH_SHORT).show(); 
     } 
    });*/ 

    return convertView; 
} 

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

}

Пожалуйста, кто-нибудь помочь мне!

Заранее спасибо

ответ

0

В вашем right_drawer_group.xml добавить ImageView, где вы хотите, чтобы ваша группа изображений для размещения, позволяет сказать groupImage.

В вашем getGroupView:

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

if (convertView == null) { 
    convertView = mInflater.inflate(R.layout.right_drawer_group, null); 
} 
//Set group image 
ImageView groupImage = (ImageView)convertView.findViewById(R.id.groupImage); 
switch((String)getGroup(groupPosition)) 
{ 
case "As a Rider": 
    //groupImage set correct image 
    break; 
case "As a Ridee": 
    //groupImage set correct image 
    break; 
case "My Recent Activities": 
    //groupImage set correct image 
    break; 
} 

// Get the group item 
String listTitle = (String) getGroup(groupPosition); 
// Set group name  
TextView textView = (TextView) convertView.findViewById(R.id.textGroup); 
textView.setText(listTitle); 
ImageView indicator = (ImageView) convertView.findViewById(R.id.groupIndicator); 
if (isExpanded) { 
    indicator.setImageResource(R.drawable.arrowup); 
} else { 
    indicator.setImageResource(R.drawable.arrowdown); 
} 

return convertView; 
} 

Это не самое изящное решение, как если бы вы изменить название группы это будет перерыв, но он будет работать в настоящее время. Лучшим решением было бы передать группы как объекты, содержащие getName и getImageId.