从RecyclerView适配器更新“活动"或“片段"的视图 [英] Updating views of Activity or Fragment from a RecyclerView adapter

查看:148
本文介绍了从RecyclerView适配器更新“活动"或“片段"的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新活动中位于TextView中的文本,以显示从RecyclerView中删除项目时的总价.但是,如何从适配器更新属于活动的视图?

I am trying to update the text in a TextView which is located in the activity to show the total price when an item is deleted from RecyclerView. But how can I update a view which belongs to activity from an adapter?

推荐答案

这是解决方案.

  1. 创建一个名为 ItemsInteractionListener 的公共接口,该接口具有一个方法 void onTotalPriceChanged(double newPrice); 适配器内

  1. Create a public interface called ItemsInteractionListener which has a method void onTotalPriceChanged(double newPrice); inside adapter

在适配器内部创建名为 mListener 的接口的对象

Create an object of the interface called mListener inside the adapter

mListener

创建一个名为 double getTotalPrice()的私有方法,该方法从列表中计算总价格.

Create a private method called double getTotalPrice() which calculates total price from the list.

在活动中实施 ItemsInteractionListener .在 void onTotalPriceChanged(double newPrice); 内,将新价格设置为TextView.

Implement ItemsInteractionListener in activity. Inside void onTotalPriceChanged(double newPrice);, set the new price to the TextView.

创建适配器后,通过调用在步骤3之前创建的设置器,将侦听器设置为 this .

After creating adapter, set the listener to this by calling the setter you created before in step 3.

在适配器内部调用 mListener.onTotalPriceChanged(newPrice); .即添加或删除项目时.

Call mListener.onTotalPriceChanged(newPrice); inside adapter whenever a change is made. ie, when adding or deleting items.

这篇关于从RecyclerView适配器更新“活动"或“片段"的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