android:indicatorRight
</td>
|
組列表項指示器的右邊約束位置 ⑦
</td>
|
</td>
|
</td>
|
</td>
|
</td>
|
</td>
|
</td>
|
</td>
|
</td>
|
</td>
</tr>
</tbody>
</table>
備注:
① 注:圖片不會完全顯示,分離子列表項的是一條直線
② 注:可以是一個圖片
③ 注:即從左端0位置開始計數,比如,假設指示符是一個圖標,給定這個屬性值為
3dip,則表示從左端起3dip開始顯示此圖標。
④ 注:表示右端到什么位置結束
⑤ 注:可以是一個圖片。
⑥ 注:表示左端從什么位置開始。
⑦ 注:表示右端到什么位置結束。
一般適用于ExpandableListView的Adapter都要繼承BaseExpandableListAdapter這個類,并且必須重載getGroupView和getChildView這兩個最為重要的方法。
當擴展BaseExpandableListAdapter時,關鍵是實現如下四個方法:
public abstract ViewgetChildView (int groupPosition, intchildPosition, boolean isLastChild, ViewconvertView, ViewGroup parent)
取得顯示給定分組給定子位置的數據用的視圖.
參數
groupPosition 包含要取得子視圖的分組位置.
childPosition 分組中子視圖(要返回的視圖)的位置.
isLastChild 該視圖是否為組中的最后一個視圖.
convertView 如果可能,重用舊的視圖對象.使用前你應該保證視圖對象為非空,并且是否是合適的類型.如果該對象不能轉換為可以正確顯示數據的視圖,該方法就創建新視圖.不保證使用先前由getChildView(int,
int,boolean, View, ViewGroup)創建的視圖.
parent 該視圖最終從屬的父視圖.
返回
指定位置相應的子視圖.
public abstract intgetChildrenCount (int groupPosition)
取得指定分組的子元素數.
參數
groupPosition 要取得子元素個數的分組位置.
返回
指定分組的子元素個數.
public abstract ViewgetGroupView (int groupPosition, booleanisExpanded, View convertView, ViewGroupparent)
取得用于顯示給定分組的視圖.這個方法僅返回分組的視圖對象,要想獲取子元素的視圖對象,就需要調用getChildView(int, int, boolean, View, ViewGroup).
參數
groupPosition 決定返回哪個視圖的組位置.
isExpanded 該組是展開狀態還是收起狀態 .
convertView 如果可能,重用舊的視圖對象.使用前你應該保證視圖對象為非空,并且是否是合適的類型.如果該對象不能轉換為可以正確顯示數據的視圖,該方法就創建新視圖.不保證使用先前由getGroupView(int,
boolean,View, ViewGroup)創建的視圖.
parent 該視圖最終從屬的父視圖.
返回
指定位置相應的組視圖.
public abstract intgetGroupCount ()
取得分組數.
返回
分組數.
BaseExpandableListAdapter的重載的其它方法如下:
public abstract Object getChild(int groupPosition, int childPosition)
取得與指定分組、指定子項目關聯的數據.
參數
groupPosition 包含子視圖的分組的位置.
childPosition 指定的分組中的子視圖的位置.
返回
與子視圖關聯的數據.
public abstract long getChildId(int groupPosition, intchildPosition)
取得給定分組中給定子視圖的ID.該組ID必須在組中是唯一的.必須不同于其他所有ID(分組及子項目的ID).
參數
groupPosition 包含子視圖的分組的位置.
childPosition 要取得ID的指定的分組中的子視圖的位置.
返回
與子視圖關聯的ID.
public abstract longgetCombinedChildId (long groupId, long childId)
取得一覽中可以唯一識別子條目的ID(包括分組ID和子條目ID).可擴展列表要求每個條目(分組條目和子條目)具有一個可以唯一識別列表中子條目和分組條目的ID.該方法根據給定子條目ID和分組條目ID返回唯一識別ID.另外,如果hasStableIds()為真,該函數返回的ID必須是固定不變的.
參數
groupId 包含子條目ID的分組條目ID.
childId 子條目的ID.
返回
可以在所有分組條目和子條目中唯一識別該子條目的ID(可能是固定不變的).
public abstract longgetCombinedGroupId (long groupId)
取得一覽中可以唯一識別子條目的ID(包括分組ID和子條目ID).可擴展列表要求每個條目(分組條目和子條目)具有一個可以唯一識別列表中子條目和分組條目的ID.該方法根據給定子條目ID和分組條目ID返回唯一識別ID.另外,如果hasStableIds()為真,該函數返回的ID必須是固定不變的.
參數
groupId 分組條目ID.
返回
可以在所有分組條目和子條目中唯一識別該分組條目的ID(可能是固定不變的).
public abstract Object getGroup(int groupPosition)
取得與給定分組關聯的數據.
參數
groupPosition 分組的位置.
返回
指定分組的數據.
public abstract long getGroupId(int groupPosition)
取得指定分組的ID.該組ID必須在組中是唯一的.必須不同于其他所有ID(分組及子項目的ID).
參數
groupPosition 要取得ID的分組位置.
返回
與分組關聯的ID.
public abstract booleanhasStableIds ()
是否指定分組視圖及其子視圖的ID對應的后臺數據改變也會保持該ID.
返回
是否相同的ID總是指向同一個對象.
public abstract booleanisChildSelectable (int groupPosition, intchildPosition)
指定位置的子視圖是否可選擇.
參數
groupPosition 包含要取得子視圖的分組位置.
childPosition 分組中子視圖的位置.
返回
是否子視圖可選擇.
注意:
在XML布局文件中,如果ExpandableListView上一級視圖的大小沒有嚴格定義的話,則不能對ExpandableListView的android:layout_height屬性使用wrap_content值。(例如,如果上一級視圖是ScrollView的話,則不應該指定wrap_content的值,因為它可以是任意的長度。不過,如果ExpandableListView的上一級視圖有特定的大小的話,比如100像素,則可以使用wrap_content)
應用實例:
運行效果圖:

