BaseExpandableListAdapter的用法
来源:互联网 发布:java 编码url 空格 20 编辑:程序博客网 时间:2024/04/30 00:39
MainActivity.java
package com.example.listviewdemo;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.app.ExpandableListActivity;import android.os.Bundle;import android.view.Gravity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.ListAdapter;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { private List<String> groupdate;//定义组 private List<List<String>> childrendate;//定义组中的子条目 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ExpandableListView expandable = (ExpandableListView)findViewById(R.id.expandableListView1); groupdate = new ArrayList<String>(); groupdate.add("Java"); groupdate.add("Android"); groupdate.add("IOS"); childrendate = new ArrayList<List<String>>(); List<String> child1 = new ArrayList<String>(); child1.add("java基础"); child1.add("java大全"); child1.add("解析java"); childrendate.add(child1); List<String> child2 = new ArrayList<String>(); child2.add("Android基础"); child2.add("Android大全"); child2.add("解析Android"); childrendate.add(child2); List<String> child3 = new ArrayList<String>(); child3.add("IOS基础"); child3.add("IOS大全"); child3.add("解析IOS"); childrendate.add(child3); expandable.setAdapter(new ExpandableAdapter()); /*expandable.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你点击了"+groupPosition+","+ childPosition,Toast.LENGTH_SHORT).show(); return false; } });*/ }class ExpandableAdapter extends BaseExpandableListAdapter { @Override public Object getChild(int groupPosition, int childPosition) { return childrendate.get(groupPosition).get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView myText = null; if (convertView != null) { myText = (TextView) convertView; myText.setText(childrendate.get(groupPosition).get( childPosition)); } else { AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 50); myText = new TextView(MainActivity.this); myText.setLayoutParams(layoutParams); myText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); myText.setPadding(50, 0, 0, 0); myText.setText(childrendate.get(groupPosition).get( childPosition)); } return myText; } @Override public int getChildrenCount(int groupPosition) { return childrendate.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return childrendate.get(groupPosition); } @Override public int getGroupCount() { return childrendate.size(); } @Override public long getGroupId(int groupPosition) { return 0; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView myText = null; if (convertView != null) { myText = (TextView) convertView; myText.setText(groupdate.get(groupPosition)); } else { myText = new TextView(MainActivity.this); AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 50); myText.setLayoutParams(layoutParams); myText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); myText.setPadding(50, 0, 0, 0); myText.setText(groupdate.get(groupPosition)); } return myText; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.listviewdemo.MainActivity" > <ExpandableListView android:id="@+id/expandableListView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" > </ExpandableListView></RelativeLayout>
0 0
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- BaseExpandableListAdapter的用法
- Android之BaseExpandableListAdapter的用法
- Android成长之路-BaseExpandableListAdapter的用法
- Android成长之路-BaseExpandableListAdapter的用法
- ExpandableListView、BaseExpandableListAdapter用法
- android之expandableListView+BaseExpandableListAdapter+ArrayList伸缩的用法:
- BaseExpandableListAdapter的详细说明
- BaseExpandableListAdapter
- BaseExpandableListAdapter
- BaseExpandableListAdapter
- Android开发:BaseExpandableListAdapter的使用
- 推荐一个手把手叫学greendao的好教程
- 技术分享-2015.7.8编
- Got fatal error 1236 from master when reading data from binary log
- 访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”
- 排序算法!~
- BaseExpandableListAdapter的用法
- 黑马程序员————第十八天
- 【MapReduce】常用计算模型详解
- qt 正则表达式
- [安卓]新闻客户端(四) 主页面之slidingMenu & fragment(2)
- Android 自定义View 之 format 详解
- android4.4.+状态栏改变颜色
- UGUI 实现界面 渐隐渐现 FadeIn/Out 效果
- 黑马程序员——Java基础---GUi(第22天)