使用actionbarsherlock的状态寻呼机获取当前片段位置

时间:2012-12-01 06:57:04

标签: android actionbarsherlock

我有一个使用actionbarsherlock的工作状态分页器项目,但我在获取显示的当前片段时遇到问题。是否有可能获得当前片段的位置?

1 个答案:

答案 0 :(得分:3)

是的,这是可能的。如果您使用的是ViewPager,则可以这样做:

_viewPager.getCurrentItem();

_pageListener = new PageListener();
_viewPager.setOnPageChangeListener(_pageListener);

并使用类似的内容跟踪当前页面:

private int _currentPage;

    private static class PageListener extends SimpleOnPageChangeListener{
            public void onPageSelected(int position) {
                Log.i(TAG, "page selected " + position);
                   _currentPage = position;
        }
    }

或者您可以使用TabAdapter轻松完成此操作:

public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
        private final Context               mContext;
        private final ActionBar             mActionBar;
        private final ViewPager             mViewPager;
        private final ArrayList<TabInfo>    mTabs   = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final Class<?>  clss;
            private final Bundle    args;

            TabInfo(Class<?> _class, Bundle _args) {

                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {

            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getSupportActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {

            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {

            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position) {

            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }

        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        public void onPageSelected(int position) {

            mActionBar.setSelectedNavigationItem(position);
            ((SherlockFragmentActivity) mContext).supportInvalidateOptionsMenu();
        }

        public void onPageScrollStateChanged(int state) {

        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {

            Object tag = tab.getTag();
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {

        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {

        }
    }

您只需要使用一种或两种方法保留当前页面。例如,在 onTabSelected

中 希望它对你有所帮助 祝你好运