背景
android的Listview,在鸿蒙中对应的是ListContainer,Android的adapter在鸿蒙中对应的是RecycleItemProvider,手机开发必然离不来列表数据,为了简洁代码高效开发,封装RecycleItemProvider便提上了日程,目前笔者正在实战开发项目,所踩过的坑与经验与大家分享一下,帮助大家更好的入坑鸿蒙,希望早日鸿蒙生态圈形成。
使用方法 |
一、 设置图片 holder.setBackgroundRes(ResourceTable.Id_Component, ResourceTable.Media_icon); |
二、 控件显示隐藏 holder.setVisible(ResourceTable.Id_Component, false); |
三、 控件显示隐藏 holder.setInvisible(ResourceTable.Id_Component, true); |
四、 设置显示文字 holder.setText(ResourceTable.Id_Component, item.getTitle()); |
五、 设置监听 holder.setClickedListener(ResourceTable.Id_Component, new Component.ClickedListener() {); |
六、 后续继续新增方法 |
封装后如何使用
OftenSeeAdapter mOftenSeeAdapter = new OftenSeeAdapter(mContext, mOftenSeeBean, ResourceTable.Layout_item_often_see); mListContainerOftenSee.setItemProvider(mOftenSeeAdapter);
Provider写法
public class OftenSeeAdapter extends RecycleItemProviderAdapter<String> { public OftenSeeAdapter(Context context, List<String> datas, int layoutId) { super(context, datas, layoutId); } @Override public void convert(ViewHolder holder, String s) { } }
相比原代码阅读性立马提升了很多,布局复用也已处理,目前笔者只把常用的属性添加到了ViewHolder中,其他需要的自行添加,以后笔者会继续完善
代码如下,
RecycleItemProviderAdapter如下
/** *qq群 : 1084365075 * author : liupeng * date : 2021/1/6 * desc : adapter基类封装 */ public abstract class RecycleItemProviderAdapter<T> extends BaseItemProvider { protected Context mContext; protected List<T> mDatas; protected LayoutScatter mInflater; public int layoutId; private int displayCount; public RecycleItemProviderAdapter(Context context, List<T> datas) { this.mContext = context; this.mDatas = datas; mInflater = LayoutScatter.getInstance(mContext); } public RecycleItemProviderAdapter(Context context, List<T> datas, int layoutId) { this.mContext = context; this.mDatas = datas; this.layoutId = layoutId; mInflater = LayoutScatter.getInstance(mContext); } @Override public int getCount() { if (displayCount != 0) { return this.displayCount; } return mDatas.size(); } public void setDisplayCount(int displayCount) { this.displayCount = displayCount; } @Override public T getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public Component getComponent(int position, Component convertView, ComponentContainer parent) { ViewHolder holder = ViewHolder.get(mContext, convertView, parent, layoutId, position); convert(holder, getItem(position)); return holder.getConvertView(); } public List<T> getDatas() { return mDatas; } public void setDatas(List<T> datas) { mDatas = datas; } public abstract void convert(ViewHolder holder, T t); }
ViewHolder 类
/** * qq群 : 1084365075 * author : liupeng * date : 2021/1/6 * desc : 适配adapter的viewHolder */ public class ViewHolder { private HashMap<Integer, Component> mViews; private int mPosition; private Component mConvertView; private Context mContext; private int mLayoutId; public ViewHolder(Context context, ComponentContainer parent, int layoutId, int position) { mContext = context; mLayoutId = layoutId; this.mPosition = position; this.mViews = new HashMap<>(); mConvertView = LayoutScatter.getInstance(mContext).parse(layoutId, null, false); mConvertView.setTag(this); } public static ViewHolder get(Context context, Component convertView, ComponentContainer parent, int layoutId, int position) { if (convertView == null) { return new ViewHolder(context, parent, layoutId, position); } else { ViewHolder holder = (ViewHolder) convertView.getTag(); holder.mPosition = position; return holder; } } public int getPosition() { return mPosition; } public int getLayoutId() { return mLayoutId; } /** * 通过viewId获取控件 * * @param viewId * @return */ public <T extends Component> T getView(int viewId) { Component view = mViews.get(viewId); if (view == null) { view = mConvertView.findComponentById(viewId); mViews.put(viewId, view); } return (T) view; } public Component getConvertView() { return mConvertView; } /** * 设置TextView的文本 * * @param viewId * @param text * @return */ public ViewHolder setText(int viewId, String text) { Text tv = getView(viewId); tv.setText(text); return this; } public ViewHolder setTextColor(int viewId, int colorid) { Text tv = getView(viewId); tv.setTextColor(ResUtil.getNewColor(mContext,colorid)); return this; } public ViewHolder setTextSize(int viewId, int size) { Text tv = getView(viewId); tv.setTextSize(size, Text.TextSizeType.FP); return this; } /** * @param url * @return */ public ViewHolder setImage(int viewId, String url) { Image image = getView(viewId); GlideUtils.getInstance().displayImage(mContext, url, image); return this; } /** * 设置背景 * * @param viewId * @param resId * @return */ public ViewHolder setBackgroundRes(int viewId, int resId) { Component component = getView(viewId); component.setBackground(ResUtil.getPixelMapDrawable(component.getContext(), resId)); return this; } /** * 设置image图片 * * @param viewId * @param resId * @return */ public ViewHolder setImageRes(int viewId, int resId) { Image image = getView(viewId); image.setImageElement(ResUtil.getPixelMapDrawable(image.getContext(), resId)); return this; } /** * 设置显示隐藏 * * @param viewId * @param visible * @return */ public ViewHolder setVisible(int viewId, boolean visible) { Component view = getView(viewId); view.setVisibility(visible ? Component.VISIBLE : Component.HIDE); return this; } /** * 设置显示隐藏 * * @param viewId * @param invisible * @return */ public ViewHolder setInvisible(int viewId, boolean invisible) { Component view = getView(viewId); view.setVisibility(invisible ? Component.VISIBLE : Component.INVISIBLE); return this; } /** * 设置圆角 * * @param viewId * @param radius * @return */ public ViewHolder setCornerRadius(int viewId, int radius) { Image view = getView(viewId); view.setCornerRadius(DisplayUtils.vp2px(mContext, radius)); return this; } /** * 设置圆角 * * @param viewId * @param left * @param top * @param right * @param bottom * @return */ public ViewHolder setCornerRadii(int viewId, int left, int top, int right, int bottom) { float[] floats = { DisplayUtils.vp2px(mContext, left), DisplayUtils.vp2px(mContext, left), DisplayUtils.vp2px(mContext, top), DisplayUtils.vp2px(mContext, top), DisplayUtils.vp2px(mContext, right), DisplayUtils.vp2px(mContext, right), DisplayUtils.vp2px(mContext, bottom), DisplayUtils.vp2px(mContext, bottom), }; Image view = getView(viewId); view.setCornerRadii(floats); return this; } /** * 关于事件 */ public ViewHolder setClickedListener(int viewId, Component.ClickedListener listener) { Component view = getView(viewId); view.setClickedListener(listener); return this; } public ViewHolder setTouchEventListener(int viewId, Component.TouchEventListener listener) { Component view = getView(viewId); view.setTouchEventListener(listener); return this; } public ViewHolder setLongClickedListener(int viewId, Component.LongClickedListener listener) { Component view = getView(viewId); view.setLongClickedListener(listener); return this; } public ViewHolder setDoubleClickedListener(int viewId, Component.DoubleClickedListener listener) { Component view = getView(viewId); view.setDoubleClickedListener(listener); return this; } }