public class LoginRegister extends AppCompatActivity implements SignUpFragment.OnFragmentInteractionListener,SignInFragment.OnFragmentInteractionListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_register); final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout_signup); tabLayout.addTab(tabLayout.newTab().setText("Sign Up")); tabLayout.addTab(tabLayout.newTab().setText("Sign in")); tabLayout.setTabGravity(TabLayout.GraviTY_FILL); final ViewPager pager =(ViewPager) findViewById(R.id.loginpager); final SigninPagerAdapter adapter = new SigninPagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount()); pager.setAdapter(adapter); tabLayout.setonTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { pager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } });
当我滑过片段时,标签的高光不会改变,但是当我触摸标签时它会正常工作.当我触摸它们时,标签指示器从左向右滑动很好,标签的字体颜色也会突出显示.
我认为问题在于这段代码
pager.addOnPagechangelistener(new ViewPager.OnPagechangelistener() { @Override public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public void onFragmentInteraction(Uri uri) { } }