0

目前我正在尝试开发一个实现在不同视图之间滑动的应用程序。刷卡工作正常,但我想将视图的标题设置为操作栏,以便用户知道他在哪里。

所以我希望你能给我一些想法,我必须在哪里放置标题更改的代码。

谢谢

4

2 回答 2

1

您需要OnPageChangeListener为您的ViewPager. 当您滑动时,OnPageChangeListener将收到通知,您可以ActionBar从回调中更改标题。

这是给你的一些伪代码:

ViewPager vp;
vp.setOnPageChangeListener(new OnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
        getActionBar().setTitle(titles[position]); // Set the correct title based on the position
    }

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

    @Override
    public void onPageScrollStateChanged(int state) {} // not used
});
于 2013-07-18T11:41:44.530 回答
0
public void setTitle (CharSequence title)

Change the title associated with this activity. If this is a top-level activity, the title for its window will change. If it is an embedded activity, the parent can do whatever it wants with it.

http://developer.android.com/reference/android/app/Activity.html#setTitle(int)

mPager.setOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(int pos) {
                    // Chage the title
                }


                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub

                }
            });
于 2013-07-18T11:19:09.977 回答