在一个“详情”片段中有两个ViewModel。

huangapple go评论120阅读模式
英文:

Two ViewModels in one "details" fragment

问题

我有三个片段,第一个是ListFragment,它是一个包含了 Firebase 集合中所有文档的 Recyclerview,第二个是FavoritesFragment,它是用户从第一个Recyclerview中选择的收藏项的Recyclerview。第三个是DetailsFragment,用于显示从这两个Recyclerview中的其中一个点击的项目的详细信息。我在应用程序中使用了导航组件和 MVVM 架构。

我的问题是:我是否可以在DetailsFragment中的onActivityCreated方法中添加两个 ViewModel,一个用于 BlockListViewModel,另一个用于 BlockListFavViewModel,以便根据需要获取来自 Firebase 的适当的 RecyclerView 数据位置?因为使用底部所示的代码,如果用户是从 ListFragment 来的,我可以得到正确的位置,但如果用户是从 FavoritesFragment 来的,位置就会错误。

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    blockListViewModel = new ViewModelProvider(getActivity()).get(BlockListViewModel.class);
    blockListViewModel.getBlockListModelData().observe(getViewLifecycleOwner(), new Observer<List<BlockListModel>>() {
        @Override
        public void onChanged(List<BlockListModel> blockListModels) {

            Glide.with(getContext())
                    .load(blockListModels.get(position).getImage())
                    .centerCrop()
                    .placeholder(R.drawable.placeholder_image)
                    .into(detailsImage);

            matTXT.setText(blockListModels.get(position).getMat());
            finishedTXT.setText(blockListModels.get(position).getFinished());
            cutTXT.setText(blockListModels.get(position).getCut());
            assembleTXT.setText(blockListModels.get(position).getAssemble());

            blockId = blockListModels.get(position).getBlock_id();
            blockTitle = blockListModels.get(position).getName();
        }
    });
}
英文:

I have three fragments, the first ListFragment it's a Recyclerview contains all documents of firebase collection, the second FavoritesFragment it's Recyclerview of favorites items chosen by the user from the first Recyclerview. and the third DetailsFragment that shows the details of the item that clicked from one of this two recyclerviews. i use navigation component in my app, and MVVM architecture.

My question is: can i add two viewmodels, one for BlockListViewModel and one for BlockListFavViewModel to DetailsFragment in onActivityCreated to get the appropriate position of recyclerview data from firebase, because with this code at the bottom i can get the correct position if the user comes from the ListFragment, and wrong if user comes from FavoritesFragment.

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        blockListViewModel = new ViewModelProvider(getActivity()).get(BlockListViewModel.class);
        blockListViewModel.getBlockListModelData().observe(getViewLifecycleOwner(), new Observer&lt;List&lt;BlockListModel&gt;&gt;() {
            @Override
            public void onChanged(List&lt;BlockListModel&gt; blockListModels) {

                Glide.with(getContext())
                        .load(blockListModels.get(position).getImage())
                        .centerCrop()
                        .placeholder(R.drawable.placeholder_image)
                        .into(detailsImage);

                matTXT.setText(blockListModels.get(position).getMat());
                finishedTXT.setText(blockListModels.get(position).getFinished());
                cutTXT.setText(blockListModels.get(position).getCut());
                assembleTXT.setText(blockListModels.get(position).getAssemble());

                blockId = blockListModels.get(position).getBlock_id();
                blockTitle = blockListModels.get(position).getName();
            }
        });
    }

答案1

得分: 0

不需要使用两个ViewModel,如果您的项目要求使用两个ViewModel。然后,您可以通过从ListFragment和FavoritesFragment传递变量来在详细Fragment中分别使用ViewModel。
如果从ListFragment打开片段,请将类名作为TAG发送,对于FavoritesFragment也是如此,在detailFragment中检查该变量,
根据该变量使用ViewModel。

英文:

