TabLayout的setSelectedNavigationItem(position)

时间:2016-03-25 11:24:14

标签: android android-toolbar

我希望使用PageChangeListener更改标签,就像使用ActionBar一样

actionBar.setSelectedNavigationItem(position);

由于我现在使用Toolbar和Tablayout for Fragments,我无法使用ViewPager中的onPageSelected更改Tab。

private void setupViewPager(ViewPager mViewPager) {
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    tabLayout.setScrollPosition(position, (float) 2323.22,true);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        tabLayout.addTab(tabLayout.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)));
    }

}

SectionsPagerAdapter

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    protected Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.

        switch(position) {
            case 0:
                return new FeedsFragment();
            case 1:
                return new InboxFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return mContext.getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return mContext.getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

这是我的活动代码,可用于选项卡选择

public class MainActivity extends AppCompatActivity {

private PageAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);

    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new PageAdapter(getSupportFragmentManager(), 3);

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            if (mViewPager.getCurrentItem() == 1) {
                Downloads frag1 = (Downloads) mViewPager.getAdapter().instantiateItem(mViewPager, mViewPager.getCurrentItem());
                frag1.refersh();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

在onpageseleted我完成了1页的参考方法所以你可以将任何页面引用到那里。试试这个。

<强> Pageadater.java

 public class PageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PageAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {
    Fragment f = null;
    switch (position) {
        case 0:
            f = new InstaDown();
            break;
        case 1:
            f = new Downloads();
            break;
        case 2:
            f = new guide();
            break;
    }
    return f;
}

@Override
public CharSequence getPageTitle(int position) {
    CharSequence f = null;
    switch (position) {
        case 0:
            f = "Home";
            break;
        case 1:
            f = "Downloads";
            break;
        case 2:
            f = "Tutorial";
            break;
    }
    return f;
}

@Override
public int getCount() {
    return mNumOfTabs;
}
}