14

我正在使用viewpager“tabs + swipe”,我想在actionBar中为每个片段设置不同的标题,这样当我切换时,标题就会改变。

我尝试了几件事但没有成功,只显示最后一个标题......并且当我切换时不再改变......

4

3 回答 3

31

首先,让你的活动实现一个OnPageChangeListener.

然后,当您创建 时ViewPager,您可以使用mViewPager.setOnPageChangeListener(this),以便您的活动在页面更改时接收回调。

最后,您需要实现OnPageChangeListener回调。你的onPageSelected()方法应该是这样的:

@Override
public abstract void onPageSelected(int position) {
    setTitle(getTitleFromPosition(position));
}

其他两个回调可以为空。

于 2012-10-04T04:33:20.883 回答
3

虽然这个问题有点过时,但我会建议我的解决方案。场景:选项卡+滑动,每个选项卡都有片段导航堆栈。因此,操作栏标题不仅可以通过切换和滑动选项卡来更改,还可以通过每个选项卡中的导航来更改。为了给每个片段提供标题,我声明TitleProvider了接口,所以如果片段有自定义标题,它必须实现这个接口。

public interface TitleProvider {
    CharSequence getTitle();
}

下面是 custom FragmentPagerAdapter,用于处理切换、滑动、导航和标题更新:

public class TabsAdapter extends FragmentPagerAdapter implements
        TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
    private final FragmentActivity activity;
    private final TabHost tabHost;
    private final ViewPager viewPager;
    private final ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private final Hashtable<Fragment, Stack<Class<?>>> fragmentBackstack = new Hashtable<Fragment, Stack<Class<?>>>();

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context context;

        public DummyTabFactory(Context context) {
            this.context = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(context);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabsAdapter(FragmentActivity activity, TabHost tabHost,
                       ViewPager pager) {
        super(activity.getSupportFragmentManager());
        this.activity = activity;
        this.tabHost = tabHost;
        viewPager = pager;
        tabHost.setOnTabChangedListener(this);
        viewPager.setAdapter(this);
        viewPager.setOnPageChangeListener(this);
    }

    public void addTab(String tag, int drawableId, Fragment fragment,
                       int tabIndicatorId) {
        TabSpec tabSpec = tabHost.newTabSpec(tag);
        tabSpec.setContent(new DummyTabFactory(activity.getApplicationContext()));

        View tabIndicator = LayoutInflater.from(
                activity.getApplicationContext()).inflate(tabIndicatorId,
                tabHost.getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.tab_title);

        if (fragment instanceof TitleProvider) {
            title.setText(((TitleProvider) fragment).getTitle());
        }

        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.tab_icon);
        icon.setImageResource(drawableId);

        tabSpec.setIndicator(tabIndicator);

        fragments.add(fragment);
        tabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

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

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

    @Override
    public int getItemPosition(Object object) {
        int returnCode;
        if (fragments.contains(object)) {
            returnCode = POSITION_UNCHANGED;
        } else {
            returnCode = POSITION_NONE;
        }
        return returnCode;
    }

    @Override
    public void onTabChanged(String tabId) {
        int position = tabHost.getCurrentTab();
        viewPager.setCurrentItem(position);
        updateTitle();
    }

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

    @Override
    public void onPageSelected(int position) {
        // Unfortunately when TabHost changes the current tab, it kindly
        // also takes care of putting focus on it when not in touch mode.
        // The jerk.
        // This hack tries to prevent this from pulling focus out of our
        // ViewPager.
        TabWidget widget = tabHost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        tabHost.setCurrentTab(position);
        widget.setDescendantFocusability(oldFocusability);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    public void navigate(Fragment fromFragment, Fragment toFragment) {
        startUpdate(viewPager);

        FragmentTransaction transaction = activity.getSupportFragmentManager()
                .beginTransaction();
        transaction.remove(fromFragment);
        transaction.commit();

        Stack<Class<?>> backstack;
        if (fragmentBackstack.containsKey(fromFragment)) {
            backstack = fragmentBackstack.get(fromFragment);
            fragmentBackstack.remove(fromFragment);
        } else {
            backstack = new Stack<Class<?>>();
        }
        backstack.push(fromFragment.getClass());
        fragmentBackstack.put(toFragment, backstack);

        fragments.set(fragments.indexOf(fromFragment), toFragment);
        finishUpdate(viewPager);
        notifyDataSetChanged();
        updateTitle();
    }

    public Boolean navigateBack() {
        startUpdate(viewPager);
        Fragment fromFragment = fragments.get(viewPager.getCurrentItem());

        if (!fragmentBackstack.containsKey(fromFragment))
            return false;
        Stack<Class<?>> backstack = fragmentBackstack.get(fromFragment);
        if (backstack.isEmpty())
            return false;

        fragmentBackstack.remove(fromFragment);
        Fragment toFragment = Fragment.instantiate(activity, backstack.pop()
                .getName());
        fragmentBackstack.put(toFragment, backstack);

        fragments.set(fragments.indexOf(fromFragment), toFragment);

        FragmentTransaction transaction = activity.getSupportFragmentManager()
                .beginTransaction();
        transaction.remove(fromFragment);
        transaction.commit();

        finishUpdate(viewPager);
        notifyDataSetChanged();
        updateTitle();
        return true;
    }

    protected Fragment getCurrentFragment() {
        return fragments.get(viewPager.getCurrentItem());
    }

    protected void updateTitle() {
        Fragment fragment = getCurrentFragment();
        if (fragment instanceof TitleProvider) {
            activity.setTitle(((TitleProvider) fragment).getTitle());
        }
    }
}
于 2013-05-26T10:12:17.030 回答
1
@Override
        public void onPageSelected(int position) {
            tabHost.setSelectedNavigationItem(position);
            if (position == 0){
                setTitle(R.string.where_is_the_bus);
            }
            else if (position == 1){
                setTitle(R.string.bus_card);
            }
            else if (position == 2){
                setTitle(R.string.favo);
            }

你可以像这样“这很容易,对吧?”

于 2015-04-18T15:07:16.457 回答