导航在点击时未跳转到下一个片段。

huangapple go评论112阅读模式
英文:

Navigation doesnt proceed to next fragment on click

问题

在按钮点击时,奇怪的是它不转到其他片段。

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. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:orientation="vertical">
  12. <androidx.appcompat.widget.Toolbar
  13. android:id="@+id/toolbar"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:background="?attr/colorPrimary"
  17. android:minHeight="?attr/actionBarSize"
  18. android:theme="?attr/actionBarTheme"
  19. app:logo="@android:drawable/btn_star_big_on" />
  20. <fragment
  21. android:id="@+id/fragment"
  22. android:name="androidx.navigation.fragment.NavHostFragment"
  23. android:layout_width="match_parent"
  24. android:layout_height="match_parent"
  25. app:defaultNavHost="true"
  26. app:navGraph="@navigation/main_nav" />
  27. </LinearLayout>
  28. </androidx.constraintlayout.widget.ConstraintLayout>

fragment_first.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. android:background="@color/colorPrimary"
  8. android:elevation="20dp"
  9. tools:context=".first">
  10. <Button
  11. android:id="@+id/button_first"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:elevation="20dp"
  15. android:text="First"
  16. android:textColor="@color/colorPrimary"
  17. android:textStyle="bold"
  18. app:layout_constraintBottom_toBottomOf="parent"
  19. app:layout_constraintEnd_toEndOf="parent"
  20. app:layout_constraintHorizontal_bias="0.5"
  21. app:layout_constraintStart_toStartOf="parent"
  22. app:layout_constraintTop_toTopOf="parent" />
  23. </androidx.constraintlayout.widget.ConstraintLayout>

fragment_second.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. android:background="#C13584"
  8. tools:context=".second">
  9. <Button
  10. android:id="@+id/button2"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="Second"
  14. android:textColor="#C13584"
  15. android:textStyle="bold"
  16. app:layout_constraintBottom_toBottomOf="parent"
  17. app:layout_constraintEnd_toEndOf="parent"
  18. app:layout_constraintHorizontal_bias="0.5"
  19. app:layout_constraintStart_toStartOf="parent"
  20. app:layout_constraintTop_toTopOf="parent" />
  21. </androidx.constraintlayout.widget.ConstraintLayout>

main_nav.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/main_nav"
  6. app:startDestination="@id/first">
  7. <fragment
  8. android:id="@+id/first"
  9. android:name="com.example.fragments.first"
  10. android:label="fragment_first"
  11. tools:layout="@layout/fragment_first">
  12. <action
  13. android:id="@+id/navigateToSecondFragment"
  14. app:destination="@id/second" />
  15. </fragment>
  16. <fragment
  17. android:id="@+id/second"
  18. android:name="com.example.fragments.second"
  19. android:label="fragment_second"
  20. tools:layout="@layout/fragment_second">
  21. <action
  22. android:id="@+id/navigateToFirstFragment"
  23. app:destination="@id/first" />
  24. </fragment>
  25. </navigation>

first.java:

  1. package com.example.fragments;
  2. import android.os.Bundle;
  3. import androidx.fragment.app.Fragment;
  4. import androidx.navigation.Navigation;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. public class first extends Fragment {
  11. private static final String ARG_PARAM1 = "param1";
  12. private static final String ARG_PARAM2 = "param2";
  13. private String mParam1;
  14. private String mParam2;
  15. private Button button;
  16. public first() {
  17. // Required empty public constructor
  18. }
  19. public static first newInstance(String param1, String param2) {
  20. first fragment = new first();
  21. Bundle args = new Bundle();
  22. args.putString(ARG_PARAM1, param1);
  23. args.putString(ARG_PARAM2, param2);
  24. fragment.setArguments(args);
  25. return fragment;
  26. }
  27. @Override
  28. public void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. if (getArguments() != null) {
  31. mParam1 = getArguments().getString(ARG_PARAM1);
  32. mParam2 = getArguments().getString(ARG_PARAM2);
  33. }
  34. }
  35. @Override
  36. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  37. Bundle savedInstanceState) {
  38. View view = inflater.inflate(R.layout.fragment_first, container, false);
  39. button = (Button) view.findViewById(R.id.button_first);
  40. button.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View view) {
  43. Navigation.findNavController(view).navigate(R.id.navigateToSecondFragment);
  44. }
  45. });
  46. return view;
  47. }
  48. }

