2017-08-09 92 views
-7

我想在片段類中添加View Pager。我嘗試過所有的東西。如何在Fragment Class中添加ViewPager?

public class MessageFragment extends android.support.v4.app.Fragment{ 

private ViewPager viewPager; 
private TabLayout tabLayout; 
public static MessageFragment newInstance() { 
    MessageFragment fragment = new MessageFragment(); 
    Log.d("Click", "newInstance: "); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

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

    View view = inflater.inflate(R.layout.fragment_message, container, false); 
    ViewPager viewPager=(ViewPager)view.findViewById(R.id.viewpager); 
    if(viewPager!=null) 
    { 
     setUpViewPager(viewPager); 
    } 
    TabLayout tabLayout = (TabLayout)view. findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(viewPager); 
    tabLayout.setTabTextColors(getResources().getColorStateList(R.color.colorToolbar)); 
    return view; 

} 
private void setUpViewPager(ViewPager viewPager) 
{ 
    Adapter adapter=new Adapter(getActivity().getSupportFragmentManager()); 
    adapter.addFragment(new TabTaskFragment(),"Tasks"); 
    adapter.addFragment(new TabChatFragment(),"Chat"); 
    viewPager.setAdapter(adapter); 
} 
static class Adapter extends FragmentPagerAdapter { 
    private final List<Fragment> mFragments = new ArrayList<>(); 
    private final List<String> mFragmentTitles = new ArrayList<>(); 
    public Adapter(FragmentManager fm) { 
     super(fm); 
    } 
    public void addFragment(Fragment fragment, String title) { 
     mFragments.add(fragment); 
     mFragmentTitles.add(title); 
    } 
    @Override 
    public Fragment getItem(int position) { 
     return mFragments.get(position); 
    } 
    @Override 
    public int getCount() { 
     return mFragments.size(); 
    } 
    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitles.get(position); 
    } 
} 

} 這裏是我的xml文件,我添加了所有viewpager代碼在這裏 請找 XML文件:

<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.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    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="wrap_content" 
     android:visibility="gone" 
     app:layout_scrollFlags="scroll|enterAlways|snap" 
     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" 
     android:background="@color/white" 
     app:tabIndicatorColor="@color/colorToolbar" 
     app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" 
     app:tabTextColor="@color/colorToolbar" /> 
</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" /> 

enter image description here

如何添加?

我無法修復它。

標籤會顯示一日一次,但是當任何底部標籤上的用戶點擊次數超過標籤說明了其顯示黑屏。

我試圖在最後失望的所有東西。

+1

那你面對的問題? – Piyush

+1

你的視圖尋呼機初始化在哪裏?視圖傳呼機的適配器在哪裏? – SripadRaj

+0

這可以幫助你https://stackoverflow.com/a/28858768/5908465 –

回答

0

類文件應該喜歡這個

public class ViewPagerFragment extends Fragment { 
    ViewPager viewPager; 
    PagerAdapter mPagerAdapter; 
    TabLayout tabLayout; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.myLayout, null); 
     viewPager = (ViewPager) view.findViewById(R.id.transactions_recharge); 
     mPagerAdapter = new ViewPagerAdapter(getChildFragmentManager(), 7); 
     viewPager.setAdapter(mPagerAdapter); 

     tabLayout = (TabLayout) view.findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 

     return view; 
    } 


    class ViewPagerAdapter extends FragmentPagerAdapter { 
     int pageCount = 0; 
     String titles[] = {"One", "Two", "Three"}; 

     ViewPagerAdapter(FragmentManager manager, int _pageCount) { 
      super(manager); 
      pageCount = _pageCount; 
     } 

     @Override 
     public Fragment getItem(int position) { 
      if (position == 0) { 
      //load fragment one 
      return new FragmentOne(); 
      } else if (position == 1) { 
      //load fragment two 
      } else if (position == 2) { 
      //load fragment three 
      } 
     } 

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

     @Override 
     public CharSequence getPageTitle(int position) { 
      return titles[position]; 
     } 

    } 

} 

而且你的XML應該我這個樣子。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:id="@+id/content_layout" 
    android:layout_height="match_parent" 
    android:background="@color/primary"> 


     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabIndicatorColor="@color/white" 
      app:tabIndicatorHeight="2dp" 
      app:tabMode="scrollable" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/transactions_recharge" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tabs" 
      android:background="@android:color/transparent"> 

     </android.support.v4.view.ViewPager> 
</RelativeLayout> 
相關問題