ActionView set to an item in support NavigationView not showing

前端 未结 2 1215
耶瑟儿~
耶瑟儿~ 2021-01-15 00:03

I\'m using the support NavigationView in my navigation drawer to display menu of items.



        
2条回答
  •  迷失自我
    2021-01-15 00:28

    I eventually found out what was causing the issue. For NavigationView to work properly with action views, you must use AppCompat support library version 23.1

    So instead of

    compile 'com.android.support:appcompat-v7:22.3.0'
    

    I had to update to

    compile 'com.android.support:appcompat-v7:23.1.1'
    

    which made the trick and the action view in navigation drawer's navigation view started showing properly, exactly as I wanted.


    When updating to the new AppCompat version I came across several more problems like ClassNotFoundException showing up when starting the app, which I fixed by updating all com.android.support libraries to the latest version:

    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.android.support:support-annotations:23.1.1'
    ...
    

    Then I was still getting NullPointerException in my header layout set to the NavigationView. If you're setting app:headerLayout="@layout/drawer_header" or similarly in code, in AppCompat version 22 it was possible to get the header view by findViewById().

    AppCompat version 23, though, uses RecyclerView for all the items including the header view, so the way to get reference to its views is following:

     mHeaderView = navigationView.getHeaderView(HEADER_INDEX);
    

    Where HEADER_INDEX is most likely 0 if you're not adding multiple headers.

提交回复
热议问题