在ListView中添加ExpandableListView的方法如下:
以下是一个简单的示例代码:
// 自定义适配器类
class MyAdapter extends BaseAdapter implements ExpandableListView.OnGroupClickListener, ExpandableListView.OnChildClickListener {
private List<String> groups; // Group数据源
private List<List<String>> children; // Child数据源
public MyAdapter(List<String> groups, List<List<String>> children) {
this.groups = groups;
this.children = children;
}
@Override
public int getCount() {
return groups.size();
}
@Override
public Object getItem(int position) {
return groups.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item, parent, false);
}
TextView textView = convertView.findViewById(R.id.textView);
ExpandableListView expandableListView = convertView.findViewById(R.id.expandableListView);
textView.setText(groups.get(position));
// 创建ExpandableListAdapter
ExpandableListAdapter adapter = new ExpandableListAdapter(children.get(position));
expandableListView.setAdapter(adapter);
expandableListView.setOnGroupClickListener(this);
expandableListView.setOnChildClickListener(this);
return convertView;
}
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
// 处理Group的点击事件
return false;
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
// 处理Child的点击事件
return false;
}
}
// 自定义ExpandableListAdapter类
class ExpandableListAdapter extends BaseExpandableListAdapter {
private List<String> children;
public ExpandableListAdapter(List<String> children) {
this.children = children;
}
@Override
public int getGroupCount() {
return 1;
}
@Override
public int getChildrenCount(int groupPosition) {
return children.size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_group, parent, false);
}
TextView textView = convertView.findViewById(R.id.textView);
textView.setText("Group");
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_child, parent, false);
}
TextView textView = convertView.findViewById(R.id.textView);
textView.setText(children.get(childPosition));
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
// 在Activity中使用
public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
// 创建数据源
List<String> groups = new ArrayList<>();
groups.add("Group 1");
groups.add("Group 2");
List<List<String>> children = new ArrayList<>();
List<String> group1Children = new ArrayList<>();
group1Children.add("Child 1-1");
group1Children.add("Child 1-2");
List<String> group2Children = new ArrayList<>();
group2Children.add("Child 2-1");
group2Children.add("Child 2-2");
children.add(group1Children);
children.add(group2Children);
// 创建适配器并设置给ListView
MyAdapter adapter = new MyAdapter(groups, children);
listView.setAdapter(adapter);
}
}
这是一个简单的示例,展示了如何在ListView中添加ExpandableListView。你可以根据实际需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云