Toggle Inside Fragment

前端 未结 1 1772
离开以前
离开以前 2021-01-22 13:03

I have made one Activity with a NavigationView (opened with a toggle button):

public class MainActivity extends AppCompatActivity implements NavigationView.OnNav         


        
1条回答
  •  囚心锁ツ
    2021-01-22 13:43

    The root cause of this is that you're using the same layout in your Fragment as you are in the Activity, and the Toolbar is inside the container ViewGroup you transact the Fragment into. Once a Fragment has been loaded with its own DrawerLayout and Toolbar, it covers the Activity's Toolbar, so nothing looks out of the ordinary. However, the toggle in the Fragment is operating the drawer in the Fragment, and that drawer is a second NavigationView that hasn't been setup like the one in the Activity, so nothing happens when you click on the drawer opened with the toggle. But, if you drag the drawer open, the one that's opening is actually the Activity's drawer, and that NavigationView was setup correctly, so clicking on that one works as expected.

    The first correction is to move the Toolbar - its element, that is - to outside of the RelativeLayout you're using as the Fragment container. This will prevent the Activity's Toolbar from being obscured when Fragments are loaded.

    The second step is to define separate, different layout XML files for the Fragments. You don't want to use the same layout in the Fragments as you are in the Activity, mostly because that's probably not really what you intend, and also because you really don't want to have multiple, redundant DrawerLayouts, Toolbars, and NavigationViews, which can cause problems, as you've seen.

    0 讨论(0)
提交回复
热议问题