VideoShowAdapter.kt 3.21 KB
Newer Older
1 2 3 4 5 6
package com.ydl.confide.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
万齐军 committed
7
import androidx.databinding.ObservableInt
8 9 10 11 12
import androidx.recyclerview.widget.RecyclerView
import com.ydl.confide.R
import com.ydl.confide.databinding.ItemConfideHomeRecentBinding
import com.ydl.confide.databinding.ItemVideoShowBinding
import com.ydl.confide.home.bean.ConfideHomeBodyBean
13
import com.ydl.confide.home.event.IConfideHomeEvent
14 15
import com.ydl.confide.intro.BindingViewHolder

16
class VideoShowAdapter(private val data: List<ConfideHomeBodyBean>?, private val event: IConfideHomeEvent) :
17
    RecyclerView.Adapter<BindingViewHolder<ItemVideoShowBinding>>() {
万齐军 committed
18
    private val dataList: List<ItemVideoShowViewModel> = data?.map { ItemVideoShowViewModel().mapOf(it) } ?: emptyList()
19 20 21 22 23 24 25 26 27 28 29
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemVideoShowBinding> {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding =
            DataBindingUtil.inflate<ItemVideoShowBinding>(layoutInflater, R.layout.item_video_show, parent, false)
        return BindingViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BindingViewHolder<ItemVideoShowBinding>, position: Int) {
30
        val itemVideoShowViewModel = dataList[position]
31
        holder.binding.item = itemVideoShowViewModel
万齐军 committed
32
        holder.itemView.setOnClickListener { event.videoShowClick(position, data) }
33 34
    }

35
    override fun getItemCount() = dataList.size
36 37 38
}


39
class RecentConfideAdapter(val data: List<ItemVideoShowViewModel>, private val event: IConfideHomeEvent) :
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    RecyclerView.Adapter<BindingViewHolder<ItemConfideHomeRecentBinding>>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemConfideHomeRecentBinding> {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding =
            DataBindingUtil.inflate<ItemConfideHomeRecentBinding>(
                layoutInflater,
                R.layout.item_confide_home_recent,
                parent,
                false
            )
        return BindingViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BindingViewHolder<ItemConfideHomeRecentBinding>, position: Int) {
57 58
        val itemVideoShowViewModel = data[position]
        holder.binding.item = itemVideoShowViewModel
霍志良 committed
59
        holder.itemView.setOnClickListener { event.consultantClick(itemVideoShowViewModel.linkUrl,itemVideoShowViewModel.doctorId) }
60 61 62 63 64 65
    }

    override fun getItemCount() = data.size
}

class ItemVideoShowViewModel {
66
    var linkUrl: String? = null
67 68 69
    val name = ObservableField<String>("")
    val coverUrl = ObservableField<String>("")
    val videoCoverUrl = ObservableField<String>("")
万齐军 committed
70
    val state = ObservableInt()
霍志良 committed
71
    var doctorId: String? = null
72 73 74 75
}

internal fun ItemVideoShowViewModel.mapOf(bean: ConfideHomeBodyBean): ItemVideoShowViewModel {
    name.set(bean.confidedName)
76
    coverUrl.set(bean.confidedIcon)
万齐军 committed
77
    videoCoverUrl.set(bean.coverVideoPicture)
万齐军 committed
78
    state.set(bean.confideLine ?: 0)
79
    linkUrl = bean.linkUrl
霍志良 committed
80
    doctorId = bean.doctorId
81 82 83 84 85 86 87
    return this
}