Android TabLayout ViewPager不在backstack上膨胀标签片段

时间:2015-11-10 08:18:14

标签: android android-fragments tabs android-tablayout

我已经设置了一个标签视图,它使用viewpager为每个标签设置了自定义片段。这是我的代码:

持有片段

public class FragInboxMainView extends Fragment implements CGFragment {
    private CGController controller;
    private CGFragment thisFragment;

    @Bind(R.id.inboxViewPager)ViewPager inboxViewPager;
    @Bind(R.id.inboxTabs)TabLayout inboxTabLayout;
    @Bind(R.id.inbox_progress_wheel)ProgressWheel inboxProgressWheel;

    public FragInboxMainView(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_inbox_mainview, container, false);
        ButterKnife.bind(this, rootView);
        thisFragment = this;
        Globals g = Globals.getInstance();
        /** Show loading spinner */
        this.inboxProgressWheel.setBarColor(ContextCompat.getColor(controller.getContext(), g.getUserObject().getUserThemeColor()));
        this.inboxProgressWheel.setVisibility(View.VISIBLE);
        /** Display the profile information based off the ID */
        controller.displayInbox(thisFragment);

        return rootView;
    }


    public void hideProgressSpinner() {
        this.inboxProgressWheel.setVisibility(View.GONE);
    }


    public ViewPager getInboxViewPager() {
        return this.inboxViewPager;
    }

    public TabLayout getInboxTabLayout() {
        return this.inboxTabLayout;
    }
}

其布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:wheel="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <android.support.design.widget.TabLayout
        android:id="@+id/inboxTabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable" />
    <com.pnikosis.materialishprogress.ProgressWheel
        android:id="@+id/inbox_progress_wheel"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        wheel:matProg_barColor="#5588FF"
        wheel:matProg_progressIndeterminate="true"
        android:visibility="gone"/>


    <android.support.v4.view.ViewPager
        android:id="@+id/inboxViewPager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:background="@android:color/white" />
</LinearLayout>

Tab片段及其通胀文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.baoyz.widget.PullRefreshLayout
        android:id="@+id/tabPullRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <view
        android:id="@+id/tabRecyclerHolder"
        class="android.support.v7.widget.RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:clipToPadding="false"
        android:layout_centerInParent="true"/>

    </com.baoyz.widget.PullRefreshLayout>
    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/tabFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@mipmap/ic_add_white"/>
</android.support.design.widget.CoordinatorLayout>

public class TabRecyclerHolder extends Fragment {

    @Bind(R.id.tabRecyclerHolder) RecyclerView tabRecyclerHolder;
    @Bind(R.id.tabPullRefresh) PullRefreshLayout tabPullRefresh;
    @Bind(R.id.tabFab) FloatingActionButton recyclerFab;

    private String tabTitle = "Title";


    public TabRecyclerHolder(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tab_recycler_holder, container, false);

        ButterKnife.bind(this, rootView);

        recyclerFab.hide(false);

        tabPullRefresh.setRefreshStyle(PullRefreshLayout.STYLE_MATERIAL);

        return rootView;
    }

    public RecyclerView getTabRecyclerHolder() {
        return this.tabRecyclerHolder;
    }

    public FloatingActionButton getRecyclerFab() {
        return this.recyclerFab;
    }

    public String getTabTitle() {
        return this.tabTitle;
    }

    public void setTabTitle(String title) {
        this.tabTitle = title;
    }

    public PullRefreshLayout getTabPullRefresh() {
        return this.tabPullRefresh;
    }
}

我的标签适配器

public class TabPagerAdapter extends FragmentStatePagerAdapter {

    private CGController controller;
    private List<Object> items;

    public TabPagerAdapter(FragmentManager fm, CGController controller, List<Object> items) {
        super(fm);
        this.controller = controller;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Fragment getItem(int num) {
        return (TabRecyclerHolder)items.get(num);
    }

    @Override
    public String getPageTitle(int num){
        return ((TabRecyclerHolder)items.get(num)).getTabTitle();
    }
}

处理代码

public void viewInbox() {
    /** Set up the views */
    receivedHolder = new TabRecyclerHolder();
    receivedHolder.setTabTitle(Constants.TAB_INBOX_RECEIVED);
    sentHolder = new TabRecyclerHolder();
    sentHolder.setTabTitle(Constants.TAB_INBOX_SENT);

    tabs.add(receivedHolder);
    tabs.add(sentHolder);

    /** Set up the tabs */
    final ViewPager inboxViewPager = inboxFragment.getInboxViewPager();
    TabLayout inboxTabLayout = inboxFragment.getInboxTabLayout();

    /** Set the adapter for the view pager */
    inboxViewPager.setAdapter(new TabPagerAdapter(inboxFragment.getChildFragmentManager(), controller, tabs));
    /** set up the tab look and feel */
    inboxTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    inboxTabLayout.setTabMode(TabLayout.MODE_FIXED);

    inboxViewPager.setOffscreenPageLimit(3);
    inboxViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(inboxTabLayout));
    inboxTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            inboxViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    /** And, display! */
    inboxTabLayout.setupWithViewPager(inboxViewPager);

    receivedAdapter = new RecyclerListAdapter(controller, items);
    final RecyclerView receivedList = receivedHolder.getTabRecyclerHolder();
    receivedList.setLayoutManager(new LinearLayoutManager(controller.getContext()));
    receivedList.setAdapter(receivedAdapter);
}

我错过了一些代码,但它与问题无关。 最初查看片段时,代码可以正常工作。但是,由于我的应用程序包含单个活动,并且只是替换导航到的每个片段的内容视图,因此每个片段都会添加到后端堆栈,然后在按下后退按钮时弹出。我的问题是,当导航回到此片段时,选项卡内的视图不会被充气,这意味着无法访问任何元素(因此在尝试在recyclerview中显示数据时应用程序崩溃等)。

我看过这个问题:TabLayout ViewPager Not Loading When Using Backstack并实施了它的建议(在设置寻呼机适配器时使用getChildFragmentManager()),但这并没有解决我的问题。

帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

将此$provide.value更改为public View onCreateView(LayoutInflater inflater,...

所以你会有类似的东西

public void onViewCreated (View view, Bundle savedInstanceState)

然后

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {        
    return inflater.inflate(R.layout.tab_recycler_holder, container, false);
}

看看是否有帮助

答案 1 :(得分:0)

在TabPagerAdapter中将FragmentStatePagerAdapter扩展为

FragmentStatePagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)