我有一个垂直聊天recycler-view
,我试图显示多个水平recycler-view's
,其中包含一个“ carousel
”的卡片视图。我可以膨胀水平视图,但是当我尝试在父视图中添加两个相同类型的水平视图作为独立项目时,我要么将新数据添加到原始数据中,然后显示两次,要么清除旧数组新数据替换 中的旧数据,recycler-view
并且不添加第二个数据。
我不确定这是否是由于我没有为每个水平视图生成单独的适配器或某些阵列问题。
我adapter
这一代,在onBindViewHolder
父回收者视图中调用:
fun generateAdapter(holder: ViewHolder): CarouselAdapter{
val layoutManager = LinearLayoutManager(appContext, LinearLayoutManager.HORIZONTAL, false )
holder.carouselView.layoutManager = layoutManager
val adapterCarousel = CarouselAdapter()
holder.carouselView.adapter = adapterCarousel
snapHelper.attachToRecyclerView(holder.carouselView)
holder.carouselView.addItemDecoration(ItemOffsetDecoration(12))
holder.carouselView.isNestedScrollingEnabled = false
layoutManager.isSmoothScrollbarEnabled = true
layoutManager.stackFromEnd = true
return adapterCarousel
}
将数据调用到子回收器视图:
"rates" -> {
if (items[i].ratesData != null) {
val adapter = generateAdapter(holder)
for (j in 0 until (items[i].ratesData!!.size)) {
adapter.addCarousel(ChatNew(items[i].msgTime, null, null, null, items[i].ratesData!![j], null, null, null, null))
}
}
//ParseHelper.clearArrays()
}
子适配器:
class CarouselAdapter : RecyclerView.Adapter<ViewHolderCarousel>() {
private val RATES = 1
private val IMG_BUTTON = 2
private val IMG_NO_BUTTON = 3
private val BLUE = 4
private val NO_IMG_BUTTON = 5
private val ERROR = 0
lateinit var type : String
var items: ArrayList<ChatNew> = ArrayList()
fun addCarousel(carousel: ChatNew){
items.add(carousel)
notifyItemInserted(itemCount)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderCarousel {
return when(viewType) {
RATES -> RatesViewHolderNew(LayoutInflater.from(appContext).inflate(R.layout.carousel_rate, parent, false))
IMG_BUTTON -> ImgButtonViewHolder(LayoutInflater.from(appContext).inflate(R.layout.carousel_img_btn, parent, false))
IMG_NO_BUTTON -> ImgNoButtonViewHolder(LayoutInflater.from(appContext).inflate(R.layout.carousel_img_no_btn, parent, false))
NO_IMG_BUTTON-> NoImgButtonViewHolder(LayoutInflater.from(appContext).inflate(R.layout.carousel_no_img_btn, parent, false))
else -> BlueViewHolder(LayoutInflater.from(appContext).inflate(R.layout.carousel_noimg_nobtn, parent, false))
}
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: ViewHolderCarousel, i: Int) {
type = findType(holder)
when (type){
"rates" -> {
val ri = items[i]
//for (i in 0 until (ri.ratesData!!.size)){
holder.pnView.text = ri.ratesData?.pn
holder.mpView.text =ri.ratesData?.mp
holder.itView.text = ri.ratesData?.it
holder.brView.text = ri.ratesData?.br
holder.pfView.text = ri.ratesData?.pf
holder.aprcView.text = ri.ratesData?.aprc
holder.tpView.text = ri.ratesData?.tp
holder.headDivider!!.setBackgroundColor(ContextCompat.getColor(appContext!!, R.color.NWBlue))
// }
}
"carousel button" -> {
val ci = items[i]
holder.imgView.setImageResource(ci.carouselData?.img!!)
holder.number.text = ci.carouselData?.number
holder.title.text = ci.carouselData?.title
holder.content.text = ci.carouselData?.content
holder.magicString.text = ci.carouselData?.magicString
}
"img no button" -> {
val ci = items[i]
holder.imgViewNoBtn.setImageResource(ci.carouselData?.img!!)
holder.numberNoBtn.text = ci.carouselData?.number
holder.titleNoBtn.text = ci.carouselData?.title
holder.contentNoBtn.text = ci.carouselData?.content
holder.noButtonString.text = ci.carouselData?.noButtonString
}
"no img no button" -> {
val ci = items[i]
holder.imgViewNoImgNoBtn.setBackgroundColor(ContextCompat.getColor(appContext!!,R.color.NWBlue))
holder.numberNoImgNoBtn.text = ci.carouselData?.number
holder.titleNoImgNoBtn.text = ci.carouselData?.title
holder.contentNoImgNoBtn.text = ci.carouselData?.content
}
"no img button" -> {
val ci = items[i]
holder.imgViewNoImg.setBackgroundColor(ContextCompat.getColor(appContext!!,R.color.NWBlue))
holder.numberNoImg.text = ci.carouselData?.number
holder.titleNoImg.text = ci.carouselData?.title
holder.contentNoImg.text = ci.carouselData?.content
holder.magicStringNoImg.text = ci.carouselData?.magicString
}
}
}
override fun getItemViewType(i: Int): Int {
return when (items.isNotEmpty()) {
true -> {
when {
items[i].ratesData != null -> RATES
items[i].carouselData != null -> {
when (items[i].carouselData?.img != null) {
true -> {
return if (items[i].carouselData?.magicString != null) {
//has image and button
IMG_BUTTON
} else {
//has image but no button
IMG_NO_BUTTON
}
}
false -> {
return if (items[i].carouselData?.magicString != null) {
//has no image but button
NO_IMG_BUTTON
} else {
//has no image and no button
BLUE
}
}
}
}
else -> ERROR
}
}
false -> ERROR
}
}
fun findType(holder: ViewHolderCarousel): String {
return when (holder) {
is RatesViewHolderNew -> "rates"
is ImgButtonViewHolder -> "carousel button"
is ImgNoButtonViewHolder -> "img no button"
is NoImgButtonViewHolder -> "no img button"
is BlueViewHolder -> "no img no button"
else -> "error"
}
}
}
open class ViewHolderCarousel(view: View) : RecyclerView.ViewHolder(view) {
// looks at the view and finds components on that view
// carousel for rates
val headDivider: LinearLayout? = view.head_divider
val pnView = view.pn_view
val mpView = view.mp_view
val itView = view.it_view
val brView = view.br_view
val pfView = view.pf_view
val aprcView = view.aprc_view
val tpView = view.tp_view
//TODO: set up links for "r" sections
//carousel with img and button
val imgView = view.imageView_btn
val number = view.number_btn
val title = view.title_btn
val content = view.content_btn
val magicString = view.carousel_button_btn
//carousel with img but no button
val imgViewNoBtn = view.imageView_no_btn
val numberNoBtn = view.number_no_btn
val titleNoBtn = view.title_no_btn
val contentNoBtn = view.content_no_btn
val noButtonString = view.magic_string_no_btn
//carousel with no img or button
val imgViewNoImgNoBtn = view.imageView_no_img_no_btn
val numberNoImgNoBtn = view.number_no_img_no_btn
val titleNoImgNoBtn = view.title_no_img_no_btn
val contentNoImgNoBtn = view.content_no_img_no_btn
val imgViewNoImg = view.imageView_no_img
val numberNoImg = view.number_no_img
val titleNoImg = view.title_no_img
val contentNoImg = view.content_no_img
val magicStringNoImg = view.carousel_button_no_img
}
class RatesViewHolderNew(itemView: View) : ViewHolderCarousel(itemView)
class ImgButtonViewHolder(itemView: View) : ViewHolderCarousel(itemView)
class ImgNoButtonViewHolder(itemView: View) : ViewHolderCarousel(itemView)
class NoImgButtonViewHolder(itemView: View) : ViewHolderCarousel(itemView)
class BlueViewHolder(itemView: View) : ViewHolderCarousel(itemView)
任何关于我如何确保每次添加一组新的轮播数据时都会将其添加为父视图中的独立项目的想法将不胜感激。