android getPageTitle不起作用

时间:2014-05-28 10:02:42

标签: java android

  

我正在使用FragmentPagerAdapter并且遇到问题   getPageTitle()方法。 Everythings工作但它。请看看我的代码,并帮助我。

public class MyFragmentPageAdapter extends FragmentPagerAdapter {

        final int PAGE_COUNT = 3;
        public final static String PAGE_CURRENT = "current_age";
        public final static String PAGE_ID = "id";
        int id = 0;

        public MyFragmentPageAdapter(FragmentManager fm, int id) {
            super(fm);
            this.id = id;
        }

        @Override
        public Fragment getItem(int position) {
            MyFragment myFragment = new MyFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(PAGE_CURRENT, position + 1);
            bundle.putInt(PAGE_ID, id);
            myFragment.setArguments(bundle);
            return myFragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            CharSequence title = null;
            if (position == 0) {
                title = "a";
            } else if (position == 1) {
                title = "b";
            } else if (position == 2) {
                title = "c";
            }
            return title;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return PAGE_COUNT;
        }

    }
  

在我的主要活动中我声明:

ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setOnPageChangeListener(this);
        FragmentManager fm = getSupportFragmentManager();
        MyFragmentPageAdapter fragmentAdapter = new MyFragmentPageAdapter(fm,
                id);
        pager.setAdapter(fragmentAdapter);

2 个答案:

答案 0 :(得分:0)

//尝试以下代码

 public class MyFragmentPageAdapter extends FragmentPagerAdapter {
 protected static final String[] CONTENT = new String[] { "a", "b", "c"};
    final int PAGE_COUNT = 3;
    public final static String PAGE_CURRENT = "current_age";
    public final static String PAGE_ID = "id";
    int id = 0;

 private int mCount = CONTENT.length;

  // CHANGE STARTS HERE
  private int current_position=0;

  public void set_current_position(int i) {
    current_position = i;
  }

    public MyFragmentPageAdapter(FragmentManager fm, int id) {
        super(fm);
        this.id = id;
    }

    @Override
    public Fragment getItem(int position) {
        MyFragment myFragment = new MyFragm`enter code here`ent();
        Bundle bundle = new Bundle();
        bundle.putInt(PAGE_CURRENT, position + 1);
        bundle.putInt(PAGE_ID, id);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        if (position == current_position-1) {
        return "Previous";
    } else if (position == current_position+1) {
        return "Next";
    }
    // CHANGE ENDS HERE
    return MyFragmentPageAdapter.CONTENT[position % CONTENT.length];
}


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return PAGE_COUNT;
    }

}

答案 1 :(得分:0)

我遇到了同样的问题,并认为这是一个Android错误。使用以下代码在TabLayout.createTabAtPosition()中设置页面标题: return newTab().setText("the page title");