項目結構圖:

package com.jph.expandablelistviewdemo;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* Describe:</br>
* 可擴展的ListView</br>
* 本實例主要通過擴展BaseExpandableListAdapter來實現ExpandableListAdapter</br>
* 并通過ExpandableListAdapter為ExpandableListView設置數據適配器</br>
* 另外,本實例為ExpandableListView的子列表單擊事件設置監聽器*
* @author jph
* Date:2014.07.14
* */
public class ExpandableListViewDemo extends Activity {
ExpandableListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_list_view);
list=(ExpandableListView)findViewById(R.id.list);
//創建一個BaseExpandableListAdapter對象
final ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
//設置組視圖的圖片
int[] logos = new int[] { R.drawable.js, R.drawable.mfzw,R.drawable.yczw};
//設置組視圖的顯示文字
private String[] category = new String[] { "僵尸 ", "魔法植物", "遠程植物" };
//子視圖顯示文字
private String[][] subcategory = new String[][] {
{"旗幟僵尸", "鎧甲僵尸", "書生見識", "鐵桶僵尸", "尸娃僵尸","舞蹈僵尸" },
{ "黃金蘑菇", "貪睡蘑菇", "大頭蘑菇", "誘惑植物", "多嘴蘑菇","七彩蘑菇" },
{ "滿天星", "風車植物", "帶刺植物", "貪睡植物","雙子植物","膽怯蘑菇" }
};
//子視圖圖片
public int[][] sublogos = new int[][] {
{ R.drawable.js_1,R.drawable.js_2,R.drawable.js_3,
R.drawable.js_4,R.drawable.js_5,R.drawable.js_6},
{ R.drawable.mfzw_1,R.drawable.mfzw_2,R.drawable.mfzw_3,
R.drawable.mfzw_4,R.drawable.mfzw_5,R.drawable.mfzw_6},
{ R.drawable.yczw_1,R.drawable.yczw_2,R.drawable.yczw_3,
R.drawable.yczw_4,R.drawable.yczw_5,R.drawable.yczw_6 } };
//定義一個顯示文字信息的方法
TextView getTextView(){
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
TextView textView=new TextView(ExpandableListViewDemo.this);
//設置 textView控件的布局
textView.setLayoutParams(lp);
//設置該textView中的內容相對于textView的位置
textView.setGravity(Gravity.CENTER_VERTICAL);
//設置txtView的內邊距
textView.setPadding(36, 0, 0, 0);
//設置文本顏色
textView.setTextColor(Color.BLACK);
return textView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
//取得用于顯示給定分組的視圖. 這個方法僅返回分組的視圖對象
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//定義一個LinearLayout用于存放ImageView、TextView
LinearLayout ll=new LinearLayout(ExpandableListViewDemo.this);
//設置子控件的顯示方式為水平
ll.setOrientation(0);
//定義一個ImageView用于顯示列表圖片
ImageView logo=new ImageView(ExpandableListViewDemo.this);
logo.setPadding(50, 0, 0, 0);
//設置logo的大小(50(padding)+46=96)
AbsListView.LayoutParams lparParams=new AbsListView.LayoutParams(96,46);
logo.setLayoutParams(lparParams);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView=getTextView();
textView.setTextSize(20);
textView.setText(category[groupPosition]);
ll.addView(textView);
return ll;
}
//取得指定分組的ID.該組ID必須在組中是唯一的.必須不同于其他所有ID(分組及子項目的ID).
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
//取得分組數
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return category.length;
}
//取得與給定分組關聯的數據
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return category[groupPosition];
}
//取得指定分組的子元素數.
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return subcategory[groupPosition].length;
}
//取得顯示給定分組給定子位置的數據用的視圖
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//定義一個LinearLayout用于存放ImageView、TextView
LinearLayout ll=new LinearLayout(ExpandableListViewDemo.this);
//設置子控件的顯示方式為水平
ll.setOrientation(0);
//定義一個ImageView用于顯示列表圖片
ImageView logo=new ImageView(ExpandableListViewDemo.this);
logo.setPadding(0, 0, 0, 0);
//設置logo的大小
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(40, 40);
logo.setLayoutParams(lp);
logo.setImageResource(sublogos[groupPosition][childPosition]);
ll.addView(logo);
TextView textView=getTextView();
textView.setText(subcategory[groupPosition][childPosition]);
ll.addView(textView);
return ll;
}
//取得給定分組中給定子視圖的ID. 該組ID必須在組中是唯一的.必須不同于其他所有ID(分組及子項目的ID).
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return subcategory[groupPosition][childPosition];
}
};
list.setAdapter(adapter);
//為ExpandableListView的子列表單擊事件設置監聽器
list.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(ExpandableListViewDemo.this, "你單擊了:"
+adapter.getChild(groupPosition, childPosition), Toast.LENGTH_LONG).show();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.expandable_list_view, menu);
return true;
}
} </pre><a href="/misc/goto?guid=4959555536042813572" class="CopyToClipboard" title="copy"></a></div>
</div>
</div>
布局文件:
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
| | | | | |