ExpandableListView二级列表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ExpandableListView二级列表相关的知识,希望对你有一定的参考价值。
package com.example.dajj;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ExpandableListView ev;
String[] name = new String[] { "你好", "我好", "dajiahao", "他好" };
String[][] names = new String[][] { { "他后", "爱的", "爱的" },
{ "他后", "爱的", "爱的" }, { "他后", "爱的", "爱的" }, { "他后", "爱的", "爱的" } };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ev = (ExpandableListView) findViewById(R.id.ev);
ev.setAdapter(new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = View.inflate(MainActivity.this,
android.R.layout.simple_list_item_1, null);
TextView text = (TextView) view
.findViewById(android.R.id.text1);
text.setText(name[groupPosition]);
return view;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return name.length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return names[groupPosition].length;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = View.inflate(MainActivity.this,
android.R.layout.simple_list_item_1, null);
TextView text = (TextView) view
.findViewById(android.R.id.text1);
text.setText(names[groupPosition][childPosition]);
return view;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return arg1;
}
});
}
}
以上是关于ExpandableListView二级列表的主要内容,如果未能解决你的问题,请参考以下文章
Kotlin ExpandableListView可扩展二级列表
(转载)自定义ExpandableListView,实现二级列表效果