Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
List/grid switchable recyclerview doesn't work

I'm trying to create a recyclerview which can change layout (a list or a grid), for that I modified my adapter to determine the which layout it should use according to the number of spans. But when I try to modify the information from the holder nothing is happening. Does anyone have an idea?

here's my adapter:

class FileAdapter(
private val context: Context,
private val layoutManager: GridLayoutManager,
private var items:MutableList<File>,
private val isFolder:Boolean = false
) : RecyclerView.Adapter<FileAdapter.FileViewHolder>() {

companion object {
    private val LIST = 1
    private val GRID = 3
    private val VIEW_TYPE_LIST = 1
    private val VIEW_TYPE_GRID = 2
}


override fun getItemViewType(position: Int): Int {
    val spanCount: Int = layoutManager.spanCount
    return if (spanCount == LIST) {
        VIEW_TYPE_LIST
    } else {
        VIEW_TYPE_GRID
    }
}


override fun onCreateViewHolder(parent: ViewGroup, viewType : Int):FileViewHolder{
    val view : View
    if(!isFolder) {
        if (viewType == VIEW_TYPE_GRID) {
            view = LayoutInflater.from(parent.context).inflate(R.layout.file_element, parent, false)
        } else {
            view = LayoutInflater.from(parent.context).inflate(R.layout.file_list_element, parent, false)
        }
    } else
        view = LayoutInflater.from(parent.context).inflate(R.layout.folder_element, parent, false)

    return FileViewHolder(view, viewType)
}



class FileViewHolder(view : View, viewType: Int) : RecyclerView.ViewHolder(view) {
    var fileName: TextView? = null
    var fileImg: ImageView? = null
    var fileSize: TextView? = null
    init {
        if (viewType == VIEW_TYPE_GRID) {
            val fileName = view.findViewById<TextView>(R.id.file_name)
            var fileImg = view.findViewById<ImageView>(R.id.file_img)
        } else {
            val fileName = view.findViewById<TextView>(R.id.file_name)
            var fileImg = view.findViewById<ImageView>(R.id.file_img)
            var fileSize = view.findViewById<TextView>(R.id.file_size)
        }
    }


}


override fun onBindViewHolder(holder: FileViewHolder, position: Int) {

    val currentFile = items[position]

    holder.fileName?.text = currentFile.name

    if(currentFile.isDirectory)
        //holder.fileImg.setImageResource(getDrawable(R.drawable.ic_folder))
    else if(currentFile.extension=="pdf"){                                
        //holder.fileImg.setImageResource(R.drawable.ic_pdf)
        CoroutineScope(Dispatchers.Default).launch {
            val bitmapPdf = pdfToBitmap(currentFile)
            CoroutineScope(Dispatchers.Main).launch {
                holder.fileImg?.setImageBitmap(bitmapPdf)
            }
        }
    }
    else if(currentFile.extension=="docx")
        holder.fileImg?.setImageResource(R.drawable.ic_docx)
    else if(currentFile.extension=="xlsx")
        holder.fileImg?.setImageResource(R.drawable.ic_xlsx)                  
    else
        holder.fileImg?.setImageResource(R.drawable.ic_file)


}
override fun getItemCount() = items.size

}

Here's the result:
enter image description here

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!