我正在尝试在视图分页器中创建选项卡,所有这些选项卡都在视图分页器选项卡中。
当我在视图分页器片段中使用第二个视图分页器时
所有标签都放在一起。
请帮帮我。
这个mainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDemoCollectionPagerAdapter =
new DemoCollectionPagerAdapter(
getSupportFragmentManager());
mViewPager = findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
final android.support.v7.app.ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(android.support.v7.app.ActionBar.NAVIGATION_MODE_TABS);
android.support.v7.app.ActionBar.TabListener tabListener = new android.support.v7.app.ActionBar.TabListener() {
@Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.support.v7.app.ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(android.support.v7.app.ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
};
for (int i = 0; i < 3; i++) {
actionBar.addTab(
actionBar.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListener));
}
}
这是我的片段,第二个viewpager位于其中
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
trustCollectionPagerAdapter =new TrustCollectionPagerAdapter(getActivity().getSupportFragmentManager());
mViewPagerTrust = rootView.findViewById(R.id.paging);
mViewPagerTrust.setAdapter(trustCollectionPagerAdapter);
final android.support.v7.app.ActionBar actionBarTrust = ((AppCompatActivity)getActivity()).getSupportActionBar();
actionBarTrust.setNavigationMode(android.support.v7.app.ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
android.support.v7.app.ActionBar.TabListener tabListenerTrust = new android.support.v7.app.ActionBar.TabListener() {
@Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPagerTrust.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.support.v7.app.ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(android.support.v7.app.ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
};
for (int i = 0; i < 3; i++) {
actionBarTrust.addTab(
actionBarTrust.newTab()
.setText("Tab " + (i + 1))
.setTabListener(tabListenerTrust));
}
return rootView;
}
答案 0 :(得分:0)
TabListener
已贬值。
相反,请尝试更换:
android.support.v7.app.ActionBar.TabListener tabListenerTrust = new android.support.v7.app.ActionBar.TabListener() {
使用addOnTabSelectedListener()
如下:
TabLayout tabLayout = (TabLayout) findViewById(R.id.yourtablayoutid);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Ta, FragmentTransaction fragmentTransaction) {
mViewPagerTrust.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Ta, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(TabLayout.Ta, FragmentTransaction fragmentTransaction) {
}
};