如何在可扩展Listview中为每个组添加不同的图像

时间:2016-02-25 13:07:09

标签: java android expandablelistview

在我的可扩展列表视图中,有三个组。我想为该组添加不同的三个图像。我尝试了很多代码,但没有工作。

我的代码:

主要活动:

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

}

请有人帮帮我!

提前致谢

1 个答案:

答案 0 :(得分: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的对象传递。