您的位置:首页 > 其它

ExpandableListView--可展开的列表组件

2016-12-08 16:41 447 查看
为培养手感,最近在前端学习期间,总会学习疯狂Android讲义上的小Demo,用来培养Android手感,以免手生,今天联系ExpandableListView可展开的列表组件,这种效果可以说到处可见了,咋感觉这种效果会有点复杂,,不过只要理清思路,这是相当简单的.下面就讲解一下了:

ExpandableListView是ListView的子类,他可以说是在普通的ListView的基础上进行扩展的,他把应用中的列表项分为几组,每组里又可包含多个列表项.所以说ExpandableListView用法和ListView的用法非常相似,只是ExpandableListView所显示的列表项应该有ExpandableListAdapter提供.ExpandableListAdapter也是一个接口.它和Adapter也是类似的,实现ExpandableListAdapter有3种常用方式.

(1)扩展BaseExpandableListAdapter实现ExpandableListAdapter.

(2)使用SimpleExpandableListAdapter将两个List集合包装成ExpandableListAdapter

(3)使用SimpleCursorTreeAdapter将Cursor中的数据包装成ExpandableListAdapter.

下面就是通过自定义ExpandableListAdapter为ExpandableListView提供列表项,那么先看该Demo的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ExpandableListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

上面布局就相当简单了,就是在LinearLayout中定义了一个ExpandableListView,下面就是看在程序内怎么进行设置父组件和子组件的,如下:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建一个BaseExpandableListAdapter对象
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

int[] logos = new int[]{
R.drawable.p,
R.drawable.z,
R.drawable.t
};
private String[] armTypes = new String[]{
"神族","虫族","人族"
};
private String[][] arms = new String[][]{
{"狂战士","龙骑士","黑暗圣堂","电兵"},
{"小狗","刺蛇","飞龙","自爆飞机"},
{"机枪兵","护士MM","幽灵"}
};
//获取指定组位置,指定子列表项出的子列表项数据
@Override
public Object getChild(int i, int i1) {
return arms[i][i1];
}

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

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

private TextView getTextView(){
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36,0,0,0);
textView.setTextSize(20);
return textView;
}
//该方法决定每个子选项的外观
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
TextView textView = getTextView();
textView.setText(getChild(i,i1).toString());
return textView;
}
//获取指定组位置处的组数据
@Override
public Object getGroup(int i) {
return armTypes[i];
}

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

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

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(0);
ImageView iv = new ImageView(MainActivity.this);
iv.setImageResource(logos[i]);
ll.addView(iv);
TextView tv = getTextView();
tv.setText(getGroup(i).toString());
ll.addView(tv);
return ll;
}

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

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

看着上面的代码可以看到分别获取组数据,和子数据.,在设置adapter时,我们就是扩展BaseExpandableListAdapter来实现ExpandableListAdapter.在扩展时,关键就是实现4个方法:(1)getGroupCount():该方法返回包含的组列表项的数量.

(2)getGroupView():该方法返回的View对象将作为组列表项

(3)getChildrenCount():该方法返回特定组所包含的子列表项的数量.

(4)getChildView():该方法返回的View对象将作为特定组,特定位置的子列表项.

一切完成了,效果图看一下吧:

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