0

我在 android 中有一个可扩展列表,我可以轻松地用字符串填充它,但现在我想在列表的每一行(每行不同的一个)上添加一个可绘制对象,使用我已经编写的代码执行此操作的最简单方法是什么有?谢谢。

public class Physical extends ExpandableListActivity {
public static final int GROUPS = 1;
public SimpleExpandableListAdapter expListAdapter;
private boolean expanded = true;

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

    String [] cats = { "phy1", "phys2" };

    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    for (int i = 0; i < GROUPS; i++) {
        Map<String, String> curGroupMap = new HashMap<String, String>();
        groupData.add(curGroupMap);
        curGroupMap.put( "group", "Categories" );

        List<Map<String, String>> children = new ArrayList<Map<String, String>>();
        for( int n = 0 ; n < cats.length ; n++ ) {
            Map<String, String> curChildMap = new HashMap<String, String>();
            curChildMap.put( "child", cats[n] );
            children.add(curChildMap);
        }
        childData.add(children);
    }

    expListAdapter =
            new SimpleExpandableListAdapter(
                    /* context */
                    this,
                    /* creates Group list */
                    groupData, 
                    /*Group item layout XML */
                    R.layout.group_row,    
                    /* the key of each group element (not child's) */
                    new String[] { "group" },
                    /* data under the key goes into this TextView */
                    new int[] { R.id.group_text }, 
                    /* creates Child List (sub-level entries) */
                    childData,
                    /* layout for children in list */
                    R.layout.child_sub,
                    /* the key of each child element */
                    new String[] { "child" },
                    /* data under the child keys go into this textView */
                    new int[] { R.id.child_text }
                );
            /* sets up and initializes the adapter for the list */
            setListAdapter(expListAdapter);

            getExpandableListView().setOnGroupExpandListener( new OnGroupExpandListener() {
                 public void onGroupExpand(int groupPosition) {
                     expanded = true;
                   }
               });
            getExpandableListView().setOnGroupCollapseListener( new OnGroupCollapseListener() {
                public void onGroupCollapse(int groupPosition) {
                    expanded = false;
                }
            });
            if (expanded != false ) {
                getExpandableListView().expandGroup(0);
            }
}
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, final int childPosition, long id) {
    switch (childPosition) {
    case 0:
        Intent spine_plus_intent = new Intent(Physical.this, SpinePlus.class);
        startActivity(spine_plus_intent);
        finish();
        break;
    }
    return true;
}

}

4

1 回答 1

1

您将定义一个自定义可扩展列表适配器并重新定义 getchildview / getgroupview。

从那里你有两条路线可以遵循:第一,你可以以编程方式将图像视图添加到子/组视图中(我不鼓励你使用这种方法),或者你为子/组视图定义一个 xml 布局文件并膨胀它(并可能动态编辑内容)

简单的适配器不够灵活,无法获得好的结果。特别是当您想要为其元素自定义视图时。(更不用说可扩展性:始终记住项目和代码,您的需求可能会在未来扩大,灵活的基础可以提供更多自由并防止您编写太多代码)

于 2011-11-21T15:28:28.640 回答