0

私はアンドロイドに拡張可能なリストを持っていて、文字列で簡単に埋めることができますが、リストの各行にドローアブルを追加したい (各行に別のドローアブルを追加したい)、すでにコードでこれを行う最も簡単な方法は何ですか?持ってる?ありがとう。

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 を再定義します。

そこから、従うべき2つのルートがあります.1つは、プログラムでimageviewsを子/グループビューに追加するか(このアプローチはお勧めしません)、または子/グループビューのxmlレイアウトファイルを定義して膨らませます(そしておそらくコンテンツを動的に編集します)

単純なアダプターは、優れた結果を得るのに十分なほど柔軟ではありません。特に、要素のカスタム ビューが必要な場合。(スケーラビリティは言うまでもありません。要件が将来拡大する可能性があることと、柔軟なベースにより自由度が高まり、コードをスクラッチしすぎないようにすることを常に念頭に置いて、プロジェクトとコードを作成してください)

于 2011-11-21T15:28:28.640 に答える