Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android getPageTitle未工作_Java_Android - Fatal编程技术网

Java android getPageTitle未工作

Java android getPageTitle未工作,java,android,Java,Android,我正在使用FragmentPagerAdapter,但有一个问题 getPageTitle()方法。一切都在运转,但它不起作用。请看一下我的代码,帮我解决问题 在我的主要活动中,我宣布: //请尝试下面的代码 public class MyFragmentPageAdapter extends FragmentPagerAdapter { protected static final String[] CONTENT = new String[] { "a", "b", "c"};

我正在使用FragmentPagerAdapter,但有一个问题 getPageTitle()方法。一切都在运转,但它不起作用。请看一下我的代码,帮我解决问题

在我的主要活动中,我宣布:

//请尝试下面的代码

 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;
    }

}

我也有同样的问题,并假设这是一个安卓错误。使用以下代码在TabLayout.createTabAtPosition()中设置页面标题:
return newTab().setText(“页面标题”)

你能粘贴你的OnPageChangeListener实现吗?嗨,LR89,我实现了OnPageChangeListener,它不调用getPageTitle()方法,有什么帮助吗?那么你在哪里调用getPageTitle()以及“它不工作”是什么意思呢。它返回null还是什么?您的适配器看起来很好,所以应该可以工作。您好,LR89,它是一个@override方法,所以我认为在运行pageAdapter时会调用它,即getItems()。我的意思是,当我运行寻呼机时,它没有被调用。@Hainguyen可能是你对这个函数太困惑了。在这里,getPageTitle()只是一个覆盖函数,用于获取当前页面标题(默认设置)。所以它是基于你想在哪里/什么时候显示你的标题。在这里,您使用的是查看寻呼机,没有理由使用它,就让它自己去吧。如果使用Actionbar,则可以通过调用该函数来设置所选页面标题。
ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setOnPageChangeListener(this);
        FragmentManager fm = getSupportFragmentManager();
        MyFragmentPageAdapter fragmentAdapter = new MyFragmentPageAdapter(fm,
                id);
        pager.setAdapter(fragmentAdapter);
 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;
    }

}