0

Я использую ViewPager для TabLayout с двумя вкладками. Обе вкладки содержат макет с расширениемListView. При первом запуске приложения ExpandableListViews отображается правильно. Но если я перейду на другой фрагмент и вернусь, ExpandableListViews исчезнет.ExpandableListView исчезает после перехода на другую активность

Я искал много, но ничего не нашел, кроме этого 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()); 

ответ

0

Я решил проблему сам. Если у кого-то есть такая же проблема, вот решение.

Неправильный вызов:

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

Правый вызов:

 CategoriesPagerAdapter adapter = new CategoriesPagerAdapter(getChildFragmentManager() 
      , 2, getActivity()); 
Смежные вопросы