android – Asymmetric GridLayout / RecyclerView with GridLayoutManager-ThrowExceptions

Exception or error:

I’m trying to display a grid like this one (graphical rack cabinet representation) with one of the title mentioned methods (help me to find what’s best), but I can’t find a proper solution.

At the moment I’m using a GridLayout inside a ScrollView, first by generating the index column (text views) to then add buttons at the correct index (effective position in which the device is mounted) with span to expand the button on multiple rows where it’s needed (device that occupies more than one rack unit).

With every path I’ve tried, I’ve always encountered the same problems :

  • How to dinamically add buttons that takes more than one row for only one column (the devices one).
  • How to leve blank spaces (only for the devices column) for the units that aren’t used.

Since I’ve finished mine, any idea is welcome.

How to solve:

Use recyclerview and customize your list item height in your onBindViewHolder method of RecyclerView.Adapter like bellow

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val item = values[position]
    // You can customize it as per your requirement
    holder.itemView.layoutParams.height = position*50

Leave a Reply

Your email address will not be published. Required fields are marked *