对vLayout适配器的封装

private final HashSet nestViews;

private final LinkedHashSet childClickViewIds;

private final LinkedHashSet itemChildLongClickViewIds;

private BaseDelegateAdapter adapter;

/**

  • use itemView instead

*/

@Deprecated

public View convertView;

/**

  • Package private field to retain the associated user object and detect a change

*/

Object associatedObject;

public BaseDelegateViewHolder(final View view) {

super(view);

this.views = new SparseArray<>();

this.childClickViewIds = new LinkedHashSet<>();

this.itemChildLongClickViewIds = new LinkedHashSet<>();

this.nestViews = new HashSet<>();

convertView = view;

}

private int getClickPosition() {

return getLayoutPosition();

}

public HashSet getItemChildLongClickViewIds() {

return itemChildLongClickViewIds;

}

public HashSet getChildClickViewIds() {

return childClickViewIds;

}

/**

  • use itemView instead

  • @return the ViewHolder root view

*/

@Deprecated

public View getConvertView() {

return convertView;

}

/**

  • Will set the text of a TextView.

  • @param viewId The view id.

  • @param value The text to put in the text view.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setText(@IdRes int viewId, CharSequence value) {

TextView view = getView(viewId);

view.setText(value);

return this;

}

public BaseDelegateViewHolder setText(@IdRes int viewId, @StringRes int strId) {

TextView view = getView(viewId);

view.setText(strId);

return this;

}

/**

  • Will set the image of an ImageView from a resource id.

  • @param viewId The view id.

  • @param imageResId The image resource id.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setImageResource(@IdRes int viewId, @DrawableRes int imageResId) {

ImageView view = getView(viewId);

view.setImageResource(imageResId);

return this;

}

/**

  • Will set background color of a view.

  • @param viewId The view id.

  • @param color A color, not a resource id.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setBackgroundColor(@IdRes int viewId, @ColorInt int color) {

View view = getView(viewId);

view.setBackgroundColor(color);

return this;

}

/**

  • Will set background of a view.

  • @param viewId The view id.

  • @param backgroundRes A resource to use as a background.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setBackgroundRes(@IdRes int viewId, @DrawableRes int backgroundRes) {

View view = getView(viewId);

view.setBackgroundResource(backgroundRes);

return this;

}

/**

  • Will set text color of a TextView.

  • @param viewId The view id.

  • @param textColor The text color (not a resource id).

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setTextColor(@IdRes int viewId, @ColorInt int textColor) {

TextView view = getView(viewId);

view.setTextColor(textColor);

return this;

}

/**

  • Will set the image of an ImageView from a drawable.

  • @param viewId The view id.

  • @param drawable The image drawable.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setImageDrawable(@IdRes int viewId, Drawable drawable) {

ImageView view = getView(viewId);

view.setImageDrawable(drawable);

return this;

}

/**

  • Add an action to set the image of an image view. Can be called multiple times.

*/

public BaseDelegateViewHolder setImageBitmap(@IdRes int viewId, Bitmap bitmap) {

ImageView view = getView(viewId);

view.setImageBitmap(bitmap);

return this;

}

/**

  • Add an action to set the alpha of a view. Can be called multiple times.

  • Alpha between 0-1.

*/

public BaseDelegateViewHolder setAlpha(@IdRes int viewId, float value) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

getView(viewId).setAlpha(value);

} else {

// Pre-honeycomb hack to set Alpha value

AlphaAnimation alpha = new AlphaAnimation(value, value);

alpha.setDuration(0);

alpha.setFillAfter(true);

getView(viewId).startAnimation(alpha);

}

return this;

}

/**

  • Set a view visibility to VISIBLE (true) or GONE (false).

  • @param viewId The view id.

  • @param visible True for VISIBLE, false for GONE.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setGone(@IdRes int viewId, boolean visible) {

View view = getView(viewId);

view.setVisibility(visible ? View.VISIBLE : View.GONE);

return this;

}

/**

  • Set a view visibility to VISIBLE (true) or INVISIBLE (false).

  • @param viewId The view id.

  • @param visible True for VISIBLE, false for INVISIBLE.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setVisible(@IdRes int viewId, boolean visible) {

View view = getView(viewId);

view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);

return this;

}

/**

  • Add links into a TextView.

  • @param viewId The id of the TextView to linkify.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder linkify(@IdRes int viewId) {

TextView view = getView(viewId);

Linkify.addLinks(view, Linkify.ALL);

return this;

}

/**

  • Apply the typeface to the given viewId, and enable subpixel rendering.

*/

public BaseDelegateViewHolder setTypeface(@IdRes int viewId, Typeface typeface) {

TextView view = getView(viewId);

view.setTypeface(typeface);

view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);

return this;

}

