2012-10-19 52 views
1

我想使用Eclipse在Android中製作一個簡單的任務列表。我試圖構建的佈局是這樣的:ExpandableListView與Android中的複選框

Layout

我要的任務是羣體,而且每個任務下,2名兒童與任務描述和任務的限制日期。

在任務/組的右側,我想要一個用於標記任務的複選框,但我不知道該怎麼做。我甚至在我的ExpandableListView中使用適配器時遇到了問題(所有的問題都可以通過使用ListViews來創建ExpandableListView & Maps的字符串,但我想在我的XML佈局中使用已經創建的ExpandableListView)我將從輸入中添加任務(輸入是按下按鈕時出現的AlertDialog)。

任何想法將不勝感激,併爲我的英語(不是我的母語)感到遺憾。

回答

8

我會這樣做的方式是使您的可擴展列表視圖利用自定義適配器(這些擴展baseexpandableadapter類)。然後,您可以爲組和子視圖充氣或創建不同的視圖。這是,如果你使用的是展開listactivity你怎麼可能將它設置一個例子(VS可擴展列表視圖的常規性活動):

public class ExpandableList1 extends ExpandableListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Set up our adapter 
    ExpandableListAdapter mAdapter = new MyExpandableListAdapter(this); 
    setListAdapter(mAdapter); 
} 
    // The custom adapter part 
public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
    // Sample data set. children[i] contains the children (String[]) for groups[i]. 
    private String[] groups = { "Task1", "Task2", "Task3", "Task4"}; 
    private String[][] children = { 
      { "Task Description", "Task time limit date" }, 
      { "Task Description", "Task time limit date" }, 
      { "Task Description", "Task time limit date" }, 
      { "Task Description", "Task time limit date" } 
    }; 

public MyExpandableListAdapter(Context context) { 
    super(); 
    this.context = context; 
} 

private Context context; 

static class ViewHolder { 
     TextView text; 
     CheckBox checkbox; 
    } 

    public Object getChild(int groupPosition, int childPosition) { 
     return children[groupPosition][childPosition]; 
    } 

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

    public int getChildrenCount(int groupPosition) { 
     return children[groupPosition].length; 
    } 

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
      View convertView, ViewGroup parent) { 
    return convertView; 
    } 

    public Object getGroup(int groupPosition) { 
     return groups[groupPosition]; 
    } 

    public int getGroupCount() { 
     return groups.length; 
    } 

    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

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

    final ViewHolder holder; 

     if (convertView == null) { 
      LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = viewInflater.inflate(R.layout.mtopics_groupview, parent, false); 
      holder = new ViewHolder(); 
      holder.text = (TextView)convertView.findViewById(R.id.mtopicsgrouptv); 
      holder.checkbox = (CheckBox)convertView.findViewById(R.id.cb_group); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

    holder.text.setText(getGroup(groupPosition).toString()); 
    return convertView; 
    } 

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

    public boolean hasStableIds() { 
     return true; 
    } 

} 

你必須通過程序或創建您的組視圖和子視圖的能力你可以從xml中膨脹這些。在我的例子中,我已經從xml中爲組視圖誇大了,因爲它對於更復雜的視圖來說更容易。我知道這種模式看起來像是一種使用兩行代碼的循環方法,但它會極大地幫助您實現您的觀點。你可以在這裏瞭解更多:http://www.youtube.com/watch?v=wDBM6wVEO70。至於現在,這個xml非常簡單,它只是一個textview和一個複選框。 (小提示:在groupviews複選框必須有「可聚焦」設置爲假,並且不能直接相鄰的組視圖指示符,或組視圖將不能展開)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <CheckBox 
     android:id="@+id/cb_group" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:checked="true" 
     android:focusable="false" /> 

    <TextView 
     android:id="@+id/mtopicsgrouptv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="40dp" 
     android:focusable="false" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

</RelativeLayout> 

我沒有做childview,但取決於你想用它做什麼,你可以在組視圖之後對它進行建模。你可以在裏面放一個文本視圖,並根據子位置設置文本。 但是我得警告你,截至目前你很快就會發現你的複選框狀態在你點擊它們時會變得古怪。有時支票會移動或消失或根本不起作用。如果你想知道爲什麼我可以告訴你,但我現在只是發佈解決方案。你必須設置一個數組列表或地圖或任何真正的複選框狀態,然後加載它們(因爲它們的位置將對應)。這裏是我如何在我的getGroupView部分做到這一點。

// make the list 
ArrayList<Boolean> group_check_states = new ArrayList<Boolean> (groups.length); 
     for(int i = 0; i < groups.length; i++) { 
     group_check_states.add(true); 
    } 
// load the checkstates 
    if ((group_check_states.get(groupPosition)) == true) { 
     holder.checkbox.setChecked(true); 
    } else { 
     holder.checkbox.setChecked(false); 
    } 

// save the checkstates 
holder.checkbox.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      if (holder.checkbox.isChecked()) { 
       group_check_states.set(groupPosition, true); 
      } else { 
       group_check_states.set(groupPosition, false); 
       } 
      } 
    }); 

您也可以將您的複選框操作代碼放在那裏。現在適配器看起來是這個部分,但它可能對你想要的來說太簡單了。您希望能夠根據您的警報對話框輸入的內容動態設置任務。在這種情況下,您不希望僅僅使用那些使用了數組(我們使用過的組和子組),您可能想要從數組列表或其他類型的列表中加載數據,這些列表可以動態地動態添加和刪除數據,這會通過您的警報對話框進行修改。

我希望它有幫助,但我相信會有更多的問題。只是霍拉,如果有些事情出現。