在viewPager中的片段之间滑动时,Tab hi-light不会改变

时间:2013-11-15 01:12:44

标签: android android-fragments android-viewpager android-tabs android-navigation

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

在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));
        }
on 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;
}

}

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

1 个答案:

答案 0 :(得分:2)

您没有向ViewPager添加OnPageChangedListener。尝试在initializePaging中添加以下内容:

    pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });