Android ApiDemos樣本解析(112):Views->Expandable Lists-&am

來源:互聯網
上載者:User

上一篇:http://www.bkjia.com/kf/201208/146527.html

Expandable List 與普通List的區別在於Expandable List可以顯示兩個層次,第一層稱為”Group” 第二層為”Child” 。其中”Child”可以摺疊或展開,有點像只支援兩級的TreeView(檔案瀏覽器)。

用法上和List 非常類似,下面的對應關係可以協助理解Expandable List

ExpandableListActivity –>ListActivity
ExpandableListView –> ListView
ExpandableListAdapter –>ListAdapter
ExpandableListActivity 預設的Layout 為一置中全螢幕顯示的列表(兩級)。Android也允許使用自訂的Layout來顯示Expandable List. 為了使用自訂的Layout的,在XML中必須包括一個ExpandableListView對象且id必須為 @android:id/list。 此外也可以為空白列表定義一個View,此View的id為 @id/android:empty。

如下例為ExpandableListActivity 自訂一個Layout ,為在列表為空白時顯示“No Data”.可以在onCreate 使用setContentView設定這個自訂Layout .

<?xml version=”1.0″ encoding=”UTF-8″?>
< LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingLeft=”8dp”
android:paddingRight=”8dp”>
<ExpandableListView android:id=”@id/android:list”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#00FF00″
android:layout_weight=”1″
android:drawSelectorOnTop=”false”/>

<TextView android:id=”@id/android:empty”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#FF0000″
android:text=”No data”/>
< /LinearLayout>

ExpandableListActivity 調用setListAdapter(ExpandableListAdapter) 設定ExpandableListAdapter, ExpandableListAdapter 允許為”group” 和 “child”分別設定View。

例如本例 MyExpandableListAdapter 派生自BaseExpandableListAdapter,重載了getChildView 和getGroupView,分別為group 和child定義一個TextView,TextView的文本為Group名稱或是Child的名稱,分別對應在groups 和children。

[java]
// Sample data set.  children[i] contains  
//the children (String[]) for groups[i].  
private String[] groups = { "People Names", "Dog Names", 
 "Cat Names", "Fish Names" }; 
private String[][] children = { 
 { "Arnold", "Barry", "Chuck", "David" }, 
 { "Ace", "Bandit", "Cha-Cha", "Deuce" }, 
 { "Fluffy", "Snuggles" }, 
 { "Goldy", "Bubbles" } 
}; 
 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
 View convertView, ViewGroup parent) { 
 TextView textView = getGenericView(); 
 textView.setText(getChild(groupPosition, childPosition).toString()); 
 return textView; 
} 
 
 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
 ViewGroup parent) { 
 TextView textView = getGenericView(); 
 textView.setText(getGroup(groupPosition).toString()); 
 return textView; 
} 

// Sample data set.  children[i] contains
//the children (String[]) for groups[i].
private String[] groups = { "People Names", "Dog Names",
 "Cat Names", "Fish Names" };
private String[][] children = {
 { "Arnold", "Barry", "Chuck", "David" },
 { "Ace", "Bandit", "Cha-Cha", "Deuce" },
 { "Fluffy", "Snuggles" },
 { "Goldy", "Bubbles" }
};

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
 View convertView, ViewGroup parent) {
 TextView textView = getGenericView();
 textView.setText(getChild(groupPosition, childPosition).toString());
 return textView;
}

 public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
 ViewGroup parent) {
 TextView textView = getGenericView();
 textView.setText(getGroup(groupPosition).toString());
 return textView;
}

getExpandableListView() 取得當前Activity 對應的ExpandableListView, 然後調用registerForContextMenu 為 ExpandableListView 添加Context Menu.

然後在onContextItemSelected事件中為List View 中的每個list Item 添加事件,本例使用Toast 顯示一行文本,顯示當前點擊的事件。

[java] 
public boolean onContextItemSelected(MenuItem item) { 
 ExpandableListContextMenuInfo info 
 = (ExpandableListContextMenuInfo) item.getMenuInfo(); 
 
 String title = ((TextView) info.targetView).getText().toString(); 
 
 int type = ExpandableListView.getPackedPositionType(info.packedPosition); 
 if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
 int groupPos 
 = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
 int childPos 
 = ExpandableListView.getPackedPositionChild(info.packedPosition); 
 Toast.makeText(this, title + ": Child " + childPos 
 + " clicked in group " + groupPos, 
 Toast.LENGTH_SHORT).show(); 
 return true; 
 } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
 int groupPos 
 = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
 Toast.makeText(this, title + ": Group " + groupPos 
 + " clicked", Toast.LENGTH_SHORT).show(); 
 return true; 
 } 
 
 return false; 
} 

public boolean onContextItemSelected(MenuItem item) {
 ExpandableListContextMenuInfo info
 = (ExpandableListContextMenuInfo) item.getMenuInfo();

 String title = ((TextView) info.targetView).getText().toString();

 int type = ExpandableListView.getPackedPositionType(info.packedPosition);
 if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
 int groupPos
 = ExpandableListView.getPackedPositionGroup(info.packedPosition);
 int childPos
 = ExpandableListView.getPackedPositionChild(info.packedPosition);
 Toast.makeText(this, title + ": Child " + childPos
 + " clicked in group " + groupPos,
 Toast.LENGTH_SHORT).show();
 return true;
 } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
 int groupPos
 = ExpandableListView.getPackedPositionGroup(info.packedPosition);
 Toast.makeText(this, title + ": Group " + groupPos
 + " clicked", Toast.LENGTH_SHORT).show();
 return true;
 }

 return false;
}

 
作者:mapdigit
 


 

 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.