2016-06-20 200 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); 
    } 

希望它能夠解決您的問題。