1.建立 2個 Fragment ,下面兩個include 不要勾java
2.建立好 Fragment 後,打開layout中的 fragment.xml 文件,將裏面默認的 textView 控件刪除掉android
把 ConstraintLayout 拖進來,用它來佈局,比較方便app
3.在res上右擊,建立一個Navigation資源ide
第4步,在navigation新建的文件中,把剛纔建立的2個 fragment 添加進來佈局
第5步,設置導航鏈接, 右邊的Label是顯示在最頂部的title內容動畫
第六步:activity_main.xml文件中,把裏面默認的控件刪除掉,並把NavHostFragment控件拖進來ui
添加後4個邊界鏈接上this
第7步,fragment 頁面添加按鈕的事件spa
按鈕的點擊事件放在這個事件中:onActivityCreatedxml
package com.example.nativigationdemo1;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import static android.view.View.OnClickListener;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
// 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_home, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button button;
//經過 getView() 就能訪問到 HomeFragment與DetialFrament中的按鈕
//注意下面這裏的id不能寫錯了,若是寫錯了會致使app啓動就出錯
button = getView().findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
NavController controller = Navigation.findNavController(v);
controller.navigate(R.id.action_homeFragment_to_detialFragment);
}
});
}
}
另外一個fragment 界面的事件方法
package com.example.nativigationdemo1;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
/**
* A simple {@link Fragment} subclass.
*/
public class DetialFragment extends Fragment {
public DetialFragment() {
// 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_detial, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().findViewById(R.id.button).setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_detialFragment_to_homeFragment));
}
}
最後一步,在 MainActivity 的 java 文件中,設置頭部導航的返回和頭部內容的顯示
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//頁面頭部標題
NavController controller = Navigation.findNavController(this,R.id.fragment); //這裏的 R.id.fragment 是 activity界面拖進來的 fragment組件(NavHotFragment)
NavigationUI.setupActionBarWithNavController(this,controller);
}
//設置返回
@Override
public boolean onSupportNavigateUp() {
//return super.onSupportNavigateUp();
NavController controller = Navigation.findNavController(this,R.id.fragment);
return controller.navigateUp();
}
注意,若是要設置動畫效果,在navigation資源xml文件中,導航鏈接上點擊,點右邊能夠設置