【问题标题】:Android Studio - data moving within tabs/fragmentsAndroid Studio - 数据在选项卡/片段中移动
【发布时间】:2016-07-27 12:41:56
【问题描述】:

我目前正在做一个需要我在选项卡/片段之间移动数据的项目。假设用户单击 listView 项目,他们将移动到另一个选项卡,而不是停留在同一个选项卡中。我可以知道我怎样才能做到这一点吗?有人可以帮助解决我的查询吗?谢谢!

【问题讨论】:

    标签: android-layout listview android-studio android-fragments


    【解决方案1】:

    您可以通过设置和获取参数在选项卡之间传输数据, 这是示例

    FragmentTwo fragmentTwo = new FragmentTwo();
    
    Bundle bundle = new Bundle();
    bundle.putString("key1", "data1");
    bundle.putString("key2", "data2");
    bundle.putString("key3", "data3");
    fragmentTwo.setArguments(bundle);
    
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_container, fragmentTwo);
    fragmentTransaction.commit();
    

    【讨论】:

      【解决方案2】:

      有 3 种方法可以做到这一点

      1) 使用接口 - 使用接口传递数据对象。网状解决方案

      Read more

      public interface onDataCHange{ 
          public void updateData(String data);
      } 
      

      2) 使用活动类 - 将模型对象存储在活动类中,并使用活动实例设置和获取。快速而肮脏的解决方案

      Read more

        //Get
        Object dataModel =  (ContainerActivity) getActivity()).getData();
      
         //Set
         ((ContainerActivity) getActivity()).setData(dataModel );
      

      3) 简洁架构 - 中心存储库保存模型对象。通过 Singleton Center 存储库对象查看更新模型。整个应用程序之间数据流的单一副本。

      Read more

      @Singleton 
      public class UserDataRepository implements UserRepository { 
      
        private final UserDataStoreFactory userDataStoreFactory;
        private final UserEntityDataMapper userEntityDataMapper;
      
        /** 
         * Constructs a {@link UserRepository}. 
         * 
         * @param dataStoreFactory A factory to construct different data source implementations. 
         * @param userEntityDataMapper {@link UserEntityDataMapper}. 
         */ 
        @Inject 
        UserDataRepository(UserDataStoreFactory dataStoreFactory,
            UserEntityDataMapper userEntityDataMapper) {
          this.userDataStoreFactory = dataStoreFactory;
          this.userEntityDataMapper = userEntityDataMapper;
        } 
      
        @Override public Observable<List<User>> users() {
          //we always get all users from the cloud 
          final UserDataStore userDataStore = this.userDataStoreFactory.createCloudDataStore();
          return userDataStore.userEntityList().map(this.userEntityDataMapper::transform);
        } 
      
        @Override public Observable<User> user(int userId) {
          final UserDataStore userDataStore = this.userDataStoreFactory.create(userId);
          return userDataStore.userEntityDetails(userId).map(this.userEntityDataMapper::transform);
        } 
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-12
        • 2018-06-24
        • 1970-01-01
        • 2013-11-15
        相关资源
        最近更新 更多