0

2つのタブを持つTabLayoutにViewPagerを使用しています。どちらのタブにも、ExpandableListViewを含むレイアウトが含まれています。アプリの最初の起動時に、ExpandableListViewsが正しく表示されます。しかし、私が他のFragmentに変更して、ExpandableListViewsが消えてしまうと、他のアクティビティに変更した後、ExpandableListViewの表示が消える

私は多くを検索しましたが、これ以外の類似点は見つかりませんでした。Link。 しかし、データバインディングを使用しているので、私はsetContentViewを使用しません。

ここは私の現在のコードの一部です。 最初のタブの

ViewPagerとフラグメント

​​

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; 
} 
} 

バインド2つ目のタブには、ちょうど別のリスト

同じコードが含まれています最初ExpandableListViewの
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); 
} 

レイアウト第2のレイアウトは同じコード

<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; 
} 

私が持っているので、私はuの人が役立つことを願って何をすべきかわからない。

更新日: エラーが見つかりました。これは上記のコードに含まれていませんでした。これは、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()); 
関連する問題