如何为导航抽屉中的每个滑动选项卡制作碎片

时间:2014-11-23 12:27:38

标签: android android-fragments tabs

您好我正在尝试使用导航抽屉和滑动标签开发应用程序,我在图片中制作了这样的图片,但是这里所有标签都位于一个片段中,我可以将其分割并分割成彼此标签(第1项,第2项...... )你能帮我把片段变成其他的(第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 个答案:

答案 0 :(得分:1)

您需要的是Fragment Pager Adapater而不是普通的PagerAdapter。查看文档,它有一个非常完整的工作示例。