在片段之间共享数据



我正在安卓系统上制作一个错误跟踪器应用程序,其中我有一个名为Task的类,它有一个字段"category",用于定义任务是否已完成、正在进行或必须执行。

我有一个想法片段"项目",我在其中添加了设置ViewModel

public class ProjectFragment extends Fragment {
private static final String TAG = "ProjectFragment";
private UserViewModel model;
private GridViewAdapter gridViewAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_projects, container, false);
model = ViewModelProviders.of(requireActivity()).get(UserViewModel.class);
GridView gridView = view.findViewById(R.id.gridview_project);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Project temp = Objects.requireNonNull(model.getCurrentuser().getValue()).getProjects().get(position);
model.setCurrproject(temp);
Log.i(TAG, "onItemClick: " + temp.getProjectName());
model.setTodoTasks(TaskSorter.getSortedTaskList(temp, "TODO"));
model.setInprogressTasks(TaskSorter.getSortedTaskList(temp, "IN PROGRESS"));
model.setDoneTasks(TaskSorter.getSortedTaskList(temp, "DONE"));
}
});
gridViewAdapter = new GridViewAdapter(requireActivity(), Objects.requireNonNull(model.getCurrentuser().getValue()).getProjects());
gridView.setAdapter(gridViewAdapter);
model.getCurrentuser().observe(this, new Observer<User>() {
@Override
public void onChanged(User user) {
gridViewAdapter.notifyDataSetChanged();
}
});
FloatingActionButton floatingActionButton = view.findViewById(R.id.floatingActionButton);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openProjectDialog();
}
});
return view;
}

我还有3个片段,用于我观察变化的相应类别。

public class FragmentToDo extends Fragment {
private static final String TAG = "FragmentToDo";
private ListView listView;
private UserViewModel model;
private ListViewAdapter listViewAdaptertodo;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_todo, container, false);
listView = view.findViewById(R.id.todo_list);
model = ViewModelProviders.of(this).get(UserViewModel.class);
model.gettodoTasks().observe(this, new Observer<ArrayList<Task>>() {
@Override
public void onChanged(ArrayList<Task> tasks) {
listViewAdaptertodo.notifyDataSetChanged();
Log.i(TAG, "onChanged: todo");
}
});
initTodo();
return view;
}
private void initTodo(){
listViewAdaptertodo = new ListViewAdapter(requireActivity(), model.gettodoTasks().getValue());
listView.setAdapter(listViewAdaptertodo);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
openTask();
}
});
}

所有的3个片段看起来都是这样的,当然有不同的名字。每当我点击一个项目时,就会发生一些变化,但并不是所有的变化。这三个类别由ViewPager持有,但我不确定问题出在哪里

public class TaskFragment extends Fragment {
private static SectionsPagerAdapter sectionsPagerAdapter;
private ArrayList<Task> todo;
private ArrayList<Task> inp;
private ArrayList<Task> done;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_task, container, false);
sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
sectionsPagerAdapter.addFragments(new FragmentToDo(), "To Do");
sectionsPagerAdapter.addFragments(new FragmentInProgress(), "In Progress");
sectionsPagerAdapter.addFragments(new FragmentDone(), "Done");
ViewPager viewPager = view.findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = view.findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
return view;
}

}

也许问题是每当我定义上下文和生命周期所有者时,因为我是一个新手安卓开发人员,我对这些主题仍然知之甚少。

有一种简单且不可扩展的方式,比如eventbus。

首选方式-将应用程序逻辑从片段移动到其他组件,数据仅以1种方式传递。也就是说,有一个组件,它暴露了你屏幕的某种模型,得到了更新——片段/视图/活动反映了这种变化。你可以在这里查看示例应用程序

相关内容

  • 没有找到相关文章

最新更新