如何从另一个活动与适配器进行通信?
我在属于活动 A 的片段内有一个适配器,从该适配器我打开活动 B 中的元素的详细信息。我如何告诉活动 A 的适配器它必须更新该适配器的列表在活动 B 的片段中?
这是我在活动中使用的方法:
private fun setOnClickFavoriteListener(recipe: RecipesItem?) {
binding?.recipeDetailContainerFavoriteButton?.setOnClickListener {
if (recipe?.isFavorite == true) {
isFavorite = false
viewModel.setFavoriteRecipe(recipe.id, isFavorite)
binding?.recipeDetailImgFavoriteButton?.setColorFilter(
ContextCompat.getColor(
this,
R.color.silver
)
)
} else {
isFavorite = true
viewModel.setFavoriteRecipe(recipe?.id, isFavorite)
binding?.recipeDetailImgFavoriteButton?.setColorFilter(
ContextCompat.getColor(
this,
R.color.sunglo
)
)
}
}
}
这是我的适配器,它不在同一个活动中,所以它们之间没有通信:
class FavoritesAdapter(private val favorites: List<RecipesItem>) :
RecyclerView.Adapter<FavoritesViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FavoritesViewHolder {
val binding = FavoritesRowBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return FavoritesViewHolder(binding)
}
override fun onBindViewHolder(holder: FavoritesViewHolder, position: Int) {
holder.bind(favorites[position])
}
override fun getItemCount(): Int {
return favorites.size
}
}
class FavoritesViewHolder(private val binding: FavoritesRowBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(recipesItem: RecipesItem) {
binding.favoritesRowImgRecipeImage.load(recipesItem.imageURL)
binding.favoritesRowLabelRecipeName.text = recipesItem.name
binding.favoritesRowLabelTime.text = binding.root.context.getString(
R.string.recipe_time,
recipesItem.time
)
binding.favoritesRowLabelDifficult.text = recipesItem.difficult
binding.root.setOnClickListener {
val bundle = bundleOf(RECIPE_BUNDLE to recipesItem)
val intent = Intent(binding.root.context, RecipeDetailActivity::class.java)
intent.putExtras(bundle)
binding.root.context.startActivity(intent)
}
}
}
我尝试将收藏夹列表的获取放在 onResume 中,但它做了一个奇怪的事情效果,它简要显示空列表图像,然后显示更新的列表:
@AndroidEntryPoint
class FavoritesFragment : Fragment() {
private val viewModel: FavoritesViewModel by viewModels()
private var binding: FragmentFavoritesBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentFavoritesBinding.inflate(layoutInflater, container, false)
return binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(activity as DashboardActivity).showToolbar(true)
(activity as DashboardActivity).setToolbarTitle(getString(R.string.option_menu_favorite))
}
private fun initObservers() {
viewModel.favoritesLiveData.observe(viewLifecycleOwner) { favorites ->
if (favorites.isNullOrEmpty()) {
binding?.favoritesRecyclerViewRecipes?.hide()
binding?.favoritesGroupEmptyRecipes?.show()
} else {
binding?.favoritesRecyclerViewRecipes?.show()
binding?.favoritesGroupEmptyRecipes?.hide()
val adapter = FavoritesAdapter(favorites)
binding?.favoritesRecyclerViewRecipes?.layoutManager = LinearLayoutManager(
context,
LinearLayoutManager.VERTICAL,
false
)
binding?.favoritesRecyclerViewRecipes?.adapter = adapter
}
}
}
override fun onResume() {
super.onResume()
viewModel.getFavoritesFromDatabase()
initObservers()
}
override fun onDestroy() {
binding = null
super.onDestroy()
}
I have an adapter inside a fragment that belongs to an Activity A, from this adapter I open the detail of an element in an Activity B. How can I tell the adapter of Activity A that it has to update the list of the adapter that is in a fragment of Activity B?
This is the method I have inside the activity:
private fun setOnClickFavoriteListener(recipe: RecipesItem?) {
binding?.recipeDetailContainerFavoriteButton?.setOnClickListener {
if (recipe?.isFavorite == true) {
isFavorite = false
viewModel.setFavoriteRecipe(recipe.id, isFavorite)
binding?.recipeDetailImgFavoriteButton?.setColorFilter(
ContextCompat.getColor(
this,
R.color.silver
)
)
} else {
isFavorite = true
viewModel.setFavoriteRecipe(recipe?.id, isFavorite)
binding?.recipeDetailImgFavoriteButton?.setColorFilter(
ContextCompat.getColor(
this,
R.color.sunglo
)
)
}
}
}
And this is my adapter which is not in the same activity, so I have no communication between them:
class FavoritesAdapter(private val favorites: List<RecipesItem>) :
RecyclerView.Adapter<FavoritesViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FavoritesViewHolder {
val binding = FavoritesRowBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return FavoritesViewHolder(binding)
}
override fun onBindViewHolder(holder: FavoritesViewHolder, position: Int) {
holder.bind(favorites[position])
}
override fun getItemCount(): Int {
return favorites.size
}
}
class FavoritesViewHolder(private val binding: FavoritesRowBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(recipesItem: RecipesItem) {
binding.favoritesRowImgRecipeImage.load(recipesItem.imageURL)
binding.favoritesRowLabelRecipeName.text = recipesItem.name
binding.favoritesRowLabelTime.text = binding.root.context.getString(
R.string.recipe_time,
recipesItem.time
)
binding.favoritesRowLabelDifficult.text = recipesItem.difficult
binding.root.setOnClickListener {
val bundle = bundleOf(RECIPE_BUNDLE to recipesItem)
val intent = Intent(binding.root.context, RecipeDetailActivity::class.java)
intent.putExtras(bundle)
binding.root.context.startActivity(intent)
}
}
}
I tried to put the fetching of the favourites list in the onResume, but it does a strange effect where it briefly shows the empty list image and then shows the updated list:
@AndroidEntryPoint
class FavoritesFragment : Fragment() {
private val viewModel: FavoritesViewModel by viewModels()
private var binding: FragmentFavoritesBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentFavoritesBinding.inflate(layoutInflater, container, false)
return binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(activity as DashboardActivity).showToolbar(true)
(activity as DashboardActivity).setToolbarTitle(getString(R.string.option_menu_favorite))
}
private fun initObservers() {
viewModel.favoritesLiveData.observe(viewLifecycleOwner) { favorites ->
if (favorites.isNullOrEmpty()) {
binding?.favoritesRecyclerViewRecipes?.hide()
binding?.favoritesGroupEmptyRecipes?.show()
} else {
binding?.favoritesRecyclerViewRecipes?.show()
binding?.favoritesGroupEmptyRecipes?.hide()
val adapter = FavoritesAdapter(favorites)
binding?.favoritesRecyclerViewRecipes?.layoutManager = LinearLayoutManager(
context,
LinearLayoutManager.VERTICAL,
false
)
binding?.favoritesRecyclerViewRecipes?.adapter = adapter
}
}
}
override fun onResume() {
super.onResume()
viewModel.getFavoritesFromDatabase()
initObservers()
}
override fun onDestroy() {
binding = null
super.onDestroy()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从活动“A”片段中的适配器中,您可以像以前一样发送意图包中的项目。
然后,您可以从活动“B”处理该捆绑包并将该项目发送到活动“B”中的片段,然后该片段将在其适配器中呈现该项目。
我希望我正确理解你的问题
From The adapter in Fragment of Activity 'A' you send the Item in the intent bundle as you did.
Then from Activity 'B' you handle this bundle and send the item to the Fragment in Activity 'B' then the fragment will render the item in its adapter.
I hope I understand your question right