/**

  • Apply the typeface to all the given viewIds, and enable subpixel rendering.

*/

public BaseDelegateViewHolder setTypeface(Typeface typeface, int… viewIds) {

for (int viewId : viewIds) {

TextView view = getView(viewId);

view.setTypeface(typeface);

view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);

}

return this;

}

/**

  • Sets the progress of a ProgressBar.

  • @param viewId The view id.

  • @param progress The progress.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setProgress(@IdRes int viewId, int progress) {

ProgressBar view = getView(viewId);

view.setProgress(progress);

return this;

}

/**

  • Sets the progress and max of a ProgressBar.

  • @param viewId The view id.

  • @param progress The progress.

  • @param max The max value of a ProgressBar.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setProgress(@IdRes int viewId, int progress, int max) {

ProgressBar view = getView(viewId);

view.setMax(max);

view.setProgress(progress);

return this;

}

/**

  • Sets the range of a ProgressBar to 0…max.

  • @param viewId The view id.

  • @param max The max value of a ProgressBar.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setMax(@IdRes int viewId, int max) {

ProgressBar view = getView(viewId);

view.setMax(max);

return this;

}

/**

  • Sets the rating (the number of stars filled) of a RatingBar.

  • @param viewId The view id.

  • @param rating The rating.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setRating(@IdRes int viewId, float rating) {

RatingBar view = getView(viewId);

view.setRating(rating);

return this;

}

/**

  • Sets the rating (the number of stars filled) and max of a RatingBar.

  • @param viewId The view id.

  • @param rating The rating.

  • @param max The range of the RatingBar to 0…max.

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setRating(@IdRes int viewId, float rating, int max) {

RatingBar view = getView(viewId);

view.setMax(max);

view.setRating(rating);

return this;

}

/**

  • Sets the tag of the view.

  • @param viewId The view id.

  • @param tag The tag;

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setTag(@IdRes int viewId, Object tag) {

View view = getView(viewId);

view.setTag(tag);

return this;

}

/**

  • Sets the tag of the view.

  • @param viewId The view id.

  • @param key The key of tag;

  • @param tag The tag;

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setTag(@IdRes int viewId, int key, Object tag) {

View view = getView(viewId);

view.setTag(key, tag);

return this;

}

/**

  • Sets the checked status of a checkable.

  • @param viewId The view id.

  • @param checked The checked status;

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setChecked(@IdRes int viewId, boolean checked) {

View view = getView(viewId);

// View unable cast to Checkable

if (view instanceof Checkable) {

((Checkable) view).setChecked(checked);

}

return this;

}

/**

  • Set the enabled state of this view.

  • @param viewId The view id.

  • @param enable The checked status;

  • @return The BaseViewHolder for chaining.

*/

public BaseDelegateViewHolder setEnabled(@IdRes int viewId,boolean enable) {

View view = getView(viewId);

view.setEnabled(enable);

return this;

}

@SuppressWarnings(“unchecked”)

public T getView(@IdRes int viewId) {

View view = views.get(viewId);

if (view == null) {

view = itemView.findViewById(viewId);

views.put(viewId, view);

}

return (T) view;

}

/**

  • Retrieves the last converted object on this view.

*/

public Object getAssociatedObject() {

return associatedObject;

}

/**

  • Should be called during convert

*/

public void setAssociatedObject(Object associatedObject) {

this.associatedObject = associatedObject;

}

}

2.Adapter(包含item的点击事件和长按事件)

public abstract class BaseDelegateAdapter<T, VH extends RecyclerView.ViewHolder> extends DelegateAdapter.Adapter{

private static final String TAG = BaseDelegateAdapter.class.getSimpleName();

public Context mContext;

private LayoutInflater mLayoutInflater;

private int mLayoutResId;

private List mData;

private LayoutHelper mLayoutHelper;

public BaseDelegateAdapter(@LayoutRes int layoutResId, @Nullable List data, LayoutHelper layoutHelper) {

this.mData = data == null ? new ArrayList<>() : data;

if (layoutResId != 0) {

this.mLayoutResId = layoutResId;

}

this.mLayoutHelper = layoutHelper;

}

/**

  • 删除一条数据

*/

public void removeItem(int position) {

mData.remove(position);

// 暴力刷新

总结

最后对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
lper) {

this.mData = data == null ? new ArrayList<>() : data;

if (layoutResId != 0) {

this.mLayoutResId = layoutResId;

}

this.mLayoutHelper = layoutHelper;

}

/**

  • 删除一条数据

*/

public void removeItem(int position) {

mData.remove(position);

// 暴力刷新

总结

最后对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

[外链图片转存中…(img-sZDO7mRc-1715257645410)]

[外链图片转存中…(img-Af2Hi3x1-1715257645411)]

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值