PagerSlidingTabStrip implement guide

前端 未结 2 596
别那么骄傲
别那么骄傲 2020-12-09 22:37

I am trying to use this library in my app:https://github.com/astuetz/PagerSlidingTabStrip

I read the documents but I didn\'t understand anything.I have two fragments

2条回答
  •  时光说笑
    2020-12-09 23:18

    Your layout file will have tabs on the top and a ViewPager on the bottom as shown in the code snippet below:

    
    
    
    
    
    

    Create Fragment

    create fragment [res/layout/fragment_page.xml] and copy and paste this code

    
    

    In PageFragment.java define the inflation logic for the fragment section of tab content:

    public class PageFragment extends Fragment {
    private int mPage;
    public static final String ARG_PAGE = "ARG_PAGE";
    
    public static PageFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        PageFragment fragment = new PageFragment();
        fragment.setArguments(args);
        return fragment;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }
    
    // Inflate the fragment layout we defined above for this fragment
    // Set the associated text for the title
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_page, container, false);
        TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        tvTitle.setText("Fragment #" + mPage);
        return view;
    }
    

    Implement FragmentPagerAdapter

    The next thing to do is to implement the adapter for your ViewPager which controls the order of the tab.

    public class SampleFragmentPagerAdapter extends FragmentPagerAdapter{
    
    
    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
    
    public SampleFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    
    @Override
    public int getCount() {
        return PAGE_COUNT;
    }
    
    @Override
    public Fragment getItem(int position) {
        return PageFragment.newInstance(position + 1);
    }
    
    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }}
    

    Setup Sliding Tabs

    Finally, we need to attach our ViewPager to the SampleFragmentPagerAdapter and then configure the sliding tabs with a two step process:

    In the onCreate() method of your activity, find the ViewPager and connect the adapter.

    Set the ViewPager on the PagerSlidingTabStrip to connect the pager with the tabs.

    public class MainActivity extends FragmentActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager()));
    
        // Give the PagerSlidingTabStrip the ViewPager
        PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        // Attach the view pager to the tab strip
        tabsStrip.setViewPager(viewPager);
    }}
    

    And this is the result

    for more information , check out the following link

    Good Luck

提交回复
热议问题