Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

android - Different toolbar for fragments and Navigation Drawer

Please, explain to me... I have Navigation Drawer in my Activity and it syncs with Toolbar (like ActionBar). Activity has few fragments and in different fragments I need to use different AppBar modes (parallax in one, simple in another). So, I think that I should set CoordinatorLayout in each frament with AppBar and content.
But how I can replace last toolbar on new to save synchronization with Drawer? Or it's wrong way and I need make it some else?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not sure if my approach is good, but I tried to add this public method to my activity:

public void setToolbar(Toolbar toolbar) {
    if(toolbar != null) {
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
    } else {
        drawer.setDrawerListener(null);
    }
}

and I added this in all fragments:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((MainActivity)getActivity()).setToolbar(toolbar);
}

@Override
public void onDestroyView() {
    ((MainActivity)getActivity()).setToolbar(null);
    super.onDestroyView();
}

It's working fine, but I'm not sure if it may cause a memory leak or any other performance issue. Maybe someone can help with it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...