Google I/O 大會中國場次已經(jīng)結(jié)束透典,最近也上線了 Android Studio 3.2 版本晴楔,在新版本上有一個谷歌著重推薦的架構(gòu)組件 Navigation。
Navigation 主要是提供了 Fragment 之間的跳轉(zhuǎn)切換峭咒,棧管理滥崩,可以使我們在應(yīng)用中更加輕松的使用 Fragment。先看下我使用兩個 Fragment 實現(xiàn)的最終效果:
第一步:想要使用 Navigation讹语,首先要將 Android Studio 升級到 3.2 版本,然后在 Settings 中最后一行找到 Experimental钙皮,對右側(cè) Enable Navigation Editor 打鉤,如下圖所示顽决,然后重啟 studio 使設(shè)置生效短条。
第二步: 創(chuàng)建兩個 Fragment,F(xiàn)irstFragment 和 SecondFragment才菠,布局內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:text="我是第一個Fragment"/>
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="跳轉(zhuǎn)到第二個Fragment"
android:textAllCaps="false"
/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondFraagment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第二個Fragment"/>
<Button
android:textAllCaps="false"
android:id="@+id/btn_back"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
/>
</FrameLayout>
第三步: 創(chuàng)建導(dǎo)航視圖文件
在 res 目錄下新建 navigation 文件夾茸时,然后新建一個 navigation 資源文件(需要 Android Studio3.2以上版本)
打開資源文件,然后切換為可視化模式赋访,將兩個已經(jīng)創(chuàng)建好的 Fragment 添加進(jìn)去可都,用箭頭連接它們缓待,這樣就為它們綁定了跳轉(zhuǎn)事件。
切換為 Text 模式后渠牲,可以看到具體的實現(xiàn)代碼:
<?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/changefragment"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="keven.com.myteststudio3l2.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first">
<action
android:id="@+id/action_firstFragment_to_secondFraagment"
app:destination="@id/secondFraagment"/>
</fragment>
<fragment
android:id="@+id/secondFraagment"
android:name="keven.com.myteststudio3l2.SecondFraagment"
android:label="fragment_second_fraagment"
tools:layout="@layout/fragment_second_fraagment"/>
</navigation>
第四步: 編輯 MainActivity
在 Acitvity 布局文件中添加 fragment 組件旋炒,設(shè)置 name 屬性為 androidx.navigation.fragment.NavHostFragment。在傳統(tǒng)的單 Activity 多 Fragment 場景中签杈,我們往往需要為 Activity 添加一個 FrameLayout 作為 Fragment 的容器瘫镇。在 Navigation 中 HavHostFragment 就是 Fragment 的容器。布局中的 navGraph 屬性建立了與 navigation 資源文件的關(guān)系答姥。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<fragment
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/changefragment"
/>
</android.support.constraint.ConstraintLayout>
重寫 onSupportNavigateUp()方法铣除,目的是將 back 事件委托出去。若棧中有兩個以上 Fragment鹦付,點擊 back 鍵就會返回到上一個 Fragment尚粘。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onSupportNavigateUp() {
Fragment fragment=getSupportFragmentManager().findFragmentById(R.id.fragment);
return NavHostFragment.findNavController(fragment).navigateUp();
}
}
第五步: 在 Fragment 中設(shè)置相應(yīng)的跳轉(zhuǎn)事件。
public class FirstFragment extends Fragment {
public FirstFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnNext=view.findViewById(R.id.btn_next);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Navigation.findNavController(v).navigate(R.id.action_firstFragment_to_secondFraagment);
}
});
}
}
public class SecondFraagment extends Fragment {
public SecondFraagment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second_fraagment, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnBack=view.findViewById(R.id.btn_back);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Navigation.findNavController(v).navigateUp();
}
});
}
}
如果在界面切換的過程中需要傳值敲长,就把數(shù)據(jù)設(shè)置在 Bundle 中郎嫁,使用如下代碼:
Navigation.findNavController(view).navigate(actionid,bundle);
現(xiàn)在,我們就可以把程序運行起來了潘明,如果需要轉(zhuǎn)場動畫行剂,可以在 Navigation 資源文件的 action 標(biāo)簽下秕噪,添加動畫屬性钳降。