Navigation的参数传递和过渡动画
本文为学习类文档,通过学习B站up主longway777的视频,如有侵权,请联系博主进行删除
Navigation导航中的参数传递和过渡动画
设计一个文本,通过点击屏幕按钮实现将文本内容传递到指定下一个界面并显示文本
- 创建两个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>
-
创建资源类my_nav并继承navigation
-
将my_nav捆绑在Activity上:
-
设置一个argument
-
填写传递数据:
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传递过去:
项目改编:
首页设置一个编辑框,用户输入信息后点击按钮实现传递数据至第二个界面并显示出来:
- 在HomeFragement中添加一个输入框:
- 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
}
});
}
- 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);
}
- 运行代码:
点击按钮实现传递数据:
自定义动画——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>
应用场景中即可完成对应的效果: