我编写了这段代码来执行操作栏选项卡之间的水平滑动。但是侦听器甚至没有被调用。这是代码-
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupTabs();
pager = (ViewPager) findViewById(R.id.pager);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
System.out.println("in");
getActionBar().setSelectedNavigationItem(position);
}
});
}
private void setupTabs() {
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = bar.newTab();
tab1.setText(R.string.tab_live);
tab1.setTabListener(new MyTabListener());
ActionBar.Tab tab2 = bar.newTab();
tab2.setText(R.string.tab_tech_13);
tab2.setTabListener(new MyTabListener());
ActionBar.Tab tab3 = bar.newTab();
tab3.setText(R.string.tab_tech_14);
tab3.setTabListener(new MyTabListener());
ActionBar.Tab tab4 = bar.newTab();
tab4.setText(R.string.tab_map);
tab4.setTabListener(new MyTabListener());
ActionBar.Tab tab5 = bar.newTab();
tab5.setText(R.string.tab_contacts);
tab5.setTabListener(new MyTabListener());
bar.addTab(tab1, POS_LIVE,true);
bar.addTab(tab2, POS_TECH_13);
bar.addTab(tab3, POS_TECH_14);
bar.addTab(tab4, POS_MAP);
bar.addTab(tab5, POS_CONTACTS);
// bar.addTab(tab2, POS_COMPT);
// bar.addTab(tab3, POS_EVENTS);
}
private class MyTabListener implements ActionBar.TabListener {
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case POS_TECH_13:
FragmentTech13 frag1 = new FragmentTech13();
ft.replace(android.R.id.content, frag1);
break;
case POS_TECH_14:
FragmentTech14 frag2 = new FragmentTech14();
ft.replace(android.R.id.content, frag2);
break;
case POS_CONTACTS:
FragmentContacts frag3 = new FragmentContacts();
ft.replace(android.R.id.content, frag3);
break;
case POS_LIVE:
FragmentLive fraglive = new FragmentLive();
ft.replace(android.R.id.content, fraglive);
fraglive.setLiveUpdateListener(ActivityMain.this);
break;
}
}
这是上面的xml文件MainActivity
-
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
当setOnPageChangeListener
我尝试更改视图时,甚至没有在这里调用。