毕设博客(十一) —— Navigation(2)参数传递及动画过渡
作者:互联网
参数传递:
- 方式一:
给Detailfragment添加Arguments:
@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);
}
运行结果:
- 方式二:
给Homefragment添加一个EditText :
...
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editText = getView().findViewById(R.id.editTextTextPersonName);
String string = editText.getText().toString();
if (TextUtils.isEmpty(string)){
Toast.makeText(getActivity(),"Please input name",Toast.LENGTH_SHORT).show();
return;
}
Bundle bundle = new Bundle();
bundle.putString("my_name",string);
NavController controller = Navigation.findNavController(view);
controller.navigate(R.id.action_homeFragment_to_detailFragment,bundle);
}
});
}
}
在Detailfragment里获取“my_name”:
...
@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
1、创建一个资源:
<?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>
2、在my_nav_graph中,选中动作,添加“Enter”的动作为刚刚创建的“slide_from_left” :
运行结果:
同理,也可以做出其他的自定义动画,例如:向右滑出、缩放等等:
在实际应用中,要根据实际情况来自定义动画,不能太过花哨。
标签:毕设,string,onActivityCreated,savedInstanceState,Bundle,参数传递,Navigation,textView,n 来源: https://blog.csdn.net/weixin_44826288/article/details/122809524