Android 在滑动选项卡布局中将数据从一个片段传递到多个片段

Android 在滑动选项卡布局中将数据从一个片段传递到多个片段,android,Android,我在滑动选项卡布局中有多个片段。我想将数据从第一个选项卡传递到另一个选项卡。请提供代码。传递数据时,标签应从第一个标签滑动到第二个标签。请帮帮我。提前感谢您考虑使用ViewModel() () 首先,打开项目的ROOT build.gradle文件(不是应用程序或模块的文件),并添加google()存储库,如下所示: allprojects { repositories { google() jcenter() } } 然后,在您的应用程序模块构

我在滑动选项卡布局中有多个片段。我想将数据从第一个选项卡传递到另一个选项卡。请提供代码。传递数据时,标签应从第一个标签滑动到第二个标签。请帮帮我。提前感谢您考虑使用ViewModel() ()

首先,打开项目的ROOT build.gradle文件(不是应用程序或模块的文件),并添加google()存储库,如下所示:

allprojects {
    repositories {
        google()
        jcenter()
    }
}
然后,在您的应用程序模块构建中。gradle只需将此库添加到gradle中,然后您就可以开始在项目中使用ViewModel

implementation "androidx.lifecycle:lifecycle-extensions:2.0.0" // Its Version May Vary

要使用视图模型,请执行以下操作:

  • 创建视图模型类并定义要传递的数据

    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,当您从第一个选项卡滑动到第二个选项卡时,您希望传递“名称”数据

  • 现在,在片段A中,您可以设置视图模型的“名称”数据,然后再将数据传递给片段B

    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"
    }
    }
    
    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=“您想要设置的值”

  • 现在,当您从第一个选项卡(FragmentA)滑动到第二个选项卡(FragmentB)

  • 注意:每当您想要获取ViewModel的“名称”数据时,只需使用
    myViewModel.name的代码即可

    ViewModel不仅可以存储String、Int、Double等数据类型,还可以存储Object,这在从一个片段到另一个片段传递大量数据时非常有用

    这是一种将数据从一个片段传递到另一个片段的简单方法


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

    检查此项检查此项您可以通过界面来完成。可能是@SalmanKhan的复制品我不知道,但我无法理解kotlin。你能提供爪哇语吗?请帮助我。我无法在我的答案中添加更多细节,请查看最上面的段落@我会接受你的回答。我的目标编译sdk版本是26。所以我不能用它