无法在ViewPager中的片段中编辑ActionBar标题 [英] Unable to edit ActionBar title from Fragment in ViewPager

查看:49
本文介绍了无法在ViewPager中的片段中编辑ActionBar标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Activity,它通过ViewPager承载多个片段.在Activity的onCreate方法中,我使用以下代码更改ActionBar标题:

I have a single Activity which hosts multiple fragments through a ViewPager. In the Activity's onCreate method I'm using the following code to change the ActionBar title:

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if(getSupportActionBar() != null) {
        getSupportActionBar().setTitle(getResources().getString(R.string.custom_title));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

它可以正常工作,但是一旦Fragment可见,我便无法更改它.为了检测到它,我已经实现了:

It works fine, but then I am unable to change it as soon as a Fragment becomes visible. In order to detect it I've implemented:

@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的标题没有影响.为了正确编辑标题,我缺少什么?

I can see that the method gets called as soon as a fragment becomes visible, but it has no effect on the ActionBar's title. What am I missing in order to edit the title correctly?

编辑

我还尝试过在viewPager中实现addOnPageChangeListener,我可以看到方法onPageSelected被调用了,但是对标题没有影响,这是MainActivity上的一个示例:

I've also tried implementing addOnPageChangeListener in the viewPager, I can see that the method onPageSelected gets called but it has no effect on the title, here's an example on 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;
    }
}

这是自定义Adapter类:

And this is the custom Adapter class:

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

}

推荐答案

在您的onCreateView方法中尝试将其片段化.也许会对您有帮助.

Try this in your onCreateView method into fragment. May be it will helps you.

if(getActivity() != null){
     getActivity().setTitle("Your title");
}

如果您在片段中使用ViewPager,则可以使用

If you used ViewPager inside a fragment then you can use

@Override
    public void setUserVisibleHint(boolean isVisible) {
        super.setUserVisibleHint(isVisible);

        if (isVisible) {
            if(getActivity() != null){
               getActivity().setTitle("Your title");
            }
        }
    }

这篇关于无法在ViewPager中的片段中编辑ActionBar标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