Java Code Examples for android.support.design.widget.TabLayout#setTabsFromPagerAdapter()

The following examples show how to use android.support.design.widget.TabLayout#setTabsFromPagerAdapter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From WeChatHongBao with Apache License 2.0 6 votes vote down vote up
private void setupViewPager() {
    mTabLayout = (TabLayout) findViewById(R.id.tabs);
    List<String> titles = new ArrayList<>();
    titles.add("微信");
    titles.add("支付宝");
    titles.add("QQ");

    mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(0)));
    mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(1)));
    mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(2)));
    List<Fragment> fragments = new ArrayList<>();
    fragments.add(new WeChatFragment());
    fragments.add(new AlipayFragment());
    fragments.add(new QQFragment());

    FragmentAdapter adapter =
            new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
    mViewPager.setAdapter(adapter);
    mTabLayout.setupWithViewPager(mViewPager);
    mTabLayout.setTabsFromPagerAdapter(adapter);
}
 
Example 2
Source File: ToolListActivity.java    From auid2 with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tool_list);

    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mToolType = (ToolType) getIntent().getSerializableExtra(EXTRA_TOOL_TYPE);
    if (mToolType == null) {
        throw new IllegalStateException("ToolType not available as extra; use startActivity");
    }
    setTitle(mToolType.getToolNameResourceId());

    // Set up tabs
    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    final ToolPagerAdapter toolPagerAdapter = new ToolPagerAdapter(getSupportFragmentManager(), getResources(), mToolType);
    tabLayout.setTabsFromPagerAdapter(toolPagerAdapter);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    mViewPager.setAdapter(toolPagerAdapter);
    tabLayout.setOnTabSelectedListener(this);
}
 
Example 3
Source File: TabLayoutActivity.java    From android-advanced-light with MIT License 5 votes vote down vote up
private void initViewPager() {
    mTabLayout = (TabLayout) findViewById(R.id.tabs);
    List<String> titles = new ArrayList<>();
    titles.add("精选");
    titles.add("体育");
    titles.add("巴萨");
    titles.add("购物");
    titles.add("明星");
    titles.add("视频");
    titles.add("健康");
    titles.add("励志");
    titles.add("图文");
    titles.add("本地");
    titles.add("动漫");
    titles.add("搞笑");
    titles.add("精选");

    for (int i = 0; i < titles.size(); i++) {
        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
    }
    List<Fragment> fragments = new ArrayList<>();
    for (int i = 0; i < titles.size(); i++) {
        fragments.add(new ListFragment());
    }
    FragmentAdapter mFragmentAdapteradapter =
            new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
    //给ViewPager设置适配器
    mViewPager.setAdapter(mFragmentAdapteradapter);
    //将TabLayout和ViewPager关联起来。
    mTabLayout.setupWithViewPager(mViewPager);
    //给TabLayout设置适配器
    mTabLayout.setTabsFromPagerAdapter(mFragmentAdapteradapter);
}
 
Example 4
Source File: TabLayoutActivity.java    From android-advanced-light with MIT License 5 votes vote down vote up
private void initViewPager() {
    mTabLayout = (TabLayout) findViewById(R.id.tabs);
    List<String> titles = new ArrayList<>();
    titles.add("精选");
    titles.add("体育");
    titles.add("巴萨");
    titles.add("购物");
    titles.add("明星");
    titles.add("视频");
    titles.add("健康");
    titles.add("励志");
    titles.add("图文");
    titles.add("本地");
    titles.add("动漫");
    titles.add("搞笑");
    titles.add("精选");

    for (int i = 0; i < titles.size(); i++) {
        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
    }
    List<Fragment> fragments = new ArrayList<>();
    for (int i = 0; i < titles.size(); i++) {
        fragments.add(new ListFragment());
    }
    FragmentAdapter mFragmentAdapteradapter =
            new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
    //给ViewPager设置适配器
    mViewPager.setAdapter(mFragmentAdapteradapter);
    //将TabLayout和ViewPager关联起来。
    mTabLayout.setupWithViewPager(mViewPager);
    //给TabLayout设置适配器
    mTabLayout.setTabsFromPagerAdapter(mFragmentAdapteradapter);
}
 
