使用带有TabLayout的ViewPager时出现意外行为

时间:2015-11-06 12:40:22

标签: android android-fragments android-viewpager

各位大家好

我有一个带有TabLayout的片段到标签,我实现了PagerAdapter,一切正常,问题是当我转到其他选项之后我再次点击选项TabLayout出现但是空的,我会与您分享我为此功能编写的代码:

适配器:

public class DevicePagerAdapter extends FragmentPagerAdapter {

    public DevicePagerAdapter(FragmentManager fm){
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:{
                return new fragmentDeviceCategory();
            }
            case 1:{
                return new fragmentDeviceRoom();
            }
            default:{
                return null;
            }
        }
    }

    @Override
    public int getCount() {
        return 2;
    }
}

TabLayout中其中一个片段的代码:

 public class fragmentDeviceList extends Fragment {

    protected ViewPager viewPager;

    public fragmentDeviceList() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setTitle(getResources().getString(R.string.title_devices_list));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_device_list, container, false);
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_category)).setIcon(getResources().getDrawable(R.drawable.ic_category_black_36dp, getActivity().getTheme())));
        } else {
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_category)).setIcon(getResources().getDrawable(R.drawable.ic_category_black_36dp)));
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_room)).setIcon(getResources().getDrawable(R.drawable.ic_room_black_36dp, getActivity().getTheme())));
        } else {
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_room)).setIcon(getResources().getDrawable(R.drawable.ic_room_black_36dp)));
        }
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        viewPager = (ViewPager) view.findViewById(R.id.pager);
        DevicePagerAdapter adapter = new DevicePagerAdapter(getFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });
        return view;
    }
}

这是我在托管TabLayout的主要Fragment中使用的代码:

  public class fragmentDeviceList extends Fragment {

    protected ViewPager viewPager;

    public fragmentDeviceList() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setTitle(getResources().getString(R.string.title_devices_list));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_device_list, container, false);
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_category)).setIcon(getResources().getDrawable(R.drawable.ic_category_black_36dp, getActivity().getTheme())));
        } else {
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_category)).setIcon(getResources().getDrawable(R.drawable.ic_category_black_36dp)));
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_room)).setIcon(getResources().getDrawable(R.drawable.ic_room_black_36dp, getActivity().getTheme())));
        } else {
            tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_room)).setIcon(getResources().getDrawable(R.drawable.ic_room_black_36dp)));
        }
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        viewPager = (ViewPager) view.findViewById(R.id.pager);
        DevicePagerAdapter adapter = new DevicePagerAdapter(getFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });
        return view;
    }
}

注意:当我调试代码时,我注意到`fragmentDeviceList`中的方法`onCreateView`(它承载TabLayout的片段)它被执行,但片段`fragmentDeviceCategory`的方法`onCreateView`是没有解雇。

2 个答案:

答案 0 :(得分:0)

我得到了问题的解决方案,解决方案很简单,我认为我可以用作ViewPage FragmentPagerAdapter 的适配器,因为我想只显示片段,但这个类仍然是状态片段,所以嵌套片段没有再次初始化,所以我将我的适配器更改为 FragmentStatePagerAdapter ,现在工作正常。

谢谢大家

答案 1 :(得分:0)

要使用Fragment中的标签,您应该使用ChildFragmentManager

该行:

DevicePagerAdapter adapter = new DevicePagerAdapter(getFragmentManager());

应该是:

DevicePagerAdapter adapter = new DevicePagerAdapter(getChildFragmentManager());

FragmentPagerAdapter仅对两个片段有效,因为它将它们保存在内存中。 FragmentStatePagerAdapter适用于大量碎片。

同样TabLayout有一个方法setupWithViewPager,它将: 这种方法将:

  • 添加一个ViewPager.OnPageChangeListener,它将事件转发到此TabLayout。
  • 从ViewPager的PagerAdapter填充TabLayout的标签页。
  • 设置我们的TabLayout.OnTabSelectedListener,它会将所选事件转发给ViewPager

您可以使用它而不是手动添加标签。