如何将参数传递给视图模型



我需要从片段中的参数中获取参数,并将其提供给视图模型,以便它根据它过滤列表。我认为可以将这个参数传递给视图模型的工厂,并从那里传递给视图模式本身的构造函数,因此它将具有val-onlyFavorites,并且可以立即在init块中使用。

ContentFragment.kt

class ContentFragment : Fragment(), ItemFavoriteClickListener {
private val viewModel: ContentViewModel by viewModels(factoryProducer = {
ContentViewModel.Factory()
})
private var adapter: MyItemModelsAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.getBoolean("onlyFavorites", false)

viewModel.items.observe(this) {
adapter?.items = it
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerView?.layoutManager = LinearLayoutManager(context)
adapter = MyItemModelsAdapter(this)
recyclerView?.adapter = adapter
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_favourites, container, false)
}
override fun onFavoriteClick(item: ItemModel, isFavorite: Boolean) {
viewModel.changeFavoriteState(item, isFavorite)
}
companion object {
fun newInstance(onlyFavorites: Boolean): ContentFragment {
val contentFragment = ContentFragment()
val args = Bundle()
args.putBoolean("onlyFavorites", onlyFavorites)
contentFragment.arguments = args
return contentFragment
}
}
}

ContentViewModel.kt

class ContentViewModel(
private val repository: MyItemsRepository
) : ViewModel() {
class Factory : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return ContentViewModel(MyItemsRepositoryImpl.getInstance()) as T
}
}
private val _items: MutableLiveData<List<ItemModel>> = MutableLiveData()
val items: LiveData<List<ItemModel>>
get() = _items
fun changeFavoriteState(item: ItemModel, favorite: Boolean) {
repository.setFavorite(item, favorite)
}
init {
_items.value = repository.items.filter { it.isFavorite }
repository.addItemChangeListener {
_items.value = repository.items.filter { it.isFavorite }
}
}
}

我需要你帮忙写代码。我在心里明白该怎么做,但我不知道该怎么写。如果能给我一个带解释的书面例子,我将不胜感激。

这并不像看上去那么难;只需在ContentViewModel 中创建一个新的公共变量

var isFavorite: Boolean = false

然后,将获取逻辑放入一个方法中,而不是ViewModel的init。

fun getItems() {
_items.value = repository.items.filter { it.isFavorite == isFavorite }
}

现在在fragment的onViewCreated((中调用这个方法,如下所示:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerView?.layoutManager = LinearLayoutManager(context)
adapter = MyItemModelsAdapter(this)
recyclerView?.adapter = adapter
val isFavorite = arguments?.getBoolean("onlyFavorites", false) ?: false
viewModel.isFavorite = isFavorite
viewModel.getItems()

viewModel.items.observe(this) {
adapter?.items = it
}
}

这样,在实际调用getItems方法之前,您就有了isFavorite的值。

我还没有测试过这个代码,但我确信它会起作用。

如果您需要更多帮助,请留下评论。

相关内容

  • 没有找到相关文章

最新更新