您的位置:首页 > 其它

ExpandableListView使用详解

2016-01-03 15:54 302 查看

ExpandableListView使用详解

ONE Goal,ONE Passion !

中午一觉醒来看到群里有人问一个效果是如何做出来的,我看了来看,就是可以展开列表像的listView嘛.

已经给那为童鞋写了个简单demo希望对他有所帮助.

其他类似方法实现请点击:

/article/8728090.html

第1步: 使用在xml中使用ExpandableListView.

a. ExpandableListView额外支持的一些常用xml属性:

xml属性说明
android:childDivider指定各组内各个列表项之间的分割条
android:childIndicator显示在子列表旁边的Drawable对象
android:groupIndicator显示在组列表旁边的Drawable对象
b. 和listView的使用方法一致. 如: activity_expandable.xml文件

<?xml version="1.0" encoding="utf-8"?>
<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"

tools:context="com.example.windowtest1.ExpandableActivity">

<ExpandableListView
android:id="@+id/list"

android:layout_width="match_parent"
android:layout_height="match_parent"></ExpandableListView>
</RelativeLayout>


第2步:使用baseExpandableListAdapter填充数据

public class ExpandableActivity extends Activity {

ExpandableListView lv;
ExpandableActivity act;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable);
act = this;

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

ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

String[] types = new String[]{"样式", "图案", "适合人群", "克重"};

String[][] items = new String[][]
{
{"吊坠", "项链"},
{"生肖", "卡通"},
{"全部", "男", "女", "儿童"},
{"1-2克", "2-5克"}
};

// 获得指定位置,指定子列表项数据
@Override
public Object getChild(int groupPosition, int childPosition) {

return items[groupPosition][childPosition];
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public int getChildrenCount(int groupPosition) {
return types[groupPosition].length();
}

// 该方法决定每个子选项的外观
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

TextView tv = new TextView(act);
tv.setPadding(20, 0, 0, 0);
tv.setText(getChild(groupPosition, childPosition).toString());

return tv;
}

// 获得指定位置出的数据
@Override
public Object getGroup(int groupPosition) {
return types[groupPosition];
}

@Override
public int getGroupCount() {
return types.length;
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

TextView tv = new TextView(act);
tv.setTextSize(40);
tv.setText(getGroup(groupPosition).toString());

return tv;
}

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

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
//打印日志看看选择的具体某一项
System.out.println("groupPosition" + types[groupPosition] + "----" + "childPosition" + items[groupPosition][childPosition]);
return false;
}
};
lv.setAdapter(adapter);
}
}


代码比较简单就不在解释了.看效果就可以了.



咦…有没有发现,我用红色线圈住的地方有一个向上的箭头.其实那是ExpandableListView 自带的,看起来很不爽啊. 当然我们可以选择去隐藏或者换成其他图标; 这就用到了上面提到的属性:

我们只需在xml中加上这句话就ok了.

android:groupIndicator="@null"


再来看效果: 已经没有了那个图标.

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