Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

Dynamic recyclerView Android kotlin

In my app i communicate with REST API to get a list of folders and list of pages, each folder have a liste of children's(children can be another folder or a list of pages) so that mean inside 1 folder we can find a hierarchy of folders,in my project for the list of folders i create a fragment and layout with RecycleView and the same for the pages, so my problem is i cant find a solution to display the hierarchy of folders, so i find as solution:

  1. create lot of fragments for the children's of the folder (that's not good because that depend how many children the folder have)
  2. use the RecycleView recursively but i dont know how :/

the project plan is as follows :

- Folder 1
      -Folder 1.1
          -List of pages
       -Folder 1.2
       -Folder 1.2.1
           - List of Pages
       -Folder 1.2.2
            -List of Pages
 - Folder 2
    - List of pages

.....

Folder Adapter

z

var onItemClick: ((Result) -> Unit)? = null
var onItemLongClick: ((Result) -> Boolean)? = null//de ty boolenn

private var folderList = emptyList<Result>()

inner class FolderViewHolder(folderItemView: View) : RecyclerView.ViewHolder(folderItemView) {
    init {
        folderItemView.setOnClickListener {
            onItemClick?.invoke(folderList[adapterPosition])
        }
        folderItemView.setOnLongClickListener {
            onItemLongClick?.invoke(folderList[adapterPosition]) == true//hna fine ajoutitr=e

        }
    }
}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FolderViewHolder {
    return FolderViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.folder_item_layout, parent, false)
    )
}

@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: FolderViewHolder, position: Int) {
    holder.itemView.title_text.text = folderList[position].name
    holder.itemView.date_text.text = "Type :" + folderList[position].type
}

override fun getItemCount(): Int {
    return folderList.size
}

fun folderAdapterLoadData(newList: List<Result>) {
    folderList = newList
    notifyDataSetChanged()
}

PageAdapter

class PagesAdapter : RecyclerView.Adapter<PagesAdapter.PagesViewHolder>() {
    private var pagesList = emptyList<Result>()
    var onItemClick: ((Result) -> Unit)? = null
    var url: String = ""

inner class PagesViewHolder(pagesItemView: View) : RecyclerView.ViewHolder(pagesItemView) {
    init {
        pagesItemView.setOnClickListener {
            onItemClick?.invoke(pagesList[adapterPosition])
        }
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagesViewHolder {

    return PagesViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.page_item_layout, parent, false)
    )
}

override fun onBindViewHolder(holder: PagesViewHolder, position: Int) {
    holder.itemView.title_text.text =pagesList[position].name
    holder.itemView.date_text.text = "Type : "+pagesList[position].type
    holder.itemView.imageView
    url = pagesList[position].zone.image.url
    Picasso.get().load(url+"/0,0,4267,6694/,400/0/default.jpg").into(holder.itemView.imageViewPage)
}

override fun getItemCount(): Int {
    return pagesList.size
}

fun pageAdapterLoadData(newList: List<Result>) {
    pagesList = newList
    notifyDataSetChanged()
}
question from:https://stackoverflow.com/questions/65929065/dynamic-recyclerview-android-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...