There is no need of use two viewmodel, if this is requirement of you're project to use to view model.
Then you can seperate use of viewmodel in detail fragment by passing veriable from ListFragment and FavoritesFragment,
If fragment open from ListFragment then send classname as TAG, same for FavoritesFragment, and in detailFragment check that veriable,
on the bases of that variable use viewmodel.

答案2

得分: 0

以下是你的代码翻译部分:

从ListFragment调用DetailFragment:

DetailFragment detailFrm = new DetailFragment();
Bundle args = new Bundle();
args.putString("OpenFrom", "ListFragment");
detailFrm.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, detailFrm).commit();

从FavoritesFragment调用DetailFragment:

DetailFragment detailFrm = new DetailFragment();
Bundle args = new Bundle();
args.putString("OpenFrom", "FavoritesFragment");
detailFrm.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, detailFrm).commit();

在DetailFragment中:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String value = getArguments().getString("OpenFrom");
    if (value.equals("ListFragment")) {
    
        // 使用第一个ViewModel
        blockListViewModel = new ViewModelProvider(getActivity()).get(BlockListViewModel.class);
        blockListViewModel.getBlockListModelData().observe(getViewLifecycleOwner(), new Observer<List<BlockListModel>>() {
            @Override
            public void onChanged(List<BlockListModel> blockListModels) {

                Glide.with(getContext())
                        .load(blockListModels.get(position).getImage())
                        .centerCrop()
                        .placeholder(R.drawable.placeholder_image)
                        .into(detailsImage);

                matTXT.setText(blockListModels.get(position).getMat());
                finishedTXT.setText(blockListModels.get(position).getFinished());
                cutTXT.setText(blockListModels.get(position).getCut());
                assembleTXT.setText(blockListModels.get(position).getAssemble());

                blockId = blockListModels.get(position).getBlock_id();
                blockTitle = blockListModels.get(position).getName();
            }
        });
    } else if (value.equals("FavoritesFragment")) {
    
        // 使用其他ViewModel
    }
}

如果你有任何疑问,可以问我。

英文:

Here I'm sharing coding part how you implement in your code

Call DetailFragment From ListFragment :

DetailFragment detailFrm = new DetailFragment();
Bundle args = new Bundle();
args.putString(&quot;OpenFrom&quot;, &quot;ListFragment&quot;);
detailFrm.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, detailFrm).commit();

Call DetailFragment From FavoritesFragment :

DetailFragment detailFrm = new DetailFragment();
Bundle args = new Bundle();
args.putString(&quot;OpenFrom&quot;, &quot;FavoritesFragment&quot;);
detailFrm.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.container, detailFrm).commit();

In DetailFragment

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String value = getArguments().getString(&quot;OpenFrom&quot;);
if(value.equals(&quot;ListFragment&quot;){
//use First Viewmodel
blockListViewModel = new ViewModelProvider(getActivity()).get(BlockListViewModel.class);
blockListViewModel.getBlockListModelData().observe(getViewLifecycleOwner(), new Observer&lt;List&lt;BlockListModel&gt;&gt;() {
@Override
public void onChanged(List&lt;BlockListModel&gt; blockListModels) {
Glide.with(getContext())
.load(blockListModels.get(position).getImage())
.centerCrop()
.placeholder(R.drawable.placeholder_image)
.into(detailsImage);
matTXT.setText(blockListModels.get(position).getMat());
finishedTXT.setText(blockListModels.get(position).getFinished());
cutTXT.setText(blockListModels.get(position).getCut());
assembleTXT.setText(blockListModels.get(position).getAssemble());
blockId = blockListModels.get(position).getBlock_id();
blockTitle = blockListModels.get(position).getName();
}
});
}else if(value.equals(&quot;FavoritesFragment&quot;){
//use Other Viewmodel		
}
}

If you have any query you can ask me.

huangapple
  • 本文由 发表于 2020年6月29日 13:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/62631628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定