Navigation的参数传递和过渡动画


本文为学习类文档,通过学习B站up主longway777的视频,如有侵权,请联系博主进行删除

Navigation导航中的参数传递和过渡动画

设计一个文本,通过点击屏幕按钮实现将文本内容传递到指定下一个界面并显示文本

  1. 创建两个Fragement
    HomeFragement:
    在这里插入图片描述
<?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=".HomeFragment">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

DetailFragement:
在这里插入图片描述

<?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=".DetailFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  1. 创建资源类my_nav并继承navigation
    在这里插入图片描述

  2. 将my_nav捆绑在Activity上:
    在这里插入图片描述

  3. 设置一个argument
    在这里插入图片描述

  4. 填写传递数据:
    HomeFragement.java:

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController controller = Navigation.findNavController(v);
                controller.navigate(R.id.detailFragment);//传一个目的地或者传一个action效果看上去是一致的,建议用action

            }
        });
    }

DetailFragement.java:

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String string = getArguments().getString("Name");//获取参数
        TextView textView = getView().findViewById(R.id.textView);
        textView.setText(string);
    }

运行项目

在这里插入图片描述
点击按钮将Jack传递过去:
在这里插入图片描述

项目改编:

首页设置一个编辑框,用户输入信息后点击按钮实现传递数据至第二个界面并显示出来:

  1. 在HomeFragement中添加一个输入框:
    在这里插入图片描述
  2. HomeFragement.java:
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText = getView().findViewById(R.id.editText);
                String string = editText.getText().toString();
                if (TextUtils.isEmpty(string)){ //条件判空,显示提示
                    Toast.makeText(getActivity(),"请输入姓名!",Toast.LENGTH_SHORT).show();
                    return;
                }
                Bundle bundle = new Bundle(); //建立bundle用以将数据带过去,并在下面controller中带入bundle
                bundle.putString("my_name",string);

                NavController controller = Navigation.findNavController(v);
                controller.navigate(R.id.action_homeFragment_to_detailFragment,bundle);//传一个目的地或者传一个action效果看上去是一致的,建议用action
            }
        });
    }
  1. DetailFragement.java:
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //String string = getArguments().getString("Name");//获取参数
        String string2 = getArguments().getString("my_name");
        TextView textView = getView().findViewById(R.id.textView);
        textView.setText(string2);
    }
  1. 运行代码:

    点击按钮实现传递数据:
    数据传递

自定义动画——Tween Animation

举例:
创建一个资源类文件,类型为Animation
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  左侧滑入   -->
    <translate
        android:fromXDelta="-100%"
        android:toXDelta="0%"
        android:duration="300"></translate>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--右侧滑出-->
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:duration="300"></translate>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--中心点从无到有的旋转缩放-->
<scale
    android:duration="1000"
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"/>

<rotate
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="1000"/>

</set>

应用场景中即可完成对应的效果:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值