second.java:

  1. package com.example.fragments;
  2. import android.os.Bundle;
  3. import androidx.fragment.app.Fragment;
  4. import androidx.navigation.Navigation;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;
  9. public class second extends Fragment {
  10. private static final String ARG_PARAM1 = "param1";
  11. private static final String ARG_PARAM2 = "param2";
  12. private String mParam1;
  13. private String mParam2;
  14. private Button button;
  15. public second() {
  16. // Required empty public constructor
  17. }
  18. public static second newInstance(String param1, String param2) {
  19. second fragment = new second();
  20. Bundle args = new Bundle();
  21. args.putString(ARG_PARAM1, param1);
  22. args.putString(ARG_PARAM2, param2);
  23. fragment.setArguments(args);
  24. return fragment;
  25. }
  26. @Override
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. if (getArguments() != null) {
  30. mParam1 = getArguments().getString(ARG_PARAM
  31. <details>
  32. <summary>英文:</summary>
  33. As the title describes on button click it doesnt go to the other fragment for some odd reason.
  34. activity_main.xml:
  35. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  36. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  37. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  38. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  39. android:layout_width=&quot;match_parent&quot;
  40. android:layout_height=&quot;match_parent&quot;
  41. tools:context=&quot;.MainActivity&quot;&gt;
  42. &lt;LinearLayout
  43. android:layout_width=&quot;match_parent&quot;
  44. android:layout_height=&quot;match_parent&quot;
  45. android:orientation=&quot;vertical&quot;&gt;
  46. &lt;androidx.appcompat.widget.Toolbar
  47. android:id=&quot;@+id/toolbar&quot;
  48. android:layout_width=&quot;match_parent&quot;
  49. android:layout_height=&quot;wrap_content&quot;
  50. android:background=&quot;?attr/colorPrimary&quot;
  51. android:minHeight=&quot;?attr/actionBarSize&quot;
  52. android:theme=&quot;?attr/actionBarTheme&quot;
  53. app:logo=&quot;@android:drawable/btn_star_big_on&quot; /&gt;
  54. &lt;fragment
  55. android:id=&quot;@+id/fragment&quot;
  56. android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
  57. android:layout_width=&quot;match_parent&quot;
  58. android:layout_height=&quot;match_parent&quot;
  59. app:defaultNavHost=&quot;true&quot;
  60. app:navGraph=&quot;@navigation/main_nav&quot; /&gt;
  61. &lt;/LinearLayout&gt;
  62. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  63. fragment_first.xml:
  64. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  65. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  66. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  67. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  68. android:layout_width=&quot;match_parent&quot;
  69. android:layout_height=&quot;match_parent&quot;
  70. android:background=&quot;@color/colorPrimary&quot;
  71. android:elevation=&quot;20dp&quot;
  72. tools:context=&quot;.first&quot;&gt;
  73. &lt;Button
  74. android:id=&quot;@+id/button_first&quot;
  75. android:layout_width=&quot;wrap_content&quot;
  76. android:layout_height=&quot;wrap_content&quot;
  77. android:elevation=&quot;20dp&quot;
  78. android:text=&quot;First&quot;
  79. android:textColor=&quot;@color/colorPrimary&quot;
  80. android:textStyle=&quot;bold&quot;
  81. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  82. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  83. app:layout_constraintHorizontal_bias=&quot;0.5&quot;
  84. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  85. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  86. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  87. fragment_second.xml:
  88. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  89. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  90. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  91. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  92. android:layout_width=&quot;match_parent&quot;
  93. android:layout_height=&quot;match_parent&quot;
  94. android:background=&quot;#C13584&quot;
  95. tools:context=&quot;.second&quot;&gt;
  96. &lt;Button
  97. android:id=&quot;@+id/button2&quot;
  98. android:layout_width=&quot;wrap_content&quot;
  99. android:layout_height=&quot;wrap_content&quot;
  100. android:text=&quot;Second&quot;
  101. android:textColor=&quot;#C13584&quot;
  102. android:textStyle=&quot;bold&quot;
  103. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  104. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  105. app:layout_constraintHorizontal_bias=&quot;0.5&quot;
  106. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  107. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  108. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  109. main_nav.xml:
  110. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  111. &lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  112. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  113. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  114. android:id=&quot;@+id/main_nav&quot;
  115. app:startDestination=&quot;@id/first&quot;&gt;
  116. &lt;fragment
  117. android:id=&quot;@+id/first&quot;
  118. android:name=&quot;com.example.fragments.first&quot;
  119. android:label=&quot;fragment_first&quot;
  120. tools:layout=&quot;@layout/fragment_first&quot; &gt;
  121. &lt;action
  122. android:id=&quot;@+id/navigateToSecondFragment&quot;
  123. app:destination=&quot;@id/second&quot; /&gt;
  124. &lt;/fragment&gt;
  125. &lt;fragment
  126. android:id=&quot;@+id/second&quot;
  127. android:name=&quot;com.example.fragments.second&quot;
  128. android:label=&quot;fragment_second&quot;
  129. tools:layout=&quot;@layout/fragment_second&quot; &gt;
  130. &lt;action
  131. android:id=&quot;@+id/navigateToFirstFragment&quot;
  132. app:destination=&quot;@id/first&quot; /&gt;
  133. &lt;/fragment&gt;
  134. &lt;/navigation&gt;
  135. first.java:
  136. package com.example.fragments;
  137. import android.os.Bundle;
  138. import androidx.fragment.app.Fragment;
  139. import androidx.navigation.Navigation;
  140. import android.util.Log;
  141. import android.view.LayoutInflater;
  142. import android.view.View;
  143. import android.view.ViewGroup;
  144. import android.widget.Button;
  145. /**
  146. * A simple {@link Fragment} subclass.
  147. * Use the {@link first#newInstance} factory method to
  148. * create an instance of this fragment.
  149. */
  150. public class first extends Fragment {
  151. // TODO: Rename parameter arguments, choose names that match
  152. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  153. private static final String ARG_PARAM1 = &quot;param1&quot;;
  154. private static final String ARG_PARAM2 = &quot;param2&quot;;
  155. // TODO: Rename and change types of parameters
  156. private String mParam1;
  157. private String mParam2;
  158. private Button button;
  159. public first() {
  160. // Required empty public constructor
  161. }
  162. /**
  163. * Use this factory method to create a new instance of
  164. * this fragment using the provided parameters.
  165. *
  166. * @param param1 Parameter 1.
  167. * @param param2 Parameter 2.
  168. * @return A new instance of fragment first.
  169. */
  170. // TODO: Rename and change types and number of parameters
  171. public static first newInstance(String param1, String param2) {
  172. first fragment = new first();
  173. Bundle args = new Bundle();
  174. args.putString(ARG_PARAM1, param1);
  175. args.putString(ARG_PARAM2, param2);
  176. fragment.setArguments(args);
  177. return fragment;
  178. }
  179. @Override
  180. public void onCreate(Bundle savedInstanceState) {
  181. super.onCreate(savedInstanceState);
  182. if (getArguments() != null) {
  183. mParam1 = getArguments().getString(ARG_PARAM1);
  184. mParam2 = getArguments().getString(ARG_PARAM2);
  185. }
  186. }
  187. @Override
  188. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  189. Bundle savedInstanceState) {
  190. // Inflate the layout for this fragment
  191. View view = inflater.inflate(R.layout.fragment_first, container, false);
  192. button = (Button) view.findViewById(R.id.button_first);
  193. button.setOnClickListener(new View.OnClickListener(){
  194. @Override
  195. public void onClick(View view) {
  196. //Start second view
  197. //Navigation.findNavController(view).navigate(R.id.);
  198. Navigation.findNavController(view).navigate(R.id.navigateToSecondFragment);
  199. }
  200. });
  201. return view;
  202. }
  203. }
  204. second.java:
  205. package com.example.fragments;
  206. import android.os.Bundle;
  207. import androidx.fragment.app.Fragment;
  208. import androidx.navigation.Navigation;
  209. import android.view.LayoutInflater;
  210. import android.view.View;
  211. import android.view.ViewGroup;
  212. import android.widget.Button;
  213. /**
  214. * A simple {@link Fragment} subclass.
  215. * Use the {@link second#newInstance} factory method to
  216. * create an instance of this fragment.
  217. */
  218. public class second extends Fragment {
  219. // TODO: Rename parameter arguments, choose names that match
  220. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  221. private static final String ARG_PARAM1 = &quot;param1&quot;;
  222. private static final String ARG_PARAM2 = &quot;param2&quot;;
  223. // TODO: Rename and change types of parameters
  224. private String mParam1;
  225. private String mParam2;
  226. private Button button;
  227. public second() {
  228. // Required empty public constructor
  229. }
  230. /**
  231. * Use this factory method to create a new instance of
  232. * this fragment using the provided parameters.
  233. *
  234. * @param param1 Parameter 1.
  235. * @param param2 Parameter 2.
  236. * @return A new instance of fragment second.
  237. */
  238. // TODO: Rename and change types and number of parameters
  239. public static second newInstance(String param1, String param2) {
  240. second fragment = new second();
  241. Bundle args = new Bundle();
  242. args.putString(ARG_PARAM1, param1);
  243. args.putString(ARG_PARAM2, param2);
  244. fragment.setArguments(args);
  245. return fragment;
  246. }
  247. @Override
  248. public void onCreate(Bundle savedInstanceState) {
  249. super.onCreate(savedInstanceState);
  250. if (getArguments() != null) {
  251. mParam1 = getArguments().getString(ARG_PARAM1);
  252. mParam2 = getArguments().getString(ARG_PARAM2);
  253. }
  254. }
  255. @Override
  256. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  257. Bundle savedInstanceState) {
  258. // Inflate the layout for this fragment
  259. View view = inflater.inflate(R.layout.fragment_first, container, false);
  260. button = (Button) view.findViewById(R.id.button_first);
  261. button.setOnClickListener(new View.OnClickListener(){
  262. @Override
  263. public void onClick(View view) {
  264. //Start first view
  265. Navigation.findNavController(view).navigate(R.id.navigateToFirstFragment);
  266. }
  267. });
  268. return view;
  269. }
  270. }
  271. </details>
  272. # 答案1
  273. **得分**: 1
  274. 对于您的第二个片段您正在填充用于第一个片段的布局`R.layout.fragment_first`您的导航可能实际上运行良好我没有发现任何问题),只是两个片段的布局相同因此您不会注意到转换因为没有定义转换动画检查当您点击按钮时是否导航栏的标题会更改fragment_firstfragment_second”,如标签中所定义的那样),如果是则问题不在于导航
  275. <details>
  276. <summary>英文:</summary>
  277. For your `second` fragment, you are inflating layout that is meant to be for the `first` fragment, i.e. `R.layout.fragment_first`. Your navigation may actually be working fine (I did not find any issue), it is just that the layout for both fragments are the same, so you would not notice the transition, as no transition animations were defined. Check if the navigationBar&#39;s title changes when you click the button (from `fragment_first` to `fragment_second`, as defined by the labels), if yes, then the issue is not in navigation.
  278. </details>

huangapple
  • 本文由 发表于 2020年8月18日 14:32:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63463038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定