gpt4 book ai didi

android - 具有可变级别数的 ExpandableListView

转载 作者:行者123 更新时间:2023-11-29 01:25:59 25 4
gpt4 key购买 nike

我正在尝试创建一个具有可变数量级别的 ExpandableListView,如下所示。

http://i.imgur.com/ukkCq7B.png

我按照这个例子创建了一个只有一级子级的 ExpandableListView:

https://gist.github.com/bowmanb/4052030

代码:

public class SavedTabsFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.saved_tabs, null);
ExpandableListView elv = (ExpandableListView) v.findViewById(R.id.list);
elv.setAdapter(new SavedTabsListAdapter());
return v;
}

public class SavedTabsListAdapter extends BaseExpandableListAdapter {

private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };

private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};

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

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

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

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

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

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

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

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
TextView textView = new TextView(SavedTabsFragment.this.getActivity());
textView.setText(getGroup(i).toString());
return textView;
}

@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
TextView textView = new TextView(SavedTabsFragment.this.getActivity());
textView.setText(getChild(i, i1).toString());
return textView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}

}

}

如何创建具有可变级别数的 ExpandableListView? ExpandableListView 是最好的选择吗?

最佳答案

This post解释了 3 级可扩展列表。希望这会有所帮助。

基本思想是,一级列表由 ExpandableListAdapter 支持。此适配器生成的 subview 本身是 ExpandableListAdapters,它们提供二级组和最终的子元素。

关于android - 具有可变级别数的 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34058597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com