-
Notifications
You must be signed in to change notification settings - Fork 4
ViewHolder 快捷用法
DONG edited this page Oct 24, 2019
·
2 revisions
BaseViewHolder
提供了很多用于在 onBind
中快速渲染元素的方法.
比如以前设置一个 TextView
的内容:
TextView tvTitle = findViewById(R.id.tv_title);
tvTitle.setText("Test");
现在只需要一行代码:
holder.setText(R.id.tv_title, "Text");
方法列表:
方法 | 说明 |
---|---|
getView(@IdRes int viewId) |
获得对应的 View. 类似于 findViewById. 但是内部通过 SparseArray<View> 记录用过的 View. 所以效率更高 |
setText(@IdRes int viewId, CharSequence value) |
设置文字 |
setText(@IdRes int viewId, @StringRes int strId) |
同上 |
setTextColor(@IdRes int viewId, @ColorInt int textColor) |
设置文字颜色 |
setImageDrawable(@IdRes int viewId, Drawable drawable) |
设置图片 |
setImageBitmap(@IdRes int viewId, Bitmap bitmap) |
同上 |
setImageResource(@IdRes int viewId, @DrawableRes int imageResId) |
同上 |
setBackgroundColor(@IdRes int viewId, @ColorInt int color) |
设置背景颜色 |
setBackgroundRes(@IdRes int viewId, @DrawableRes int backgroundRes) |
设置背景资源 |
setAlpha(@IdRes int viewId, float value) |
设置 Alpha |
setGone(@IdRes int viewId, boolean visible) |
设置隐藏 (Gone) |
setVisible(@IdRes int viewId, boolean visible) |
设置是否可见 (INVISIBLE) |
setVisibility(@IdRes int viewId, @holderVisibility int visibility) |
设置是否可见 (接受 View.VISIBLE, View.INVISIBLE, View.GONE) |