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:
- create lot of fragments for the children's of the folder (that's not good because that depend how many children the folder have)
- 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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…