4

我想添加一個viewpager到一個使用新的android支持設計庫的片段。我有我的XML代碼的佈局如下:片段內的ViewPager

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

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/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/tabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

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

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

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

而且相應的Java類如下:

public class Tracks extends Fragment { 

    ViewPager viewPager; 
    TabLayout tabLayout; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.tracks, container, false); 

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

     AppCompatActivity activity = (AppCompatActivity) getActivity(); 
     assert activity.getSupportActionBar() != null; 
     activity.getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.app_theme))); 

     return view; 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     setupViewPager(viewPager); 
     tabLayout.setupWithViewPager(viewPager); 

     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 

      } 

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

      } 

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

      } 
     }); 
    } 

    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager()); 
     viewPagerAdapter.addFragment(new TopTracks(), "Top Tracks"); 
     viewPagerAdapter.addFragment(new WorldCharts(), "World Charts"); 
     viewPagerAdapter.addFragment(new NewMusic(), "New Music"); 
     viewPagerAdapter.addFragment(new AfricaHot(), "Africa Hot"); 
     viewPagerAdapter.addFragment(new Playlists(), "Playlists"); 
     viewPagerAdapter.addFragment(new Recommended(), "Recommended"); 
     viewPager.setAdapter(viewPagerAdapter); 
    } 

    private class ViewPagerAdapter extends FragmentPagerAdapter { 

     List<Fragment> fragmentList = new ArrayList<>(); 
     List<String> fragmentTitles = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager fragmentManager) { 
      super(fragmentManager); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return fragmentList.get(position); 
     } 

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

     @Override 
     public CharSequence getPageTitle(int position) { 
      return fragmentTitles.get(position); 
     } 

     public void addFragment(Fragment fragment, String name) { 
      fragmentList.add(fragment); 
      fragmentTitles.add(name); 
     } 
    } 
} 

的viewpager工作正常保存的事實,我的標籤不會顯示。我得到的是如下畫面:

The tablayout is present but tabs are absent

我已經通過代碼很多次了,但我仍然不能似乎得到的是錯我的代碼

+0

如果您返回相同的活動(例如嘗試按方形按鈕並在同一應用程序中返回)選項卡顯示? –

+0

@Fondesa從來沒有嘗試過,但是他們這樣做。這是爲什麼? –

+0

我現在回答,等待 –

回答

4

這與設計支持庫中的常見錯誤與ViewCompat有關。

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    setupViewPager(viewPager); 
    // after you set the adapter you have to check if view is laid out, i did a custom method for it 
    if (ViewCompat.isLaidOut(tabLayout)) { 
     setViewPagerListener(); 
    } else { 
     tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 
      @Override 
      public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
       setViewPagerListener(); 
       tabLayout.removeOnLayoutChangeListener(this); 
      } 
     }); 
    } 
} 

private void setViewPagerListener() { 
    tabLayout.setupWithViewPager(viewPager); 
    // use class TabLayout.ViewPagerOnTabSelectedListener 
    // note that it's a class not an interface as OnTabSelectedListener, so you can't implement it in your activity/fragment 
    // methods are optional, so if you don't use them, you can not override them (e.g. onTabUnselected) 
    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) { 
     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 
      super.onTabReselected(tab); 
     } 

     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      super.onTabSelected(tab); 
     } 
    }); 
} 
+0

它工作!非常感謝! –

+0

@AlexKombo很好! –