Android support v23.1.0 update breaks NavigationView get/find header

后端 未结 5 630
野趣味
野趣味 2020-12-03 01:27

I have been using the v23.0.1 support library until now with no problems. Now when I switch to the new v23.1.0 library I am getting a null

相关标签:
5条回答
  • 2020-12-03 01:31

    With the design library v 23.1.0 the NavigationView works with a RecyclerView.
    Also the Header is now a type of row.

    It means that the header could not be immediately available in the view hierarchy.
    It can cause issues if you are using methods like navigationView.findViewById(XXX) to get a view inside the header.

    There is a bug in the Google Tracker.

    EDIT 12/10/2015: Design library 23.1.1

    The 23.1.1 introduces a new API for retrieving header views for NavigationView with getHeaderView()

    BEFORE 23.1.1

    workaround fot 23.1.0 can be to use a addOnLayoutChangeListener. Somenthing like:

    navigationView.addOnLayoutChangeListener( new View.OnLayoutChangeListener()
    {
        @Override
        public void onLayoutChange( ... )
        {
            navigationView.removeOnLayoutChangeListener( this );
    
            View view = navigationView.findViewById( ... );
        }
    } );
    

    Another possible workaround are:

    • remove the app:headerLayout attribute from the xml, and then add the header programatically.

    • Inflate the headerView programmatically.

    Use somenthing like this:

    View headerLayout = navigationView.inflateHeaderView(R.layout.navigation_header);
    headerLayout.findViewById(xxx);
    
    0 讨论(0)
  • 2020-12-03 01:36

    in the new NavigationView the header is now a row type of RecyclerView in order for you or anybody to find the view by its id you'll need to workaround it and use addOnLayoutChangeListener listener and then you can find the view i know it should be documented somewhere but android be like meh!.

    0 讨论(0)
  • 2020-12-03 01:37

    it is a bug at 23.1.0

    23.1.1 fixed

    https://plus.google.com/+AndroidDevelopers/posts/ebXLByBiEBU

    0 讨论(0)
  • 2020-12-03 01:39

    It appears attaching the header view to the navigation drawer using xml is currently broken. The solution is to inflate and attach the view manually.

    activity layout

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header" <!-- remove this line -->
        app:menu="@menu/drawer_items" />
    

    Then in your code inflate and attach the header by doing the following.

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    View drawerHeader = navigationView.inflateHeaderView(R.layout.drawer_header);
    
    TextView username = (TextView) drawerHeader.findViewById(R.id.username_textView);
    
    0 讨论(0)
  • 2020-12-03 01:46

    I have updated build tools from Android sdk manager, then 23.1.0 is also working fine for me.

    I am using

    buildToolsVersion "23.0.2"
    

    before this it was 23.0.1.

    and there is no need of using

    (View)navigationView.findViewById(R.id.idOfViewFromHeaderView);
    

    In your activity you can directly use

    (View)findViewById(R.id.idOfViewFromHeaderView);
    
    0 讨论(0)
提交回复
热议问题