在导航抽屉中使用滑动选项卡如何为每个选项卡内部创建和设置片段

时间:2014-11-21 14:04:50

标签: android android-fragments tabs

您好我正在尝试使用导航抽屉和滑动标签开发应用程序,我制作了这样的图片,但是这里所有标签都位于一个片段中,我可以将其分割并制作片段到彼此标签(第1项,第2项......)在里面你可以帮我制作片段到其他(第1项,第2项......)以及如何以及在哪里添加感谢enter image description here

代码:

  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.view.PagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;


 public class ScreenOne extends Fragment {

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)