Android—Navigation的使用
Navigation是Fragment的一個容器,用于管理Fragment。
我們可以通過它實現Fragment的跳轉以及傳值等操作。
1、先寫Fragment的xml文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"><TextViewandroid:id="@+id/tv"android:gravity="center"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintLeft_toLeftOf="parent"android:layout_width="200dp"android:layout_height="60dp"android:text="跳轉到第二個Fragment"android:background="@color/white"/></androidx.constraintlayout.widget.ConstraintLayout>2、寫Fragment的java文件
public class FirstFragment extends Fragment {@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {return inflater.inflate(R.layout.first_fragment,container,false);}@Overridepublic void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);TextView b = view.findViewById(R.id.tv);b.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Navigation.findNavController(getActivity(),R.id.fragment).navigate(R.id.secondFragment);}});} } findNavController()方法就是獲取NavController,id指向的是navigation,navigate方法就是跳轉到指定id的fragment。第二個Fragment也同上兩個步驟
3、先定義一個fragment容器,Navgtion的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_fragment"app:startDestination="@id/firstFragment"><fragmentandroid:id="@+id/firstFragment"android:name="com.example.myapplication.FirstFragment"><actionapp:enterAnim="@anim/fragment_fast_out_extra_slow_in"app:exitAnim="@anim/nav_default_pop_exit_anim"android:id="@+id/toSecondFragment"app:destination="@+id/secondFragment"/></fragment><fragmentandroid:id="@+id/secondFragment"android:name="com.example.myapplication.SecondFragment"><actionapp:enterAnim="@anim/fragment_fade_enter"app:popExitAnim="@anim/fragment_close_exit"android:id="@+id/toFirstFragment"app:destination="@+id/firstFragment"/></fragment></navigation>將兩個fragment都添加進容器,為其添加action,可指定進出動畫等。
4、在activity布局中添加navigation,之前是把所有fragment都加進去,現在只需加一個navigation
<?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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><fragmentandroid:name="androidx.navigation.fragment.NavHostFragment"app:navGraph = "@navigation/nav_fragment"android:id="@+id/fragment"android:layout_height="500dp"android:layout_width="500dp"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toRightOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>name字段是固定的。
通過navGraph字段fragment標簽與navigation的xml文件綁定。
直接運行就可以實現點擊Fragment中的textView進行跳轉。
關于BootomNavigation的使用
需要在activity中讓底部導航欄與navigation綁定
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);BottomNavigationView navView = findViewById(R.id.nav_view);AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications1, R.id.a, R.id.b).build();NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);NavigationUI.setupWithNavController(navView, navController);}}AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications1, R.id.a, R.id.b) .build();
Builder參數的id需要與navigation中fragment的id和menu中的id保持一致。前后順序不影響。fragment的xml和java文件和menu文件就不展示了。
setupWithNavController()方法中就是通過比較fragment的id與menu的id進行聯動。
static boolean matchDestination(@NonNull NavDestination destination,@IdRes int destId) {NavDestination currentDestination = destination;while (currentDestination.getId() != destId && currentDestination.getParent() != null) {currentDestination = currentDestination.getParent();}return currentDestination.getId() == destId;}?acitvity的布局:
<?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"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingTop="?attr/actionBarSize"><com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/nav_view"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="0dp"android:layout_marginEnd="0dp"app:itemIconTint="@color/teal_200"app:itemTextColor="@color/teal_700"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:menu="@menu/bottom_nav_menu" /><fragmentandroid:id="@+id/nav_host_fragment"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"android:layout_height="match_parent"app:defaultNavHost="true"app:layout_constraintBottom_toTopOf="@id/nav_view"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:navGraph="@navigation/mobile_navigation" /></androidx.constraintlayout.widget.ConstraintLayout>itemIconTint和itemTextColor可以改變底部圖標顏色和字體顏色。
Navigation的缺點是跳轉方法
navigate(int)會不斷創建新的fragment,不支持復用。解決方法:https://www.jianshu.com/p/adae9494d822
總結
以上是生活随笔為你收集整理的Android—Navigation的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言return关键字
- 下一篇: 自动让钱生钱方法100%安全稳定