我有一个带有视图寻呼机的活动,其中包含5个片段。在每个片段的布局中,我需要一个可扩展的列表视图。当在每个片段中使用普通列表视图时,应用程序运行正常,但是当使用可扩展列表视图时,应用程序崩溃。如何在片段内使用可扩展列表视图?
答案 0 :(得分:0)
你能提供你的崩溃日志吗?没有记录,没有解决方案...
以下代码不会崩溃。 Fragment
内的Activity
,fragment
的根视图为ExpandableListView
。也许它可以帮助你。
private Map<String, List<String>> dataset = new HashMap<>();
private String[] parentList = new String[]{"first", "second", "third"};
private List<String> childrenList1 = new ArrayList<>();
private List<String> childrenList2 = new ArrayList<>();
private List<String> childrenList3 = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_expandable_listview, container, false);
}
private void initialData() {
childrenList1.add(parentList[0] + "-" + "first");
childrenList1.add(parentList[0] + "-" + "second");
childrenList1.add(parentList[0] + "-" + "third");
childrenList2.add(parentList[1] + "-" + "first");
childrenList2.add(parentList[1] + "-" + "second");
childrenList2.add(parentList[1] + "-" + "third");
childrenList3.add(parentList[2] + "-" + "first");
childrenList3.add(parentList[2] + "-" + "second");
childrenList3.add(parentList[2] + "-" + "third");
dataset.put(parentList[0], childrenList1);
dataset.put(parentList[1], childrenList2);
dataset.put(parentList[2], childrenList3);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initialData();
ExpandableListView expandableListView = (ExpandableListView) view;
expandableListView.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseExpandableListAdapter {
@Override
public int getGroupCount() {
return dataset.size();
}
@Override
public int getChildrenCount(int i) {
return dataset.get(parentList[i]).size();
}
@Override
public Object getGroup(int i) {
return dataset.get(parentList[i]);
}
@Override
public Object getChild(int i, int i1) {
return dataset.get(parentList[i]).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = View.inflate(getActivity(), R.layout.parent_item, null);
}
view.setTag(R.layout.parent_item, i);
view.setTag(R.layout.child_item, -1);
TextView text = (TextView) view.findViewById(R.id.parent_title);
text.setText(parentList[i]);
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = View.inflate(getActivity(), R.layout.child_item, null);
}
view.setTag(R.layout.parent_item, i);
view.setTag(R.layout.child_item, i1);
TextView text = view.findViewById(R.id.child_title);
text.setText(dataset.get(parentList[i]).get(i1));
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "点到了内置的textview", Toast.LENGTH_SHORT).show();
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
}