HarmonyOS 鸿蒙Next PageSliderProvider调用notifyDataChanged不会刷新数据
HarmonyOS 鸿蒙Next PageSliderProvider调用notifyDataChanged不会刷新数据
为啥PageSliderProvider调用notifyDataChanged不会刷新数据,只是走了getCount()方法,并没有走
createPageInContainer方法,怎么刷新数据啊. 这里说明一点数据内容有变化,但是getCount()的长度没变,难道鸿蒙是根据getCount()的长度有没有变化才刷新数据, 不会这么坑吧. 求教怎么刷新数据
/*
- Copyright © 2021 Huawei Device Co., Ltd.
- Licensed under the Apache License, Version 2.0 (the “License”);
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an “AS IS” BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License. */
package com.cmhi.hm.universalapp.mobaihe.provider;
import com.cmhi.hm.universalapp.ResourceTable; import com.cmhi.hm.universalapp.mobaihe.OnItemClickListener; import com.cmhi.hm.universalapp.mobaihe.model.MbhMediaRecommendGroup; import com.cmhi.hm.universalapp.utils.TextUtils; import com.cmhi.hm.universalapp.utils.ToastUtil; import ohos.agp.components.*; import ohos.app.Context; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel;
import java.util.ArrayList; import java.util.List;
public class TabPagerProvider extends PageSliderProvider { private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, “TabPagerProvider.class”); List<MbhMediaRecommendGroup> data; private OnMediaPageClickListener listener; private Context context; private MainMediaDataProvider mediaDataProvider; private String mediaType;
/**
* 构造
*
* @param context 上下文
* @param data 数据源
*/
public TabPagerProvider(Context context, List<MbhMediaRecommendGroup> data) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object createPageInContainer(ComponentContainer componentContainer, int i) {
Component componentAt = componentContainer.getComponentAt(i);
HiLog.debug(LABEL_LOG, "TabPagerProvider ");
PageItem pageItem;
if(componentAt == null){
componentAt = LayoutScatter.getInstance(context).parse(
ResourceTable.Layout_item_tab, null, false);
pageItem = new PageItem(componentAt);
componentAt.setTag(pageItem);
}else {
pageItem = (PageItem) componentAt.getTag();
}
pageItem.dlMoreContainer.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//跳到更多
if(listener != null){
String mediaType = data.get(i).getMediaType();
listener.onMoreMediaCLick(mediaType);
}
}
});
pageItem.dlRefreshContainer.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
//刷新当前类型
ToastUtil.showToast("刷新");
if(listener != null){
mediaType = data.get(i).getMediaType();
listener.onRefreshMediaClick(mediaType);
}
}
});
mediaDataProvider = new MainMediaDataProvider(data.get(i).getMediaList(), new OnItemClickListener() {
@Override
public void OnitemClick(int position) {
ToastUtil.showToast("详情");
}
});
TableLayoutManager tableLayoutManager = new TableLayoutManager();
tableLayoutManager.setColumnCount(3);
tableLayoutManager.setRowCount(2);
pageItem.listContainer.setLayoutManager(tableLayoutManager);
pageItem.listContainer.setItemProvider(mediaDataProvider);
componentContainer.addComponent(componentAt);
return componentAt;
}
@Override
public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
componentContainer.removeComponent((Component) o);
}
@Override
public boolean isPageMatchToObject(Component component, Object o) {
return component == o;
}
public void setData(List<MbhMediaRecommendGroup> datas) {
this.data = datas;
// if(!TextUtils.isEmpty(mediaType)){ // if(data != null && data.size() >0){ // for(int i =0; i<data.size();i++){ // if(mediaType.equals( data.get(i).getMediaType())){ // mediaDataProvider.refreshData(data.get(i).getMediaList()); // break; // } // } // } // } notifyDataChanged(); }
public void setClickListener(OnMediaPageClickListener listener){
this.listener =listener;
}
public interface OnMediaPageClickListener{
void onMoreMediaCLick(String mediaType);
void onRefreshMediaClick(String mediaType);
void onMediaDetailClick();
}
public static class PageItem {
private ListContainer listContainer;
private DependentLayout dlMoreContainer;
private DependentLayout dlRefreshContainer;
PageItem(Component component){
listContainer = (ListContainer) component.findComponentById(ResourceTable.Id_lc_media_list);
dlMoreContainer = (DependentLayout) component.findComponentById(ResourceTable.Id_dl_more_container);
dlRefreshContainer = (DependentLayout) component.findComponentById(ResourceTable.Id_dl_refresh_container);
}
}
}
后面直接调用mediaDataProvider.refreshData()方法来刷新listContainer,调用notifyDataChanged方法,我看getComponent方法也回调了,里面数据设置方法都走了,数据也变了, 但是页面就是没有刷新. 我必须手动切换pageSlide,才会刷新数据, 这是为啥啊
package com.cmhi.hm.universalapp.mobaihe.provider;import com.bumptech.glide.Glide; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.bumptech.glide.request.RequestOptions; import com.cmhi.hm.universalapp.ResourceTable; import com.cmhi.hm.universalapp.mobaihe.OnItemClickListener; import com.cmhi.hm.universalapp.mobaihe.model.ContentInfoListBean; import com.cmhi.hm.universalapp.utils.MbhUtil; import ohos.agp.components.*; import ohos.app.Context; import ohos.hiviewdfx.HiLog; import ohos.hiviewdfx.HiLogLabel;
import java.util.ArrayList; import java.util.List;
import static com.cmhi.hm.universalapp.utils.DensityUtil.vp2px;
public class MainMediaDataProvider extends BaseItemProvider { private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, “MainMediaDataProvider.class”); private List<ContentInfoListBean> mDatas = new ArrayList<>(); private OnItemClickListener listener;
public MainMediaDataProvider(List<ContentInfoListBean> mDatas, OnItemClickListener listener) { super(); this.mDatas = mDatas; this.listener = listener; HiLog.debug(LABEL_LOG, "MainMediaDataProvider "); } @Override public int getCount() { return mDatas == null ? 0 : mDatas.size(); } @Override public ContentInfoListBean getItem(int i) { if (mDatas != null && mDatas.size() > i) { return mDatas.get(i); } return null; } @Override public long getItemId(int i) { return i; } @Override public Component getComponent(int i, Component component, ComponentContainer componentContainer) { Component parse = component; //默认 component ==null 第一次的时候 HiLog.debug(LABEL_LOG, "getComponent " + i); /** * LayoutScatter * 您可以使用该工具在XML文件中定义一个组件, * 然后调用parse(int, ohos.agp.components.ComponentContainer, boolean) * 方法来使用该文件生成一个组件对象。 * parse 公共组件解析(int xmlId, ComponentContainer根,boolean attachToRoot) * 将XML文件转换为组件。 * */ /** * parse * 参数1 组件id * 参数2 跟组件 就是当前容器 父组件。 */ LiveItem liveItem;//初始化 if (parse == null) { //获取上下文 这里获取的是父组件 就是 ListContainer 上下文 Context context = componentContainer.getContext(); //获取xml 布局模板 parse = LayoutScatter.getInstance(context) .parse(ResourceTable.Layout_hardware_item_media, componentContainer, false); //指明给哪个模板容器添加数据 liveItem = new LiveItem(parse); parse.setTag(liveItem);//将布局模板写入缓存 下次调用使用 不用重复查询模板 } else { //第二次进来有缓存 liveItem = (LiveItem) parse.getTag(); } //给布局模板里面的组件添加数据 MbhUtil.setMbhItemWithAndHeight(liveItem.dependentLayout,3,104,138); MbhUtil.setMbhItemWithAndHeight(liveItem.image,3,104,138); liveItem.name.setText(mDatas.get(i).getDpContentName()); liveItem.mediaDetail.setText(mDatas.get(i).getDpContentShortDesc()); Glide.with(componentContainer.getContext()).load(mDatas.get(i).getDpContentPicUrl()) .apply(RequestOptions.bitmapTransform(new RoundedCorners(16))).into(liveItem.image); parse.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { if (listener != null) { listener.OnitemClick(i); } } }); if ((i % 3) == 0) { parse.setMarginLeft(vp2px(parse.getContext(), 12)); parse.setMarginRight(vp2px(parse.getContext(), 6)); } else if((i % 3) == 1){ parse.setMarginLeft(vp2px(parse.getContext(), 6)); parse.setMarginRight(vp2px(parse.getContext(), 6)); }else { parse.setMarginLeft(vp2px(parse.getContext(), 6)); parse.setMarginRight(vp2px(parse.getContext(), 12)); } //绑定完成 返回当前模板 return parse; } public void setData(List<ContentInfoListBean> data) { int size = mDatas.size(); this.mDatas = data; notifyDataSetItemRangeInserted(size,mDatas.size() -size); } public void refreshData(List<ContentInfoListBean> data) { this.mDatas = data; notifyDataChanged(); } /** * 动态卡片容器 */ public static class LiveItem { Text name;//初始化组件 Image image; Text mediaDetail; DirectionalLayout dependentLayout; LiveItem(Component itemComponent) { //获取模板里面的 组件 name = (Text) itemComponent.findComponentById(ResourceTable.Id_tv_media_name); dependentLayout = (DirectionalLayout) itemComponent.findComponentById(ResourceTable.Id_dl_content_container); mediaDetail = (Text) itemComponent.findComponentById(ResourceTable.Id_tv_media_detail); image = (Image) itemComponent.findComponentById(ResourceTable.Id_iv_media); } }
}
看下是不是isPageMatchToObject方法没有返回true造成的
链接好像打不开呀😂
在HarmonyOS中,如果NextPageSliderProvider
调用notifyDataChanged()
后数据没有刷新,这通常与数据源的更新方式或UI的响应机制有关。请确保以下几点:
- 数据源在调用
notifyDataChanged()
之前已正确更新。 - 检查
NextPageSliderProvider
的实现,确保在notifyDataChanged()
被调用时,它确实重新生成了视图或数据列表。 - 验证是否有任何异步操作影响了数据更新和UI刷新的顺序。
如果问题依旧没法解决请加我微信,我的微信是itying888。