Home > database >  Is the RecyclerView absolutely wrong when there is only one item?
Is the RecyclerView absolutely wrong when there is only one item?

Time:12-09

enter image description here

I'm making a function that looks like an image.

Although not shown in the image, these items can be added or deleted through the button.

Item is composed of Header and Detail. The adapter also has HeaderAdapter and DetailAdapter respectively.

I'm using ConcatAdapter to make sure there are HeaderAdapter and DetailAdatper per group rather than the whole list.

Because I thought it would be more manageable than using multiview types in one adapter (purely my opinion).

But I have a question. HeaderAdapter.

As you can see from the image, there is one header per group. So, there must be only one HeaderItem in the HeaderAdapter of each group.

In this case, I don't think there is much reason to use the Adapter.

In my case, is it better to use a multiview type for one Adapter?

RoutineItem

sealed class RoutineItem(
    val layoutId: Int
) {
    data class Header(
        val id: String = "1",
        val workout: String = "2",
        val unit: String = "3",
    ) : RoutineItem(VIEW_TYPE) {
        companion object {
            const val VIEW_TYPE = R.layout.routine_item
        }
    }
    data class Detail(
        val id: String = UUID.randomUUID().toString(), // UUID
        val set: Int = 1,
        var weight: String ="",
        val reps: String = "1"
    ) : RoutineItem(VIEW_TYPE) {
        companion object {
            const val VIEW_TYPE = R.layout.item_routine_detail
        }
    }
}

HeaderAdapter

class HeaderAdapter(item: RoutineItem.Header) : BaseAdapter<RoutineItem.Header>(initialItem = listOf(item)) {

    override fun createViewHolder(itemView: View): GenericViewHolder<RoutineItem.Header> {
        return HeaderViewHolder(itemView)
    }

    override fun getItemCount(): Int = 1

    class HeaderViewHolder(itemView: View) : GenericViewHolder<RoutineItem.Header>(itemView)
}

DetailAdapter

class DetailAdapter(private val items: List<RoutineItem.Detail> = emptyList())
    : BaseAdapter<RoutineItem.Detail>(initialItem = items) {

    override fun createViewHolder(itemView: View): GenericViewHolder<RoutineItem.Detail> {
        return DetailViewHolder(itemView)
    }

    override fun getItemCount(): Int = items.size

    class DetailViewHolder(itemView: View) : GenericViewHolder<RoutineItem.Detail>(itemView)
}

Activity

class MainActivity : AppCompatActivity() {
    var concatAdpater: ConcatAdapter = ConcatAdapter()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val rv: RecyclerView = findViewById(R.id.rv)

        val adapterItems: ArrayList<Pair<RoutineItem.Header, List<RoutineItem.Detail>>> = arrayListOf()
        val childItems : List<RoutineItem.Detail> = listOf(
            RoutineItem.Detail(),
            RoutineItem.Detail(),
            RoutineItem.Detail(),
            RoutineItem.Detail(),
            RoutineItem.Detail()
        )

        adapterItems.add(Pair(RoutineItem.Header(), childItems))
        adapterItems.add(Pair(RoutineItem.Header(), childItems))
        adapterItems.add(Pair(RoutineItem.Header(), childItems))
        for ((header, list) in adapterItems) { // 1 adapter per group
            concatAdpater.addAdapter(HeaderAdapter(header))
            concatAdpater.addAdapter(DetailAdapter(list))
        }
        rv.adapter = concatAdpater
    }
}

Because it is a test code, there are parts that are not functionally implemented! (Ex. Dynamically adding and deleting items..)

CodePudding user response:

It's always better to use a single adapter because item animation and state changes are way more managable with DiffUtil. Also it's easier to maintain and way more efficient (in terms of speed and resource managment).

More detailed answers:

  • Related