将FragmentContainerView与导航组件一起使用?

时间:2019-10-10 10:09:13

标签: android android-layout

从先前版本更新到导航2.2.0-beta01后,lint会发出警告,将<fragment>标签替换为FragmentContainerView

但是,仅更换标签似乎可以防止导航图膨胀。

根据2.2.0-alpha01FragmentContainerView在内部使用。我们应该忽略棉绒警告吗?


activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Lint warning: "Replace the <fragment> tag with FragmentContainerView. -->
    <fragment
        android:id="@+id/nav_host_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph_main"/>

    <!-- other elements -->

</androidx.constraintlayout.widget.ConstraintLayout>

4 个答案:

答案 0 :(得分:8)

现在,您还应该声明对片段1.2.0-beta02的依赖性,因为它包含此用例的修复程序。

implementation "androidx.fragment:fragment:1.2.0-beta02"
  

版本1.2.0-beta02

     

2019年10月11日

     

错误修复

     

解决了Fragment的onInflate()无法从FragmentContainerView接收适当属性的问题,打破了诸如NavHostFragment之类的情况。 (b/142421837

来源:https://developer.android.com/jetpack/androidx/releases/fragment#1.2.0-beta02

答案 1 :(得分:7)

如果仅将 fragment 替换为 ,仍然有一个错误会引发异常。许多Google工程师在此bug thread中指出,目前的解决方法是在您的活动中更改您的代码:

val navHostFragment = supportFragmentManager.findFragmentById(R.id.my_nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController

答案 2 :(得分:2)

NavigationBasicSample已更新为2.2.0-alpha01,但仍在使用using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Xml; using System.Xml.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Windows.Media.Media3D; using System.Xml.Serialization; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace NewBird { public partial class MainWindow : Window { public MainWindow() { } List<Bird> BirdList = new List<Bird>(); Bird bx = new Bird(); List<Bird.Nest> NList = new List<Bird.Nest>(); private void AddNewBird_Click(object sender, RoutedEventArgs e) { Process tempBird = new Process(); string tempName = birdtextbox.Text; } private void AddNewNest_Click(object sender, RoutedEventArgs e) { Bird.Nest tempNest = new Bird.Nest(); NList.Add(tempNest); } private void Add2List_Click(object sender, RoutedEventArgs e) { Bird holder = new Bird(); holder= bx; holder.nl = NList; BirdList.Add(holder); } } } public class Bird { public string BirdName { get; set; } public List<Nest> nl { get; set; } public Nest nest{ get; set; } [Serializable()] public class Nest { public int Number { get; set; } } } 标签。 NavigationAdvancedSample显示了fragment的用法,但是nav图在代码中被夸大了(它们具有几个不同的图),并且相应的主机片段被添加到FragmentContainerView中。所以我想说,如果我们想要自动充气,我们应该忽略该警告。

答案 3 :(得分:1)

使用Java

NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
                .findFragmentById(R.id.nav_host_fragment);
navController = navHostFragment.getNavController();