如何使用导航组件将数组从第一个片段发送到第二个片段?
How can I send the array from the first fragment to the second fragment using Navigation Component?
FirstFragment.java
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CategoryModel[] list = {new CategoryModel(1, "Category 1")
, new CategoryModel(2, "Category 2")
, new CategoryModel(3, "Category 3")
, new CategoryModel(4, "Category 4")
, new CategoryModel(5, "Category 5")};
FirstFragmentDirections.actionFirstFragmentToSecondFragment(list);
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
SecondFragment.java
public class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CategoryModel[] list = SecondFragmentArgs.fromBundle(getArguments()).getCategories();
return inflater.inflate(R.layout.fragment_second, container, false);
}
}
CategoryModel.java
public class CategoryModel {
private final int id;
private final String name;
public CategoryModel(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
main_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/main_navigation"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.test.ttt.FirstFragment"
tools:layout="@layout/fragment_first">
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.test.ttt.SecondFragment"
tools:layout="@layout/fragment_second">
<argument
android:name="categories"
app:argType="com.test.ttt.CategoryModel[]" />
</fragment>
</navigation>
我正在尝试将类别从第一个片段发送到第二个片段,但我在 运行 应用
期间遇到此错误
发布这个问题之前仔细搜索过,但没有找到任何解决方案................................................ .
您可以像 Intent 一样在 NavController 的 navigate() 方法中将 bundle 作为第二个参数传递。
使用科特林:
fun Activity.open(destination: Int, params: HashMap<String, Any?>) {
try {
val navController = findNavController(this, id of navigation graph container) // Mine is R.id.fragmentsContainer
navController.navigate(destination, params.toBundle())
} catch (e: Exception) {
e.printStackTrace()
}
}
您可以使用上面的快捷方式进行片段导航,只需传递目标片段 ID & hashmap/replace 它与 bundle)
使用Java:
void open(Integer destination, Bundle bundle) {
try {
NavController navController = Navigation.findNavController(this, id of navigation graph container); // Mine is R.id.fragmentsContainer
navController.navigate(destination, bundle);
} catch (e:Exception){
e.printStackTrace()
}
}
FirstFragment.java
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CategoryModel[] list = {new CategoryModel(1, "Category 1")
, new CategoryModel(2, "Category 2")
, new CategoryModel(3, "Category 3")
, new CategoryModel(4, "Category 4")
, new CategoryModel(5, "Category 5")};
FirstFragmentDirections.actionFirstFragmentToSecondFragment(list);
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
SecondFragment.java
public class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
CategoryModel[] list = SecondFragmentArgs.fromBundle(getArguments()).getCategories();
return inflater.inflate(R.layout.fragment_second, container, false);
}
}
CategoryModel.java
public class CategoryModel {
private final int id;
private final String name;
public CategoryModel(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
main_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/main_navigation"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.test.ttt.FirstFragment"
tools:layout="@layout/fragment_first">
<action
android:id="@+id/action_firstFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.test.ttt.SecondFragment"
tools:layout="@layout/fragment_second">
<argument
android:name="categories"
app:argType="com.test.ttt.CategoryModel[]" />
</fragment>
</navigation>
我正在尝试将类别从第一个片段发送到第二个片段,但我在 运行 应用
期间遇到此错误发布这个问题之前仔细搜索过,但没有找到任何解决方案................................................ .
您可以像 Intent 一样在 NavController 的 navigate() 方法中将 bundle 作为第二个参数传递。
使用科特林:
fun Activity.open(destination: Int, params: HashMap<String, Any?>) {
try {
val navController = findNavController(this, id of navigation graph container) // Mine is R.id.fragmentsContainer
navController.navigate(destination, params.toBundle())
} catch (e: Exception) {
e.printStackTrace()
}
}
您可以使用上面的快捷方式进行片段导航,只需传递目标片段 ID & hashmap/replace 它与 bundle)
使用Java:
void open(Integer destination, Bundle bundle) {
try {
NavController navController = Navigation.findNavController(this, id of navigation graph container); // Mine is R.id.fragmentsContainer
navController.navigate(destination, bundle);
} catch (e:Exception){
e.printStackTrace()
}
}