0

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()
}
Mactavisch
  • 3
  • 1
  • 4
  • I am not really sure what are you trying to achieve since there are only adapters here, but I am not sure if the path you have taken is a great one. This can have a serious impact on your performance. I think you need to take a step back and figure out what do you want your users to see and how to use that information. There is something called ExpandableList where Group (parent) can have ExpandableList as a child. But for a large set of data, this isn't great either. There is a lot of ways to implement this but you need to be exact. This is the philosophic answer, but give me more. – SlothCoding Jan 28 '21 at 00:41
  • my question is to find a solution to display this hierarchy of folders as the hierarchy of folders in windows system, am not sure what should i do, am asking for a solution to develop this hierarchy, ( i receive all of this data from REST API, the objectif of this project is to display the list of pages(images) but to display this pages the user need to navigate between folders. i wish you understand me . – Mactavisch Jan 28 '21 at 01:25
  • If you want it to be as on Windows then just use fragments with lists or recyclerview or grid view whatever you want. The first fragment shows all of your parent folders, in the above example Folder1 and Folder2. On click open another fragment showing child data, in the first example is Folder1.1, Folder1.2. This layout can reuse the first fragment since you are showing the same type of data. When you come to a list of images, use RecylcerView to show them. Just check each child before you call fragment. I don't have many chars left, do you understand this? – SlothCoding Jan 28 '21 at 10:23
  • yes i understood i think is good idea but i wish the navigation back to previous element will work in this case. – Mactavisch Jan 28 '21 at 13:49
  • Of course it will, just use getSupportFragmentManager().beginTransaction().add() to add new fragment. Then the back button will just go back to the previous fragment created. You can work your logic there and if you are stuck just post a question and we'll help. – SlothCoding Jan 28 '21 at 14:00
  • i did it with 2 additional fragment i dont know if is it a good practice but its work thank you – Mactavisch Jan 29 '21 at 17:04
  • Why shouldn't it be a good practice? Fragments are light components that can be reused over and over again and they are great. Happy coding! – SlothCoding Jan 29 '21 at 18:44
  • 1
    thank you for your advices :) – Mactavisch Feb 01 '21 at 16:57

0 Answers0