0

我编写了这段代码来执行操作栏选项卡之间的水平滑动。但是侦听器甚至没有被调用。这是代码-

@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我尝试更改视图时,甚至没有在这里调用。

4

2 回答 2

0

如果您使用的是 FragmentActivity,下面的代码就可以正常工作:

public class Starter extends FragmentActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {ViewPager} that will host the section contents.
 */
static ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.starter);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    //tab bar background
    mViewPager.setBackgroundColor(Color.rgb(r,g,b));

    FragmentPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());

    page = getIntent().getIntExtra("pagina", 2);

    mViewPager.setCurrentItem(page);
}
}
于 2013-09-08T13:48:07.547 回答
0

这是我的(工作)代码:

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private ActionBar.Tab mTab;
private ActionBar mActionBar;
private ViewPager mPager;
private MainViewPagerAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initTabs();
}

/**
 * Function creates all tabs with initializations and creates action bar
 */
private void initTabs(){
    // Create an actionbar
    mActionBar = getSupportActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(true);
    mActionBar.setTitle(R.string.first_tab_title);
    mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);
    mActionBar.setDisplayHomeAsUpEnabled(true);

    mPager = (ViewPager) findViewById(R.id.fragment_pager);
    mAdapter = new MainViewPagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mAdapter);
    mPager.setCurrentItem(0);
    mPager.setOnPageChangeListener(this);

    // Create Actionbar Tabs
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create first Tab
    mTab = mActionBar.newTab().setTabListener(this);
    mTab.setText(R.string.first_tab_title);
    mTab.setIcon(R.drawable.tab_pet_selected);
    mActionBar.addTab(mTab);

    // Create Second Tab
    mTab = mActionBar.newTab().setTabListener(this);
    mTab.setText(R.string.demo_tab_title);
    mTab.setIcon(R.drawable.tab_vet_selected);
    mActionBar.addTab(mTab);

    // Create Third Tab
    mTab = mActionBar.newTab().setTabListener(this);
    mTab.setText(R.string.demo_tab_title);
    mTab.setIcon(R.drawable.tab_news_selected);
    mActionBar.addTab(mTab);

    // Create Third Tab
    mTab = mActionBar.newTab().setTabListener(this);
    mTab.setText(R.string.demo_tab_title);
    mTab.setIcon(R.drawable.tab_emergency_selected);
    mActionBar.addTab(mTab);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_about:
            Intent i = new Intent(MainActivity.this, AboutActivity.class);
            startActivity(i);
            return true;
    }
    return false;
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    mPager.setCurrentItem(tab.getPosition(), true);
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onPageScrolled(int i, float v, int i2) {
}

@Override
public void onPageSelected(int i) {
    mActionBar.getTabAt(i).select();
}

@Override
public void onPageScrollStateChanged(int i) {
}

}

XML:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/layout_parent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
            android:id="@+id/fragment_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


</RelativeLayout>

结果如下:(请不要担心图形;)) 在此处输入图像描述

于 2013-09-08T14:16:29.433 回答