我目前正在开展一个项目,要求我在标签/片段之间移动我的数据。让我们说用户点击listView项目,他们将移动到另一个标签,而不是保持相同标签。我可以知道如何实现这一目标吗?有人可以帮忙解决我的疑问吗?谢谢!
答案 0 :(得分:0)
您可以通过set和get Arguments在选项卡之间传输数据, 这是示例
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();
答案 1 :(得分:0)
有三种方法可以做到这一点
1)使用接口 -use接口传递数据对象。网状解决方案
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存储库对象查看更新模型。整个App之间的单个数据流副本。
@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);
}
}