Example 5
Source File: CoordinatorLayoutActivity.java    From android-advanced-light with MIT License 5 votes vote down vote up
private void initViewPager() {

        mTabLayout = (TabLayout) findViewById(R.id.tabs);
        List<String> titles = new ArrayList<>();
        titles.add("精选");
        titles.add("体育");
        titles.add("巴萨");
        titles.add("购物");
        titles.add("明星");
        titles.add("视频");
        titles.add("健康");
        titles.add("励志");
        titles.add("图文");
        titles.add("本地");
        titles.add("动漫");
        titles.add("搞笑");
        titles.add("精选");
        for (int i = 0; i < titles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
        }
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++) {
            fragments.add(new ListFragment());
        }
        FragmentAdapter mFragmentAdapteradapter =
                new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        //给ViewPager设置适配器
        mViewPager.setAdapter(mFragmentAdapteradapter);
        //将TabLayout和ViewPager关联起来。
        mTabLayout.setupWithViewPager(mViewPager);
        //给TabLayout设置适配器
        mTabLayout.setTabsFromPagerAdapter(mFragmentAdapteradapter);

    }
 
Example 6
Source File: CoordinatorLayoutActivity.java    From android-coordinator-layout with MIT License 5 votes vote down vote up
private void initViewPager() {

        mTabLayout = (TabLayout) findViewById(R.id.tabs);
        List<String> titles = new ArrayList<>();
        titles.add("精选");
        titles.add("体育");
        titles.add("巴萨");
        titles.add("购物");
        titles.add("明星");
        titles.add("视频");
        titles.add("健康");
        titles.add("励志");
        titles.add("图文");
        titles.add("本地");
        titles.add("动漫");
        titles.add("搞笑");
        titles.add("精选");
        for (int i = 0; i < titles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
        }
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++) {
            fragments.add(new ListFragment());
        }
        FragmentAdapter mFragmentAdapteradapter =
                new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        //给ViewPager设置适配器
        mViewPager.setAdapter(mFragmentAdapteradapter);
        //将TabLayout和ViewPager关联起来。
        mTabLayout.setupWithViewPager(mViewPager);
        //给TabLayout设置适配器
        mTabLayout.setTabsFromPagerAdapter(mFragmentAdapteradapter);

    }
 
Example 7
Source File: ToolListActivity.java    From auid2 with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tool_list);

    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mToolType = (ToolType) getIntent().getSerializableExtra(EXTRA_TOOL_TYPE);
    if (mToolType == null) {
        throw new IllegalStateException("ToolType not available as extra; use startActivity");
    }
    setTitle(mToolType.getToolNameResourceId());

    // Set up tabs
    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    final ToolPagerAdapter toolPagerAdapter = new ToolPagerAdapter(getSupportFragmentManager(), getResources(), mToolType);
    tabLayout.setTabsFromPagerAdapter(toolPagerAdapter);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    mViewPager.setAdapter(toolPagerAdapter);
    tabLayout.setOnTabSelectedListener(this);

    // Handle animation from previous activity
    postponeEnterTransition();
    mViewPager.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            mViewPager.getViewTreeObserver().removeOnPreDrawListener(this);
            startPostponedEnterTransition();
            return true;
        }
    });

    setEnterSharedElementCallback(new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            if (mViewPager.getCurrentItem() != 0) {
                // Not displaying the about page, which has the hero image
                names.clear();
                sharedElements.clear();
            }
        }
    });


}
 
Example 8
Source File: ToolListActivity.java    From auid2 with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tool_list);

    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mToolType = (ToolType) getIntent().getSerializableExtra(EXTRA_TOOL_TYPE);
    if (mToolType == null) {
        throw new IllegalStateException("ToolType not available as extra; use startActivity");
    }
    setTitle(mToolType.getToolNameResourceId());

    // Set up tabs
    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    final ToolPagerAdapter toolPagerAdapter = new ToolPagerAdapter(getSupportFragmentManager(), getResources(), mToolType);
    tabLayout.setTabsFromPagerAdapter(toolPagerAdapter);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    mViewPager.setAdapter(toolPagerAdapter);
    tabLayout.setOnTabSelectedListener(this);

    // Handle animation from previous activity
    postponeEnterTransition();
    mViewPager.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            mViewPager.getViewTreeObserver().removeOnPreDrawListener(this);
            startPostponedEnterTransition();
            return true;
        }
    });

    setEnterSharedElementCallback(new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            if (mViewPager.getCurrentItem() != 0) {
                // Not displaying the about page, which has the hero image
                names.clear();
                sharedElements.clear();
            }
        }
    });


}