2016-06-20 201 views
2

我的ViewPager及其包含的片段有问题。我有2个菜单(A和B),我的主页直接是A.当我点击一个菜单B片段时,它包含一个TabLayout,其中包含一个ViewPager自己的三个片段(每个片段都包含一个带有Lorem ipsum的简单TextView)。 ViewPager的3个片段是正确的,但是如果我点击菜单A并且再次点击菜单B,我没有任何内容。片段1和片段2与第3片段没有任何内容,但是如果我返回到片段1读取收入(对于片段2,没有任何内容)。TabLayout - ViewPager - 片段

这是我的代码:

菜单B(FragmentTabLayout)

public class TabLayoutFragment extends Fragment { 
 

 
    private static final String ARG_TEXT = "ARG_TEXT"; 
 
    private static final String ARG_COLOR = "ARG_COLOR"; 
 

 
    private Toolbar toolbar; 
 
    private TabLayout tabLayout; 
 
    private ViewPager viewPager; 
 

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

 
    public static TabLayoutFragment newInstance(String text, int color) { 
 
     Bundle args = new Bundle(); 
 
     args.putString(ARG_TEXT, text); 
 
     args.putInt(ARG_COLOR, color); 
 

 
     TabLayoutFragment tabLayoutFragment = new TabLayoutFragment(); 
 
     tabLayoutFragment.setArguments(args); 
 

 
     return tabLayoutFragment; 
 
    } 
 

 

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

 
     setHasOptionsMenu(true); 
 
     // Inflate the layout for this fragment 
 
     View view = inflater.inflate(R.layout.fragment_tab_layout, container, false); 
 

 
     toolbar = (Toolbar) view.findViewById(R.id.toolbar); 
 
     ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar); 
 
     toolbar.setBackgroundColor(getArguments().getInt(ARG_COLOR)); 
 
     toolbar.setTitle(getArguments().getString(ARG_TEXT)); 
 

 
     viewPager = (ViewPager) view.findViewById(R.id.viewpager); 
 
     setupViewPager(viewPager); 
 

 

 
     tabLayout = (TabLayout) view.findViewById(R.id.tabs); 
 
     tabLayout.setupWithViewPager(viewPager); 
 
     tabLayout.setBackgroundColor(getArguments().getInt(ARG_COLOR)); 
 
     tabLayout.setSelectedTabIndicatorColor(Color.WHITE); 
 
     
 

 
     return view; 
 
    } 
 

 
    @Override 
 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
 
     inflater.inflate(R.menu.tab_menu, menu); 
 
     super.onCreateOptionsMenu(menu, inflater); 
 
    } 
 

 
    private void setupViewPager(ViewPager viewPager) { 
 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager()); 
 
     viewPager.setAdapter(adapter); 
 
    } 
 

 
    class ViewPagerAdapter extends FragmentPagerAdapter { 
 

 
     //private final List<TabFragment> mFragmentList = new ArrayList<>(); 
 
     //private final List<String> mFragmentTitleList = new ArrayList<>(); 
 

 
     public ViewPagerAdapter(FragmentManager manager) { 
 
      super(manager); 
 
     } 
 
     
 
     @Override 
 
     public Fragment getItem(int position) { 
 
      switch (position) { 
 
       case 0: 
 
        return TabFragment.newInstance("Fragment 2-1"); 
 
       case 1: 
 
        return TabFragment.newInstance("Fragment 2-2"); 
 
       case 2: 
 
        return TabFragment.newInstance("Fragment 2-3"); 
 
       default: 
 
        return TabFragment.newInstance("Fragment Default"); 
 
      } 
 
     } 
 

 
     @Override 
 
     public int getCount() { 
 
      //return mFragmentList.size(); 
 
      return 3; 
 
     } 
 
    } 
 
}

片段在ViewPager:

public class TabFragment extends Fragment { 
 

 
    private static final String ARG_TEXT = "ARG_TEXT"; 
 

 
    private TextView tv; 
 

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

 
    public static TabFragment newInstance(String text) { 
 
     Bundle args = new Bundle(); 
 
     args.putString(ARG_TEXT, text); 
 

 
     TabFragment tabFragment= new TabFragment(); 
 
     tabFragment.setArguments(args); 
 

 
     return tabFragment; 
 
    } 
 

 
    @Override 
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
 
          Bundle savedInstanceState) { 
 
     // Inflate the layout for this fragment 
 
     View view = inflater.inflate(R.layout.fragment_tab, container, false); 
 

 
     tv = (TextView) view.findViewById(R.id.tv); 
 

 
     return view; 
 
    } 
 

 
}

布局FragmentTabLayout:

<android.support.design.widget.CoordinatorLayout 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:orientation="vertical"> 
 

 
    <android.support.design.widget.AppBarLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
 

 
     <android.support.v7.widget.Toolbar 
 
      android:id="@+id/toolbar" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="?attr/actionBarSize" 
 
      android:background="?attr/colorPrimary" 
 
      app:layout_scrollFlags="scroll|enterAlways" 
 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
 

 
     <android.support.design.widget.TabLayout 
 
      android:id="@+id/tabs" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      app:tabMode="fixed" 
 
      app:tabGravity="fill"/> 
 
    </android.support.design.widget.AppBarLayout> 
 

 
    <android.support.v4.view.ViewPager 
 
     android:id="@+id/viewpager" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     /> 
 

 
</android.support.design.widget.CoordinatorLayout>

布局tabFragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:orientation="vertical" 
 
    tools:context="com.application.myapplication.TabFragment"> 
 

 
    <android.support.v4.widget.NestedScrollView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
 
     > 
 

 
    <TextView 
 
     android:id="@+id/tv" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:text="@string/lorem_ipsum" 
 
     /> 
 

 
    </android.support.v4.widget.NestedScrollView> 
 

 
</LinearLayout>

+0

看看http://www.gadgetsaint.com/android/create-viewpager-tabs-android/#.WPuppVN97BI – ASP

回答

1

你可以扩展你ViewPagerAdapter的

FragmentStatePagerAdapter 

代替

FragmentPagerAdapter 

我有同样的问题,它为我工作。

+0

Thx罗马为这个很好的答案,但我问自己我未来使用我的片段将包含一个很多数据(GridLayout)带有图片和文字。 – GJer

0

在我看来,更好的方法是你应该尝试使用childFragmentManager您嵌套片段

下面是简单的例子:

private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getChildFragmentManager()); 
     viewPager.setAdapter(adapter); 
    } 

希望它能够解决您的问题。