Android-Navigation(导航)切换Fragment&动画(Animations)

创建两个Fragment

Android-Navigation(导航)切换Fragment&动画(Animations)_第1张图片

 Android-Navigation(导航)切换Fragment&动画(Animations)_第2张图片

创建navigation资源文件

Android-Navigation(导航)切换Fragment&动画(Animations)_第3张图片

 Android-Navigation(导航)切换Fragment&动画(Animations)_第4张图片

 Android-Navigation(导航)切换Fragment&动画(Animations)_第5张图片

 向资源文件添加FragmentAndroid-Navigation(导航)切换Fragment&动画(Animations)_第6张图片

 Android-Navigation(导航)切换Fragment&动画(Animations)_第7张图片

 activity.xml

Android-Navigation(导航)切换Fragment&动画(Animations)_第8张图片Android-Navigation(导航)切换Fragment&动画(Animations)_第9张图片

 跳转传参

传参方式一

这种方式只能给自己设置,只适合静态传递

Android-Navigation(导航)切换Fragment&动画(Animations)_第10张图片

 Android-Navigation(导航)切换Fragment&动画(Animations)_第11张图片

 方式二动态传递

homeFragment

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View viewFragment = inflater.inflate(R.layout.fragment_home, container, false);
        Button but3 = viewFragment.findViewById(R.id.button3);

        but3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //getView相当于 inflater.inflate(R.layout.fragment_home, container, false)
                EditText editText = getView().findViewById(R.id.editText01);
                if (editText.getText().toString().isEmpty()) {
                    return;
                }
                //传递值
                Bundle bundle = new Bundle();
                bundle.putString("my_name",editText.getText().toString());

                NavController navController = Navigation.findNavController(v);
                //从home跳转到detail
                navController.navigate(R.id.action_homeFragment_to_detailFragment,bundle);

            }
        });
        return viewFragment;
    }

 detailFragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //获取对应的fragment
        View viewFragment = inflater.inflate(R.layout.fragment_detail, container, false);
        //获取静态中参数
        String name = getArguments().getString("name");
        //获取从home fragment 传递过来的参数
        String myName = getArguments().getString("my_name");
        //设置值
        TextView text2 = viewFragment.findViewById(R.id.textView2);
        text2.setText(myName);

        Button but = viewFragment.findViewById(R.id.button);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                //从detail跳转到home
                navController.navigate(R.id.action_detailFragment_to_homeFragment);

            }
        });
        return viewFragment;
    }

标题返回后退

Android-Navigation(导航)切换Fragment&动画(Animations)_第12张图片

package com.example.navigationdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    NavController navController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navController = Navigation.findNavController(this, R.id.fragment);
        //actionBar 左上角后退键
        NavigationUI.setupActionBarWithNavController(this, navController);

    }

    //    重写 onSupportNavigateUp 方法
    @Override
    public boolean onSupportNavigateUp() {
        return navController.navigateUp();

    }
}

切换动画效果(Animations)

创建资源

Android-Navigation(导航)切换Fragment&动画(Animations)_第13张图片

 Android-Navigation(导航)切换Fragment&动画(Animations)_第14张图片

 从左边滑过来

其他动画标签自行查询




    

    

设置

Android-Navigation(导航)切换Fragment&动画(Animations)_第15张图片

 viewModel 之数据传动

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