更改为其他活动后,ExpandableListView消失

时间:2017-01-11 18:41:18

标签: android android-fragments android-viewpager expandablelistview

我正在使用带有两个标签的TabLayout的ViewPager。两个选项卡都包含一个带有ExpandableListView的布局。在第一次启动应用程序时,ExpandableListViews正确显示。但是如果我改为其他Fragment并返回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>

ExpandableListViews的适配器

 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());

1 个答案:

答案 0 :(得分:0)

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

错误的电话:

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

正确的电话:

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