1

我正在操作栏中实现带有选项卡导航的视图寻呼机,并且在滑动屏幕与触摸选项卡以浏览片段时遇到问题。最初,当相应的片段可见时,选项卡会突出显示。当我单击未突出显示的选项卡进行导航时,突出显示正确更改,片段也是如此。但是当我滑动屏幕切换片段时,突出显示仍然在最后一个选项卡上。理想情况下,当屏幕被滑动以及标签被触摸时,突出显示会切换。

在 MainActivity 设置操作栏

final ActionBar actionBar = getActionBar();
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
     ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction arg1) {

            }

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction arg1) {
                // TODO Auto-generated method stub
                ViewPager mViewPager = (ViewPager)findViewById(R.id.viewpager);
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }
        };
        for (int i = 0; i < 3; i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText("Tab " + (i + 1))
                            .setTabListener(tabListener));
        }

在 MainActivity onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

在 MainActivity 中初始化分页

public void initializePaging(){

     List<Fragment> fragments = new Vector<Fragment>();

     fragments.add(TextPageFragment.newInstance(text_data_list));
     fragments.add(ImagePageFragment.newInstance(image_data_list));
     fragments.add(BothPageFragment.newInstance(item_list));

     this.mPagerAdapter  = new PagerAdapter1(super.getSupportFragmentManager(), fragments);

     ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
        pager.setAdapter(this.mPagerAdapter);
}

适配器扩展片段适配器

private List<Fragment> fragments;


public PagerAdapter1(android.support.v4.app.FragmentManager fm, List<Fragment> fragments){
    super(fm);
    this.fragments = fragments;
}


@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}


@Override
public int getCount() {
    return this.fragments.size();
}

@Override
public float getPageWidth(int position){
    float wid = (float) 1;
    return wid;
}

}

我真的无法弄清楚或找到在哪里寻找问题..

4

1 回答 1

2

您没有将OnPageChangedListener添加到您的ViewPager. 尝试添加以下内容initializePaging

    pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });
于 2013-11-15T01:43:59.503 回答