哪個小可愛在偷偷的看我~~
背景
android的Listview,在鴻蒙中對應的是ListContainer,Android的adapter在鴻蒙中對應的是RecycleItemProvider垒棋,手機開發(fā)必然離不來列表數據拖刃,為了簡潔代碼高效開發(fā)薯演,封裝RecycleItemProvider便提上了日程贸诚,目前筆者正在實戰(zhàn)開發(fā)項目耻姥,所踩過的坑與經驗與大家分享一下销钝,幫助大家更好的入坑鴻蒙,希望早日鴻蒙生態(tài)圈形成琐簇。
使用方法 |
---|
一萨脑、 設置圖片 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());
|
五、 設置監(jiān)聽 holder.setClickedListener(ResourceTable.Id_Component, new Component.ClickedListener() {);
|
六丈秩、 后續(xù)繼續(xù)新增方法 |
封裝后如何使用
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中蘑秽,其他需要的自行添加饺著,以后筆者會繼續(xù)完善
代碼如下,
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;
}
}