【问题标题】:ExpandableListView dissapears after changing to other activityExpandableListView 更改为其他活动后消失
【发布时间】:2017-01-11 18:41:17
【问题描述】:

我正在为带有两个选项卡的 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());

【问题讨论】:

    标签: android android-fragments android-viewpager expandablelistview


    【解决方案1】:

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

    打错电话:

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

    正确的调用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2015-08-02
      • 1970-01-01
      • 2016-09-25
      • 2019-04-05
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多