gpt4 book ai didi

android - 从另一个 fragment 的适配器刷新 RecyclerView fragment

转载 作者:行者123 更新时间:2023-12-02 17:09:17 25 4
gpt4 key购买 nike

我正在制作一个连接到 WordPress 的 Android 电子商务应用程序。在我的应用程序中,我有两个 fragment :商店 fragment (其中包含从网站获取的产品)和我的愿望 list fragment (包含愿望 list 产品在里面)。我还有两个列表:一个用于检索到的产品,另一个用于愿望 list 产品。愿望 list 产品列表包含检索到的产品列表中的每个产品的索引,因此我可以稍后获得完整的详细信息(图像、描述等...)。

我面临的问题是在将产品添加到心愿单时,我希望能够点击商店 fragment 中的心愿单按钮,心愿单产品将显示在我的心愿单中 fragment 。我在网上搜索了很多,发现我应该使用 notifyDataSetChanged 但我没有找到任何关于如何从其他 fragment 调用它的示例。

如果我理解正确,我应该从 ShopProductAdapter 调用 WishlistProductAdapter 但我是 Android 开发的初学者所以我不确定我是否可以拉这个自己下车。

我相信这段位于 MyWishlist.java( fragment )中的代码可以帮助:

//getting the recyclerview from xml
RecyclerView recyclerView = (RecyclerView) getView().findViewById(R.id.wishlist_products);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(getResources().getInteger(R.integer.products_column_count), StaggeredGridLayoutManager.VERTICAL));

JSONArray shop_products = //fetched from website, done in MainActivity;
ArrayList<String> wishlist_products = ["3","0"]; // It contains product of index 0 and product of index 3 inside shop_products
//initializing the productlist
List<Product> productList = new ArrayList<>();

for (int i=0; i<wishlist_products.size(); i++) {
String index = wishlist_products.get(i);
JSONObject JO = (JSONObject) shop_products.get(Integer.parseInt(index));

productList.add(
new Product(
i, //index
Integer.parseInt(JO.get("id").toString()), //id
JO.get("title").toString(), //title
null,
null,
JO.get("price").toString(), //price
JO.get("featured_image_url").toString(), //image_url
null
)
);
}
//creating recyclerview adapter
adapter = new WishlistProductAdapter(getActivity().getApplicationContext(), productList);

//setting adapter to recyclerview
recyclerView.setAdapter(adapter);

编辑:

Shop.java除了for循环( fragment )是一样的:

for (int i=0; i<shop_products.length(); i++) {
JSONObject JO = (JSONObject) shop_products.get(i);

productList.add(
new Product(
i,
Integer.parseInt(JO.get("id").toString()),
JO.get("title").toString(),
null,
null,
JO.get("price").toString(),
JO.get("featured_image_url").toString(),
null
)
);
}

编辑:

我的 fragment 、适配器和 MainActivity 代码在这里:https://ufile.io/25z9e

非常感谢任何帮助!

最佳答案

fragment 应该通过父 Activity 相互通信。两个 Fragment 不应该直接通信。

设 A 是您要从中调用 fragment B 中适配器的 notifyDataSetChanged() 的 fragment 。

因此,您可以做的是在 fragment A 中创建一个接口(interface)。在 Activity 中实现该接口(interface)。单击按钮时,调用接口(interface)的方法。在实现此接口(interface)的 Activity 中,您可以获得对 fragment B 的引用。您可以在 fragment B 中创建一个方法,您可以在该方法中调用适配器上的 notifyDataSetChanged()。

上面粗略的代码是怎么做的-

在 fragment B中创建一个方法

    public void refreshAdapter(){
adapter.notifyDataSetChanged();
}

在Fragment A中创建一个接口(interface)

   interface RefreshInterface{
public void refreshAdapterFragmentB();
}

ParentActivity 实现了 RefreshInterface,您需要定义 refreshAdapterFramgentB() 方法的实现,而 fragmentB 是对您在 Activity 中的 fragment B 的引用。您可以像这样调用 fragment B 的 refreshAdapter() 方法

    @Override 
public void refreshAdapterFragmentB(){
fragmentB.refreshAdapter();
}

关于android - 从另一个 fragment 的适配器刷新 RecyclerView fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50115471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com