我有一个单一的Activity
,通过ViewPager
托管多个片段。在Activity的onCreate
方法中,我使用以下代码更改ActionBar
标题:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle(getResources().getString(R.string.custom_title));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
这个功能运行良好,但当Fragment
变得可见后,我无法立即更改它。为了检测它,我已经实现了以下代码:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && getContext() != null) {
((MainActivity) getContext()).editActionBarTitle("new title");
}
}
以下是 editActionBarTitle
的定义:
public void editActionBarTitle(String title) {
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
我可以看到该方法在片段变得可见时被调用,但对ActionBar的标题没有影响。我错过了什么以便正确编辑标题?
编辑
我还尝试在viewPager
中实现addOnPageChangeListener
,我可以看到方法onPageSelected
被调用,但对标题没有影响,在MainActivity中的示例如下:
public class MainActivity extends BaseActivity {
private NoScrollViewPager mViewPager;
private SectionsPagerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar initialization
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle("MainActivity");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
// ViewPager initialization
mViewPager = findViewById(R.id.containter);
setupViewPager();
}
private void setupViewPager() {
mAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mAdapter.addFragment(new Fragment1(), "Fragment 1");
mAdapter.addFragment(new Fragment2(), "Fragment 2");
mAdapter.addFragment(new Fragment3(), "Fragment 3");
mViewPager.setOffscreenPageLimit(mAdapter.getCount());
mViewPager.setAdapter(mAdapter);
// viewPager event listener
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
@Override
public void onPageSelected(int position) {
setTitle("fragment " + position);
if(getSupportActionBar() != null) {
getSupportActionBar().setTitle("fragment " + position);
}
}
@Override
public void onPageScrollStateChanged(int state) { }
});
}
public void setViewPager(int position) { mViewPager.setCurrentItem(position); }
}
这是自定义的
ViewPager
类:public class NoScrollViewPager extends ViewPager {
// Disable horizontal scrolling in ViewPager
private boolean isPagingEnabled = false;
public NoScrollViewPager(Context context) {
super(context);
}
public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
@SuppressLint("AndroidLintClickableViewAccessibility")
public boolean onTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onInterceptTouchEvent(event);
}
public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}
这是自定义适配器类:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
private FragmentManager fragmentManager;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
fragmentManager = fm;
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public Fragment getItem(int position) { return mFragmentList.get(position); }
@Override
public int getCount() { return mFragmentList.size(); }
@Override
public int getItemPosition(Object object) { return POSITION_NONE; }
}
((MainActivity)getActivity()).getSupportActionBar().setTitle();
- grabarz121