Android自定义expanddeablelistivew没有xml布局

时间:2011-06-06 08:42:11

标签: android expandablelistview

我正在按照以下代码在android中创建expandablistView with xml layout.its工作正常,但我不知道如何更改图像如果我在子/组布局中使用图像? 我已经完成了在childes / group视图中添加ImageView的代码,但是我如何在子视图/组视图上刷新/更改使用ImageView发布的图像。 。 提前感谢指南

公共类GundamExpAdapter扩展了BaseExpandableListAdapter {     / -------------------------- Fields ------------------- ------- /

private final HashMap<String, ArrayList<String>> myData = new HashMap<String, ArrayList<String>>();
private final HashMap<Integer, String> lookUp = new HashMap<Integer, String>();
private final Context context;



/*-------------------------- Public --------------------------*/

public GundamExpAdapter(final Context con)
{
    context = con;
}

public boolean AddGroup(final String groupName, final ArrayList<String> list)
{
    final ArrayList<String> prev = myData.put(groupName, list);

    if (prev != null)
        return false;

    lookUp.put(myData.size() - 1, groupName);

    notifyDataSetChanged();
    return true;
}

@Override
public Object getChild(int groupPos, int childPos) 
{
    if (lookUp.containsKey(groupPos))
    {
        final String str = lookUp.get(groupPos);
        final ArrayList<String> data = myData.get(str);

        return data.get(childPos);
    }

    return null;
}

@Override
public long getChildId(int groupPos, int childPos) 
{  
    return 0;
}

@Override
public View getChildView(int groupPos, int childPos, boolean isLastChild, 
                         View convertView, ViewGroup parent) 
{
    LinearLayout linear = new LinearLayout(context);

    final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

    TextView text = new TextView(context);

    // Indent
    final String str = "\t\t\t" + (String)getChild(groupPos, childPos);

    linear = new LinearLayout(context);
    linear.setOrientation(LinearLayout.VERTICAL);

    text.setLayoutParams(params);
    text.setText(str);
    linear.addView(text);

    return linear;
}

@Override
public int getChildrenCount(int groupPos) 
{
    if (lookUp.containsKey(groupPos))
        return myData.get(lookUp.get(groupPos)).size();

    return 0;
}

@Override
public Object getGroup(int groupPos) 
{
    if (lookUp.containsKey(groupPos))
        return myData.get(lookUp.get(groupPos));

    return null;
}

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

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

@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) 
{
    LinearLayout linear = new LinearLayout(context);

    final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

    TextView text = new TextView(context);

    // Push the group name slightly to the right for drop down icon
    final String str = "\t\t" + lookUp.get(groupPos);

    linear = new LinearLayout(context);
    linear.setOrientation(LinearLayout.VERTICAL);

    text.setLayoutParams(params);
    text.setText(str);
    text.setTextSize(18.0f);
    linear.addView(text);

    return linear;
}

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

@Override
public boolean isChildSelectable(int groupPos, int childPos) 
{
    return false;
}

} 其活动如下

public class Main extends ExpandableListActivity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        GundamExpAdapter gea = new GundamExpAdapter(this);

        ArrayList<String> models = new ArrayList<String>();
        models.add("NZ 666 Kshatriya");
        models.add("Unicorn");
        models.add("Sinanju");
        gea.AddGroup("Unicorn", models);

        models = new ArrayList<String>();
        models.add("DeathScythe");
        models.add("Altron");
        models.add("HeavyArms");
        models.add("SandRock");
        models.add("Epyon");
        models.add("ZERO");
        gea.AddGroup("Wing", models);

        setListAdapter(gea);
        ExpandableListView elv = getExpandableListView();
        elv.setTextFilterEnabled(true);
        //listview.setOnItemClickListener(OnClickingListItem());
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您需要更改数据,以便知道哪一行会有不同的图片。 然后你可以调用BaseExpandableListAdapter.notifyDataSetChanged(),它会在每一行上再次调用getChildView。然后,您将能够在每个要更改图像的图像视图上setBackgroundDrawable()