如何修复TabIndicator不随选定选项卡移动

时间:2017-03-10 18:37:28

标签: java android android-viewpager android-tablayout viewpagerindicator

我正在使用tablayout来构建我的应用程序但是当我在选项卡中滑动时,选项卡指示器不起作用。任何人都可以帮助我吗?我已经尝试添加addOnTabSelectedListener,但这也不起作用。

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener{

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private PagerAdapter pagerAdapter;
private DrawerLayout drawerLayout;
private String[] pageTitle = {"myPlanner", "News", "Parking"};
private String studyRooms = "http://library2.csumb.edu/mrbs/mobilenow.php";
public static String MYPREFERENCE = "myPref";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    setSupportActionBar(toolbar);

    //create default navigation drawer toggle
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();


    tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);
    for (int i = 0; i < 3; i++) {
        tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
    }

    //set gravity for tab bar
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    //handling navigation view item event
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    assert navigationView != null;
    navigationView.setNavigationItemSelectedListener(this);

    viewPager = (ViewPager)findViewById(R.id.view_pager);
    pagerAdapter = new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
    viewPager.setAdapter(pagerAdapter);


    //setting Tab layout (number of Tabs = number of ViewPager pages)
    //viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Fragment frag = pagerAdapter.fragments[position];
            if(frag != null && frag instanceof MapsFragment){
                Toast.makeText(MainActivity.this, "On page Selected", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
   // viewPager.setOffscreenPageLimit(3);
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));

}

class PagerAdapter extends FragmentPagerAdapter{

    String tabTitles[] = new String[]{"myPlanner", "News", "Parking"};
    public Fragment[] fragments = new Fragment[tabTitles.length];
    Context context;

    public PagerAdapter(FragmentManager fm, Context context){
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount(){
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position){
        switch (position){
            case 0:
                return new myPlanner();
            case 1:
                return new News();
            case 2:
                return new MapsFragment();
        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position){
        //Generate title based on item position
        return tabTitles[position];
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){
        Fragment createdFragment = (Fragment)super.instantiateItem(container,position);
        fragments[position] = createdFragment;
        return createdFragment;
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[],int[] grantResults){

    if(requestCode == MapsFragment.MY_PERMISSIONS_REQUEST_LOCATION){
        MapsFragment mapFragment = (MapsFragment) pagerAdapter.fragments[2];
        if(mapFragment != null){
            mapFragment.onRequestPermissionsResult(requestCode,permissions,grantResults);
        }
    }
    else{
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

1 个答案:

答案 0 :(得分:1)

删除它:

tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));

并添加:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
});

此外,您应在设置tabLayout.setupWithViewPager(viewPager);

后致电viewpager
viewPager = (ViewPager)findViewById(R.id.view_pager);
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);