【问题标题】:pass data from one fragment to multiple fragment in sliding tab layout在滑动选项卡布局中将数据从一个片段传递到多个片段
【发布时间】:2019-03-13 09:16:22
【问题描述】:

我在滑动标签布局中有多个片段。我想将数据从第一个选项卡传递到其他选项卡。请提供代码。传递数据选项卡时应从第一个选项卡滑动到第二个选项卡。请帮我。提前谢谢

【问题讨论】:

标签: android


【解决方案1】:

考虑使用 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

  1. 创建 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,并且您想在从第一个选项卡滑动到第二个选项卡时传递“名称”数据

  1. 现在,在 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" 的代码

  1. 现在,当您从第一个选项卡 (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,在传递大量数据时非常有用 从片段到片段。

这是将数据从片段传递到片段的简单方法之一。

希望这会有所帮助,谢谢。

【讨论】:

  • 但我不太了解 kotlin。你会在java中提供它吗
  • 请帮助我。我无法实现
  • 在我的答案中添加了更多详细信息,请查看顶部段落。 @PrithamBnr
  • 我会接受你的回答。我的目标编译 sdk 版本是 26。所以我不能使用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-29
相关资源
最近更新 更多