【发布时间】:2019-03-13 09:16:22
【问题描述】:
我在滑动标签布局中有多个片段。我想将数据从第一个选项卡传递到其他选项卡。请提供代码。传递数据选项卡时应从第一个选项卡滑动到第二个选项卡。请帮我。提前谢谢
【问题讨论】:
-
你可以用一个界面来做。 devexchanges.info/2015/02/…
-
@SalmanKhan 我没有得到
标签: android
我在滑动标签布局中有多个片段。我想将数据从第一个选项卡传递到其他选项卡。请提供代码。传递数据选项卡时应从第一个选项卡滑动到第二个选项卡。请帮我。提前谢谢
【问题讨论】:
标签: android
考虑使用 ViewModel (https://developer.android.com/topic/libraries/architecture/viewmodel) (https://developer.android.com/topic/libraries/architecture/adding-components)
首先,打开您的项目的 ROOT build.gradle 文件(不是您的应用或模块的文件)并添加 google() 存储库,如下所示:
allprojects {
repositories {
google()
jcenter()
}
}
然后, 在您的 APP MODULE build.gradle 中只需将此库添加到您的 Gradle,然后您就可以开始在您的项目中使用 ViewModel :
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0" // Its Version May Vary
使用 ViewModel :
创建 ViewModel 类并定义您想要传递的数据
public class MyViewModel extends ViewModel {
// Assume you wanted to Pass the Data of 'name' from 1st Tab to 2nd Tab
String name = "";
void resetData() { // Function that will Reset the Data
name= "";
}
}
假设第一个选项卡是 FragmentA,第二个选项卡是 FragmentB,并且您想在从第一个选项卡滑动到第二个选项卡时传递“名称”数据
现在,在 FragmentA 中,您可以在将数据传递给 FragmentB 之前设置 ViewModel 的“名称”数据强>
public class FragmentA extends Fragment {
private MyViewModel myViewModel ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// MUST Initialize your ViewModel
myViewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
}
protected void onStart() {
super.onStart();
// You can Set your Value anywhere, Let Set the Value at onStart as an Example
// Set your Name Data to "Pritham Bnr"
myViewModel.name= "Pritham Bnr"
}
}
注意:当你想设置 ViewModel 的“名称”数据时,只需使用 myViewModel.name= "Value you want to Set" 的代码
现在,当您从第一个选项卡 (FragmentA) 滑动到第二个选项卡 (FragmentB)
时,您可以从 FragmentB 中的 ViewModel 获取“名称”数据public class FragmentB extends Fragment {
private MyViewModel myViewModel ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// MUST Initialize your ViewModel
myViewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
}
protected void onStart() {
super.onStart();
// You can Get your Value anywhere by call "myViewModel.name", Let Get the Value at onStart as an Example
// Using Toast to Display the 'name' Data pass from FragmentA using ViewModel
Toast.makeText(getContext(), myViewModel.name, Toast.LENGTH_SHORT).show();
}
// for Example, If you want Clear the Data when Swipe Back to FragmentA, you can call resetData() function of the ViewModel
// Let say we Clear the Data when the Fragment onStop() as you Swipe back to FragmentA
// This is Optional, just an Example Telling you how to Reset the Data if you want to
protected void onStop() {
super.onStop();
// Reset Data when Screen is Being Swipe to FragmentA
// After Call this function, the ViewModel previous Data of "Pritham Bnr" will be Reset and become "" empty value.
// So FragmentA now will get "" Data from the ViewModel
myViewModel.destroyViewModelData();
}
}
注意:当你想获取 ViewModel 的“名称”数据时,只需使用代码myViewModel.name
ViewModel不仅可以存储String、Int、Double等DataType,还可以存储Object,在传递大量数据时非常有用 从片段到片段。
这是将数据从片段传递到片段的简单方法之一。
希望这会有所帮助,谢谢。
【讨论】: