• Navigation 组件(二) Fragment 跳转带参数,动画


    1.创建 HomeFragment

      1.1 HomeFragment.java

    1. public class HomeFragment extends Fragment {
    2. @Override
    3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    4. return inflater.inflate(R.layout.fragment_home, container, false);
    5. }
    6. @Override
    7. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    8. super.onViewCreated(view, savedInstanceState);
    9. EditText editText = view.findViewById(R.id.editText);
    10. view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
    11. @Override
    12. public void onClick(View view) {
    13. String string = editText.getText().toString().trim();
    14. Bundle bundle = new Bundle();
    15. bundle.putString("my_name", string);
    16. NavController controller = Navigation.findNavController(view);
    17. controller.navigate(R.id.action_homeFragment_to_detailFragment, bundle);
    18. }
    19. });
    20. }
    21. }

      1.2 fragment_home.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".HomeFragment">
    8. <Button
    9. android:id="@+id/button"
    10. android:layout_width="wrap_content"
    11. android:layout_height="wrap_content"
    12. android:text="Button"
    13. app:layout_constraintBottom_toBottomOf="parent"
    14. app:layout_constraintEnd_toEndOf="parent"
    15. app:layout_constraintStart_toStartOf="parent"
    16. app:layout_constraintTop_toTopOf="parent" />
    17. <EditText
    18. android:id="@+id/editText"
    19. android:layout_width="wrap_content"
    20. android:layout_height="wrap_content"
    21. android:ems="10"
    22. android:hint="name"
    23. android:inputType="textPersonName"
    24. android:text="Name"
    25. app:layout_constraintBottom_toTopOf="@+id/button"
    26. app:layout_constraintEnd_toEndOf="parent"
    27. app:layout_constraintStart_toStartOf="parent"
    28. app:layout_constraintTop_toTopOf="parent" />
    29. </androidx.constraintlayout.widget.ConstraintLayout>

    2. 创建 DetailFragment

      2.1 DetailFragment.java

    1. public class DetailFragment extends Fragment {
    2. @Override
    3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    4. return inflater.inflate(R.layout.fragment_detail, container, false);
    5. }
    6. @Override
    7. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    8. super.onViewCreated(view, savedInstanceState);
    9. String string = getArguments().getString("name");
    10. String string2 = getArguments().getString("my_name");
    11. TextView textView = getView().findViewById(R.id.textView);
    12. textView.setText(string2);
    13. }
    14. }

      2.2 fragment_detail.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".DetailFragment" >
    8. <TextView
    9. android:id="@+id/textView"
    10. android:layout_width="wrap_content"
    11. android:layout_height="wrap_content"
    12. android:text="TextView"
    13. android:textSize="24sp"
    14. app:layout_constraintBottom_toBottomOf="parent"
    15. app:layout_constraintEnd_toEndOf="parent"
    16. app:layout_constraintStart_toStartOf="parent"
    17. app:layout_constraintTop_toTopOf="parent"
    18. app:layout_constraintVertical_bias="0.499" />
    19. </androidx.constraintlayout.widget.ConstraintLayout>

    3. 创建 my_navigation.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <navigation xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:id="@+id/my_navigation"
    6. app:startDestination="@id/homeFragment">
    7. <fragment
    8. android:id="@+id/homeFragment"
    9. android:name="com.example.navigationdemo2.HomeFragment"
    10. android:label="fragment_home"
    11. tools:layout="@layout/fragment_home">
    12. <action
    13. android:id="@+id/action_homeFragment_to_detailFragment"
    14. app:destination="@id/detailFragment"
    15. app:enterAnim="@anim/scale_rotation"
    16. app:exitAnim="@anim/slide_to_right">
    17. <argument
    18. android:name="name"
    19. android:defaultValue="Tom" />
    20. </action>
    21. </fragment>
    22. <fragment
    23. android:id="@+id/detailFragment"
    24. android:name="com.example.navigationdemo2.DetailFragment"
    25. android:label="fragment_detail"
    26. tools:layout="@layout/fragment_detail" >
    27. <argument
    28. android:name="name"
    29. app:argType="string"
    30. android:defaultValue="Jack" />
    31. </fragment>
    32. </navigation>

    4. MainActivity

      4.1 MainActivity.java

    1. public class MainActivity extends AppCompatActivity {
    2. @Override
    3. protected void onCreate(Bundle savedInstanceState) {
    4. super.onCreate(savedInstanceState);
    5. setContentView(R.layout.activity_main);
    6. }
    7. }

      4.2 activity_main.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".MainActivity">
    8. <androidx.fragment.app.FragmentContainerView
    9. android:id="@+id/fragmentContainerView"
    10. android:name="androidx.navigation.fragment.NavHostFragment"
    11. android:layout_width="match_parent"
    12. android:layout_height="match_parent"
    13. app:defaultNavHost="true"
    14. app:navGraph="@navigation/my_navigation" />
    15. </androidx.constraintlayout.widget.ConstraintLayout>

    5. 动画文件 anim

      5.1 slide_from_left.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <set xmlns:android="http://schemas.android.com/apk/res/android">
    3. <translate
    4. android:duration="300"
    5. android:fromXDelta="-100%"
    6. android:toXDelta="0%">
    7. </translate>
    8. </set>

      5.2 slide_to_right.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <set xmlns:android="http://schemas.android.com/apk/res/android">
    3. <translate
    4. android:duration="300"
    5. android:fromXDelta="0"
    6. android:toXDelta="100%">
    7. </translate>
    8. </set>

      5.3 scale_rotation.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <set xmlns:android="http://schemas.android.com/apk/res/android">
    3. <scale
    4. android:duration="1000"
    5. android:fromXScale="0.0"
    6. android:fromYScale="0.0"
    7. android:pivotX="50%"
    8. android:pivotY="50%"
    9. android:toXScale="1.0"
    10. android:toYScale="1.0" />
    11. <rotate
    12. android:duration="1000"
    13. android:fromDegrees="0"
    14. android:pivotX="50%"
    15. android:pivotY="50%"
    16. android:toDegrees="360" />
    17. </set>

  • 相关阅读:
    python+vue实验室课程预约管理系统
    289. 生命游戏 Python
    设计模式之代理模式的理解
    完成flex布局与float布局
    无人机避障技术
    EIGRP & OSPF对比
    线性表(顺序表和链表)
    吴恩达深度学习笔记(一)——神经网络基础、 logistic 回归
    绿盟应急响应团队
    4个宝宝晚上不建议出门的原因,你知道吗?
  • 原文地址:https://blog.csdn.net/u011193452/article/details/126587287