我将 Viewpager 与 TabLayout 一起使用,并在 setOnTabSelectedListener 触发的片段之间滑动,与单击选项卡相同。
更新后的 gradle 依赖项有问题,即滑动不再触发 setOnTabSelectedListener,仅在单击选项卡时。
将所有依赖项从 22 更改为 23 时,是否需要做一些不同的事情?
这是 Gradle 依赖项:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:7.8.0'
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:recyclerview-v7:23.0.0'
}
以下是我如何对 TabLayout 执行制表符:
private TabLayout tabLayout;
private void setupTablayout(){
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab1)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab2)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab3)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount(), this);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Voids.savePreferences(getApplicationContext(), Voids.CHAT_SELECTED_TAB, tab.getPosition());
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 0:
// Do first tab actions
break;
case 1:
// Do second tab actions
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}