在使用底部导航的单个活动中找不到使用androidx导航架构的任何Java示例

时间:2019-04-19 16:30:32

标签: java navigation androidx

我无法使用Java找到任何示例,google提供的所有代码实验室似乎都在kotlin中,理想情况下,我不想学习该语言。

有没有使用1个活动,2/3片段都由我可以浏览的底部导航栏链接的简单示例?

我看了谷歌提供的所有文档,无法理解。

我只知道我会使用

 NavHostFragment.findNavController(Fragment)
    Navigation.findNavController(Activity, @IdRes int viewId)
    Navigation.findNavController(View)

在Java中我应该使用

 Navigation.createNavigateOnClickListener()

我希望我离这个不太远:

public class MainActivity extends AppCompatActivity {

NavController navController;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView navView = findViewById(R.id.nav_view);

    navController = Navigation.findNavController(this, R.id.nav_host_fragment);

    NavigationUI.setupWithNavController(navView,navController);
}

}

谢谢。

1 个答案:

答案 0 :(得分:0)

最终找到了面临类似新手问题的其他人的答案:

/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/nav_graph"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/nav_view"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java在onCreate下

  BottomNavigationView navView = findViewById(R.id.nav_view);
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(navView,navController);

在/res/navigation/nav_graph.xml下

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/mapFragment">
    <fragment
        android:id="@+id/mapFragment"
        android:name="com.company.pinpoint.MapFragment"
        android:label="fragment_map"
        tools:layout="@layout/fragment_map">
        <action
            android:id="@+id/action_mapFragment_to_newsFeedFragment"
            app:destination="@id/newsFeedFragment" />
        <!--app:enterAnim="@anim/slide_in_right"-->
        <!--app:exitAnim="@anim/slide_out_left"-->
        <!--app:popEnterAnim="@anim/slide_in_left"-->
        <!--app:popExitAnim="@anim/slide_out_right" />-->
    </fragment>
    <fragment
        android:id="@+id/newsFeedFragment"
        android:name="com.company.pinpoint.NewsFeedFragment"
        android:label="fragment_news_feed"
        tools:layout="@layout/fragment_news_feed" />
</navigation>