ExpandableListView 在更改为其他 activity 后消失

ExpandableListView dissapears after changing to other activity

我正在为带有两个选项卡的 TabLayout 使用 ViewPager。这两个选项卡都包含一个带有 ExpandableListView 的布局。首次启动应用程序时,ExpandableListViews 会正确显示。但是,如果我更改为其他 Fragment 并且 return ExpandableListViews 消失了。

我搜索了很多,但除了这个 Link 没有发现任何类似的东西。 但是由于我使用的是数据绑定,所以我不使用 setContentView。

这是我当前代码的一部分。

带有 ViewPager 的片段

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <!-- TabLayout with two Tabs  -->
    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:tabMode="fixed"
        />

    <!-- ViewPager for swipe -->
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tabLayout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</RelativeLayout>

ViewPager 的适配器

   //Constructor to the class
public CategoriesPagerAdapter(FragmentManager fm, int tabCount, Context context) {
    super(fm);
    //Initializing tab count
    this.tabCount = tabCount;
    this.context = context;
}

@Override
public Fragment getItem(int position) {
    //Returning the current tabs
    switch (position) {
        case 0:
            return new Tab_Income();
        case 1:
            return new Tab_Expense();
        default:
            return new Tab_Income();
    }
}

@Override
public int getCount() {
    return tabCount;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return context.getResources().getString(R.string.income_categories);
        case 1:
            return     context.getResources().getString(R.string.expense_categories);
    }
    return null;
}
}

绑定第一个选项卡 第二个选项卡包含相同的代码只是另一个列表

private FragmentIncomeCategoriesBinding binding;
CategoriesAdapter expandableListAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_income_categories, container, false);
    expandableListAdapter = new CategoriesAdapter(getActivity(), GlobalLists.getInstance().getIncomeCategoriesMap());
    binding.expandableListViewIncome.setAdapter(expandableListAdapter);
    return binding.getRoot();
}
@Override
public void onResume() {
    super.onResume();
    expandableListAdapter = new CategoriesAdapter(getActivity(), GlobalLists.getInstance().getIncomeCategoriesMap());
    binding.expandableListViewIncome.setAdapter(expandableListAdapter);
}

第一个ExpandableListView的布局 第二个布局是相同的代码

<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<ExpandableListView
    android:id="@+id/expandableListView_expense"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="0.5dp" />
</layout>

两个 ExpandableListView 的适配器

 private Context context;
ArrayMap<Category, List<Category>> categoryMap ;

public CategoriesAdapter(Context context, ArrayMap<Category, List<Category>> categoryMap) {
    this.context = context;
    this.categoryMap = categoryMap;
}

@Override
public int getGroupCount() {
    return categoryMap.size();
}

@Override
public int getChildrenCount(int listPosition) {
    return categoryMap.valueAt(listPosition).size();
}

@Override
public Object getGroup(int listPosition) {
    return categoryMap.valueAt(listPosition);
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
    return categoryMap.valueAt(listPosition).get(expandedListPosition);
}

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

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return categoryMap.valueAt(listPosition).get(expandedListPosition).getId();
}

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

@Override
public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    CategoryListParentBinding parentBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.getContext()),
            R.layout.category_list_parent, parent, false);
    parentBinding.setParentCategory(categoryMap.keyAt(listPosition));
    return parentBinding.getRoot();
}

@Override
public View getChildView(int listPosition, int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    CategoryListChildBinding childBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.getContext()),
            R.layout.category_list_child, parent, false);
        childBinding.setChildCategory(categoryMap.valueAt(listPosition).get(expandedListPosition));
    return childBinding.getRoot();
}

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

希望大家能帮忙,因为我不知道该怎么做。

更新: 我发现了错误,它不在上面的代码中。这是启动包含 ViewPager 的片段的方式。

错误调用:

        CategoriesPagerAdapter adapter = new CategoriesPagerAdapter(getActivity().getFragmentManager()
            , 2, getActivity());

正确的调用:

        CategoriesPagerAdapter adapter = new CategoriesPagerAdapter(getChildFragmentManager()
            , 2, getActivity());

我自己解决了这个问题。如果有人遇到同样的问题,这里就是解决方案。

错误调用:

        CategoriesPagerAdapter adapter = new CategoriesPagerAdapter(getActivity().getFragmentManager()
            , 2, getActivity());

正确调用:

        CategoriesPagerAdapter adapter = new CategoriesPagerAdapter(getChildFragmentManager()
            , 2, getActivity());