expandable adapterlistadapter怎么设置其他样式的item

android 中的 BaseExpandableListAdapter 类可不可以做三层视图,请问如何写 ?_百度知道android开发expandablelistview中怎么让子item默认展示_百度知道ExpandableListView怎么实现不同的布局?
[问题点数:20分,结帖人gu330]
ExpandableListView怎么实现不同的布局?
[问题点数:20分,结帖人gu330]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。下次自動登錄
現在的位置:
& 綜合 & 正文
BaseExpandableListAdapter的用法
&?xml version="1.0" encoding="utf-8"?&&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &
&ExpandableListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ecpandable"
/&&/LinearLayout&
package cn.csdn.import android.app.Aimport android.os.Bimport android.view.Vimport android.view.ViewGimport android.widget.AbsListVimport android.widget.BaseExpandableListAimport android.widget.ExpandableListAimport android.widget.ExpandableListVimport android.widget.ImageVimport android.widget.LinearLimport android.widget.TextVpublic class ExpandableListViewActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_layout);
/**BaseExpandableListAdapter實現了ExpandableListAdapter*/
ExpandableListAdapter adapter = new BaseExpandableListAdapter(){/**----------定義數組-------------------------------------------------------------------*/
private int[] images = new int[]{
R.drawable.ic_launcher,
R.drawable.stop,
R.drawable.play
private String[] armTypes = new String[]{
"神族","蟲族","人族"
private String[][] arms = new String[][]{
{"狂戰士","龍騎士","黑暗聖堂"},
{"小狗","飛龍","自爆妃子"},
{"步兵","傘兵","護士mm"}
/*===========組元素表示可摺疊的列表項,子元素表示列表項展開後看到的多個子元素項=============*//**----------得到armTypes和arms中每一個元素的ID-------------------------------------------*/
//獲取組在給定的位置編號,即armTypes中元素的ID
public long getGroupId(int groupPosition) {
return groupP
//獲取在給定的組的兒童的ID,就是arms中元素的ID
public long getChildId(int groupPosition, int childPosition) {
return childP
/**----------根據上面得到的ID的值,來得到armTypes和arms中元素的個數 ------------------------*/
//獲取的群體數量,得到armTypes里元素的個數
public int getGroupCount() {
return armTypes.
//取得指定組中的兒童人數,就是armTypes中每一個種族它軍種的個數
public int getChildrenCount(int groupPosition) {
return arms[groupPosition].
/**----------利用上面getGroupId得到ID,從而根據ID得到armTypes中的數據,並填到TextView中 -----*/
//獲取與給定的組相關的數據,得到數組armTypes中元素的數據
public Object getGroup(int groupPosition) {
return armTypes[groupPosition];
//獲取一個視圖顯示給定組,存放armTypes
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getTextView();//調用定義的getTextView()方法
textView.setText(getGroup(groupPosition).toString());//添加數據
return textV
}/**----------利用上面getChildId得到ID,從而根據ID得到arms中的數據,並填到TextView中---------*/
//獲取與孩子在給定的組相關的數據,得到數組arms中元素的數據
public Object getChild(int groupPosition, int childPosition) {
return arms[groupPosition][childPosition];
//獲取一個視圖顯示在給定的組 的兒童的數據,就是存放arms
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(ExpandableListViewActivity.this);
ll.setOrientation(0);//定義為縱向排列
ImageView logo = new ImageView(ExpandableListViewActivity.this);
logo.setImageResource(images[groupPosition]);//添加圖片
ll.addView(logo);
TextView textView = getTextView();//調用定義的getTextView()方法
textView.setText(getChild(groupPosition,childPosition).toString());//添加數據
ll.addView(textView);
/**------------------自定義一個設定TextView屬性的方法----------------------------------------------*/
//定義一個TextView
private TextView getTextView(){
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,40);
TextView textView = new TextView(ExpandableListViewActivity.this);
textView.setLayoutParams(lp);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textV
/**-------------------其他設置-------------------------------------------------------------------*/
//孩子在指定的位置是可選的,即:arms中的元素是可點擊的
public boolean isChildSelectable(int groupPosition,
int childPosition) {
return true;
//表示孩子是否和組ID是跨基礎數據的更改穩定
public boolean hasStableIds() {
return true;
/**使用適配器*/
ExpandableListView expandListView = (ExpandableListView) this.findViewById(R.id.ecpandable);
expandListView.setAdapter(adapter);
&&&&推薦文章:
【上篇】【下篇】15330人阅读
Tech-Android-View(10)
1、更换ExpandableListView右边的箭头(小图标)的疑问
&&&&&&第一个方法: 网上许多说到在ExpandableListView的布局xml中的groupIndicator更换成@drawable/***,我试过,可行,代码如下:
首先在drawable的文件夹下建立自定义选择布局属性的xml文件:ex_smallimage.xml
&span style=&color:#666666;background-color: rgb(255, 255, 255);&&&?xml version=&1.0& encoding=&UTF-8&?&
&selector xmlns:android=&/apk/res/android&&
&item android:state_expanded=&false&
android:drawingCacheQuality=&auto&
android:drawable=&@drawable/la&/&
这里la是下拉图标
&item android:state_expanded=&true&
android:drawingCacheQuality=&auto&
android:drawable=&@drawable/shou&/& 这里shou是收回图标
&/selector&&/span&
然后在ExpandableListView的groupIndicator设为: & & & & & & & & & & &&android:groupIndicator=&@drawable/ex_smallimage&
这样做了可行,但是有个问题,就是图标总是随group的大小而拉伸,不会缩放:
那么我调了xml的group代码之后,可以勉强达到自己要求的效果,但是这样的代码没有弹性,换张图片还得改布局。。。如果有知道如何设置图标的大小告诉我咯~呵呵。。
方法二:经过上面的尝试,我禁掉了groupIndicator属性,然后再在group的xml中加入ImageView,这样可以控制我的图片的缩放和位置的更改,代码:
android:groupIndicator=&@null&这里非常重要,禁用系统自带的图标
再在group的xml中设置ImageView,这个简单,然后在适配器的getGroupView中加入图片变换的效果:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView mgroupimage=(ImageView)convertView.findViewById(R.id.mGroupimage);
mgroupimage.setImageBitmap(mla);
if(!isExpanded){
mgroupimage.setImageBitmap(mshou);
OK,搞定它:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:184803次
积分:2639
积分:2639
排名:第9769名
原创:72篇
转载:20篇
评论:45条
(1)(3)(4)(5)(4)(1)(3)(5)(1)(9)(2)(1)(2)(4)(3)(1)(5)(6)(7)(7)(13)(1)(5)}

我要回帖

更多关于 expandablelistview 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信