自定义Adapter中的步骤:
(1)新建 N 个常量,每个常量代表一个子项类型;
(2)重写 RecyclerView.java
的 getItemViewType(int position)
方法, 根据 position
参数界定出各个类型;
(3)重写 onCreateViewHolder
方法:
@NonNull
public abstract VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType);
根据子项类型,分别构建出不同的 RecyclerView.ViewHolder
的子类并 return
。
(4)重新方法:
public abstract void onBindViewHolder(@NonNull VH holder, int position);
通过 position
获取子项类型,然后将holder
参数转换为对应子项的 ViewHolder
,就可以去操作其内部的控件了。
class NetworkLiveBgListAdapter : RecyclerView.Adapter<BaseViewHolder>() {
companion object ViewType {
const val TITLE_LAYOUT = 1 //标题子项,自占一行
const val COMMON_LAYOUT = 2 //主题子项,一行三列
const val DEVIDER_LAYOUT = 3 //主题间的分割线,自占一行
const val BOTTOM_LINE_LAYOUT = 4 //最底部占位布局,自占一行
}
private var mData: ArrayList<LiveBgThemeBean> = arrayListOf()
/**
* 根据不同pos来获取itemViewType
* @param position Int
* @return Int
*/
override fun getItemViewType(position: Int): Int {
return when (mData[position].id) {
LiveThemeManagerActivity.THEME_TITLE_ID -> { // 标题
TITLE_LAYOUT
}
LiveThemeManagerActivity.THEME_DEVIDER_ID -> { //分割线
DEVIDER_LAYOUT
}
LiveThemeManagerActivity.THEME_BOTTOM_LINE_ID -> { //最底部占位布局
BOTTOM_LINE_LAYOUT
}
else -> {
COMMON_LAYOUT
}
}
}
override fun getItemCount(): Int {
return mData.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
val context = parent.context
return if (viewType == TITLE_LAYOUT) {
val itemView = LayoutInflater.from(context).inflate(R.layout.item_network_live_bg_layout_title,
null, false)
TitleViewHolder(itemView)
} else if (viewType == DEVIDER_LAYOUT) {
val itemView = LayoutInflater.from(context).inflate(R.layout.item_network_live_bg_layout_devider,
null, false)
DeviderViewHolder(itemView)
} else if(viewType == BOTTOM_LINE_LAYOUT){
val itemView = LayoutInflater.from(context).inflate(R.layout.item_network_live_bg_layout_bottomline,
null, false)
BottomLineViewHolder(itemView)
}else {
val itemView = LayoutInflater.from(context).inflate(R.layout.item_network_live_bg_layout,
null, false)
OtherViewHolder(itemView)
}
}
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
val type = getItemViewType(position)
when(type){
TITLE_LAYOUT -> {
if (holder is TitleViewHolder) { //标题布局
holder.run {
val data : LiveBgThemeBean = mData[position]
tvLiveBgThemeTitle.text = data.name
}
}
}
DEVIDER_LAYOUT -> { //分割线布局
}
BOTTOM_LINE_LAYOUT -> { //底部占位布局
}
COMMON_LAYOUT -> { //主题子项布局
if (holder is OtherViewHolder) {
holder.run {
val data : LiveBgThemeBean = mData[position]
}
}
}
}
}
fun setAdapterData(newData: List<LiveBgThemeBean>?) {
if (mData == null) {
mData = ArrayList()
}
if (newData != null) {
mData.addAll(newData)
}
}
class OtherViewHolder(itemView: View) : BaseViewHolder(itemView) {
val rlThemeBgRoot: RelativeLayout = itemView.findViewById(R.id.rl_theme_bg_root)
val llImgRoot: FrameLayout = itemView.findViewById(R.id.fl_img_root)
val imgLiveBg: ImageView = itemView.findViewById(R.id.img_live_bg)
val llThemeSelectTask: LinearLayout = itemView.findViewById(R.id.ll_theme_seect_task)
val llThemeSelectLayout: LinearLayout = itemView.findViewById(R.id.ll_theme_select_layout)
val tvThemeTitle: TextView = itemView.findViewById(R.id.tv_theme_title)
}
class TitleViewHolder(itemView: View) : BaseViewHolder(itemView) {
val tvLiveBgThemeTitle: TextView = itemView.findViewById(R.id.tv_live_bg_theme_title)
}
class DeviderViewHolder(itemView: View) : BaseViewHolder(itemView) {
}
class BottomLineViewHolder(itemView: View) : BaseViewHolder(itemView) {
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
val manager = recyclerView.layoutManager
if(manager is GridLayoutManager){
manager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
val type = getItemViewType(position)
if(type == COMMON_LAYOUT){
return 1
}else{
return manager.spanCount
}
}
}
}
}
}
备注:
(1)BaseViewHolder
是 RecyclerView.ViewHolder
的子类;
(2)onAttachedToRecyclerView
方法的作用,设置每类子项的宽度;