2014-11-23 56 views
1

您好我正在開發應用程序與導航抽屜和滑動選項卡,我在圖片中這樣做,但在這裏所有選項卡都位於一個片段中,我可以分割和製作片段其他選項卡(第1項,第2項...)裏面,你可以幫我做片段ECH其他(第1項,第2項...),以及如何和在那裏你需要的是增加得益於enter image description here如何爲導航抽屜中的每個滑動選項卡製作片段

private SlidingTabLayout mSlidingTabLayout; 
private ViewPager mViewPager; 

public ScreenOne() { 
} 

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

View rootView = inflater.inflate(R.layout.screen_one, container, false); 

return rootView; 
} 

    @Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
// Get the ViewPager and set it's PagerAdapter so that it can display items 
mViewPager = (ViewPager) view.findViewById(R.id.viewpager); 
mViewPager.setAdapter(new SamplePagerAdapter()); 

// Give the SlidingTabLayout the ViewPager, this must be 
// done AFTER the ViewPager has had it's PagerAdapter set. 
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs); 
mSlidingTabLayout.setViewPager(mViewPager); 
} 

// Adapter 
class SamplePagerAdapter extends PagerAdapter { 

/** 
* Return the number of pages to display 
*/ 
@Override 
public int getCount() { 
    return 10; 
} 

/** 
* Return true if the value returned from is the same object as the View 
* added to the ViewPager. 
*/ 
@Override 
public boolean isViewFromObject(View view, Object o) { 
    return o == view; 
} 

/** 
* Return the title of the item at position. This is important as what 
* this method returns is what is displayed in the SlidingTabLayout. 
*/ 
@Override 
public CharSequence getPageTitle(int position) { 
    return "Item " + (position + 1); 
} 

/** 
* Instantiate the View which should be displayed at position. Here we 
* inflate a layout from the apps resources and then change the text 
* view to signify the position. 
*/ 
@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    // Inflate a new layout from our resources 
    View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item, 
      container, false); 
    // Add the newly created View to the ViewPager 
    container.addView(view); 

    // Retrieve a TextView from the inflated View, and update it's text 
    TextView title = (TextView) view.findViewById(R.id.item_title); 
    title.setText(String.valueOf(position + 1)); 

    // Return the View 
    return view; 
} 

/** 
* Destroy the item from the ViewPager. In our case this is simply 
* removing the View. 
*/ 
@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    container.removeView((View) object); 
} 
} 
} 

回答

1

一個Fragment Pager Adapater而不是一個正常的PagerAdapter。檢查文檔,它有一個非常完整的工作示例。

+0

它會與我的屏幕一個片段,因爲它需要一個FragmentAktivity – Sultan 2014-11-23 13:47:58

+0

是的,它會工作。只要你使用孩子片段管理器,你應該沒問題。基本上,'mAdapter = new MyAdapter(getSupportFragmentManager());''通過'mAdapter = new MyAdapter(getChildFragmentManager());' – 2014-11-23 23:38:23

+0

這意味着我應該將Screen one更改爲FragmentAktivity,然後連接適配器? – Sultan 2014-11-24 04:40:28