java 展开收起列表组件_可展开的列表组件

packageorg.crazyit.helloworld;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Gravity;importandroid.view.Menu;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.AbsListView;importandroid.widget.BaseExpandableListAdapter;importandroid.widget.ExpandableListAdapter;importandroid.widget.ExpandableListView;importandroid.widget.ImageView;importandroid.widget.LinearLayout;importandroid.widget.TextView;public class ExpandableListViewTest extendsActivity {

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.expandable_list_view_test);//创建一个BaseExpandableListAdapter对象

ExpandableListAdapter adapter=newBaseExpandableListAdapter(){int[] logos=new int[]{

R.drawable.p,

R.drawable.z,

R.drawable.t

};private String[] armTypes=new String[]{"神族兵种","虫族兵种","人族兵种"};private String[][] arms=newString[][]{

{"狂战士","龙骑士","黑暗圣堂","电兵"},

{"小狗","刺蛇","飞龙","自爆飞机"},

{"机枪兵","护士MM","幽灵"}

};

@Overridepublic Object getChild(int groupPosition, intchildPosition) {//TODO Auto-generated method stub

returnarms[groupPosition][childPosition];

}

@Overridepublic long getChildId(int groupPosition, intchildPosition) {//TODO Auto-generated method stub

returnchildPosition;

}privateTextView getTextView(){

AbsListView.LayoutParams lp=newAbsListView.LayoutParams(

ViewGroup.LayoutParams.MATCH_PARENT,64);

TextView textView=new TextView(ExpandableListViewTest.this);

textView.setLayoutParams(lp);

textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);

textView.setPadding(70, 0, 0, 0);

textView.setTextSize(20);returntextView;

}//该方法决定每个子选项的外观

@Overridepublic View getChildView(int groupPosition, intchildPosition,booleanisLastChild, View convertView, ViewGroup parent) {//TODO Auto-generated method stub

TextView textView=getTextView();

textView.setText(getChild(groupPosition,childPosition).toString());returntextView;

}

@Overridepublic int getChildrenCount(intgroupPosition) {//TODO Auto-generated method stub

returnarms[groupPosition].length;

}

@Overridepublic Object getGroup(intgroupPosition) {//TODO Auto-generated method stub

returnarmTypes[groupPosition];

}

@Overridepublic intgetGroupCount() {//TODO Auto-generated method stub

returnarmTypes.length;

}

@Overridepublic long getGroupId(intgroupPosition) {//TODO Auto-generated method stub

returngroupPosition;

}//该方法决定每个组选项的外观

@Overridepublic View getGroupView(int groupPosition, booleanisExpanded,

View convertView, ViewGroup parent) {//TODO Auto-generated method stub

LinearLayout ll=new LinearLayout(ExpandableListViewTest.this);

ll.setOrientation(0);

ImageView logo=new ImageView(ExpandableListViewTest.this);

logo.setImageResource(logos[groupPosition]);

ll.addView(logo);

TextView textView=getTextView();

textView.setText(getGroup(groupPosition).toString());

ll.addView(textView);returnll;

}

@Overridepublic booleanhasStableIds() {//TODO Auto-generated method stub

return true;

}

@Overridepublic boolean isChildSelectable(intgroupPosition,intchildPosition) {//TODO Auto-generated method stub

return true;

}

};

ExpandableListView expandListView=(ExpandableListView)findViewById(R.id.list);

expandListView.setAdapter(adapter);

}

@Overridepublic booleanonCreateOptionsMenu(Menu menu) {//Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.expandable_list_view_test, menu);return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值