您的位置:首页 > 移动开发 > Android开发

Android的ExpandableListView-android学习之旅(二十八)

2015-05-31 20:48 609 查看

ExpandableListView简介

ExpandableListView是ListView的子类,用法和ListView类似,ExpandableListView可以创建几个类别,每个类别下面又包括几个条目,实现了二级目录。

注意

因为他的数据组织是二级目录,所以数据提供者也是有特殊的。

必须是ExpandableListAdapter的子类。





ExpandableListView的属性



ExpandableListView的实例

package peng.liu.testview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
private ExpandableListView exList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
int[] imageIds = new int[]{
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
};
String[] listNames = new String[]{
"hello","java","world"
};
String[][] names = new String[][]{
{"hello","java","world","python"},
{"hello","java","world","python"},
{"hello","java","world"}
};
@Override
public int getGroupCount() {
return listNames.length;
}

@Override
public int getChildrenCount(int i) {
return names[i].length;
}

@Override
public Object getGroup(int i) {
return listNames[i];
}

@Override
public Object getChild(int i, int i2) {
return names[i][i2];
}

@Override
public long getGroupId(int i) {
return i;
}

@Override
public long getChildId(int i, int i2) {
return i2;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
LinearLayout layout = new LinearLayout(MainActivity.this);
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(imageIds[i]);
layout.addView(image);
TextView text = new TextView(MainActivity.this);
text.setText(getGroup(i).toString());
layout.addView(text);
return layout;
}
private TextView getText(){
AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
TextView text = new TextView(MainActivity.this);
text.setLayoutParams(params);
text.setTextSize(20);
text.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.LEFT);
text.setPadding(36,0,0,0);
return text;
};
@Override
public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
TextView text = getText();
text.setText(getChild(i,i2).toString());
return text;
}

@Override
public boolean isChildSelectable(int i, int i2) {
return true;
}
};
exList = (ExpandableListView) findViewById(R.id.exList);
exList.setAdapter(adapter);
}
}


布局代码

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<ExpandableListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/exList"
android:childDivider="@drawable/ic_launcher"/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: