JetPack组件---Navigation实现页面跳转

1.概述 

       实现在fragment之间的导航 ,单activity多fragement。特别是可以利用可视化来进行导航,可以清晰,替代每次跳转需要书写的intent,fragment中切换的复杂事务。

2.实现

(1)主要的组成部分

1.三个部分实现 在fragment之间导航

NavHost:展示导航图中目标视图的空容器, 实则就是放置fragment,主要是栈,进来就是push,导航出去就是pop;存在默认的NavHostFragment显示Fragment目标

NavController:控制导航逻辑,实现页面之间的跳转

NavGraph:xml资源,设置导航路线、action;设置页面跳转的动画;

2.三者之间关系:

切换fragment时,利用NavController对象,告诉其想去NavGraph中的哪个fragment,NavController将fragment展示到NavHostFragment

3.例子

如果要实现在一个activity上面两个fragment进行跳转可以这样写

Step1:新建一个activity,两个fragment

Step2:新建navGraph添加fragment,添加navHost-activity

 JetPack组件---Navigation实现页面跳转_第1张图片

Step3:导航路线的绘制

Step4:Java中代码

public class NavigationActivity extends AppCompatActivity {

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

        NavController controller= Navigation.findNavController(this,R.id.fragmentContainerView);
        NavigationUI.setupActionBarWithNavController(this,controller);


    }
}
public class HomeFragment extends Fragment {

  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @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(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Button button;

        button=getView().findViewById(R.id.bt_na);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText=getView().findViewById(R.id.et_na);
                String string_et=editText.getText().toString();
                if(TextUtils.isEmpty(string_et)){
                    Toast.makeText(getActivity(),"请输入名字!",Toast.LENGTH_SHORT).show();
                    return;
                }
                Bundle bundle=new Bundle();
                bundle.putString("my name",string_et);
                NavController controller=Navigation.findNavController(view);
                controller.navigate(R.id.action_homeFragment_to_detailFragment,bundle);
            }
        });
    }
}
public class DetailFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_detail, container, false);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
      // getView().findViewById(R.id.bt_na1).setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_detailFragment_to_homeFragment));
       // getView().findViewById(R.id.bt_na1)
               // .setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_detailFragment_to_homeFragment));
       Button button_detail;
        button_detail=getView().findViewById(R.id.bt_na1);
        button_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavController controller=Navigation.findNavController(view);
                controller.navigate(R.id.action_detailFragment_to_homeFragment);
            }
        });
       String string=getArguments().getString("name");
      String string2=getArguments().getString("my name");
        TextView textView1=getView().findViewById(R.id.tv_na1);
      // textView.setText(string2);
        textView1.setText(string);
    }

(2)传值:

1.利用argument进行传参 (参数类型是一个key-value类型) 通过getArguments获取参数

2.动态传参利用一个bundle进行传递

Bundle bundle=new Bundle();
bundle.putString("my name",string_et);
NavController controller=Navigation.findNavController(view);
controller.navigate(R.id.action_homeFragment_to_detailFragment,bundle);

(3)页面切换的动画:

创建资源文件:类型animation

你可能感兴趣的:(android,android,前端,android,studio,后端,java)