1

I have a NestedScrollView that contains a variety of layouts one of them being a recycler view. The hierarchy is something like shown below.

NestedScrollView
    ConstraintLayout
        TextView
        Many other layouts
        RecyclerView

From various articles I know this is a bad idea trying to have 2 scrolling views in the same direction and to solve it what I've come across is creating a layout out of all the other views, removing the ScrollView and then adding the layout as an item to the RecyclerView. However, other views have complicated logic. Would it be a terrible idea to convert the layout into a fragment and add that as an item to the RecyclerView? Is it a bad coding practice to follow this route?

The goal of this is to make sure the RecyclerView actually recycles its views.

Thanks

Saurabh
  • 426
  • 1
  • 7
  • 18
  • extends recylerview and expand it – Iqbal Rizky Aug 06 '19 at 19:22
  • https://stackoverflow.com/questions/27475178/how-do-i-make-wrap-content-work-on-a-recyclerview – Iqbal Rizky Aug 06 '19 at 19:27
  • this is different than my use case. I have two scrolls in the same direction and so setting height for recycler view as `wrap_content` creates all the items instead of creating only the visible ones – Saurabh Aug 06 '19 at 20:15

1 Answers1

0

You can create a custom adapter where you set your layout individually:

class Adapter(var dataset: MutableList<String> = mutableListOf(), var context: Context?) :
RecyclerView.Adapter<BrowseAdapter.ViewHolder>() {

class ViewHolder(val layout: RelativeLayout) : RecyclerView.ViewHolder(layout)

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

    val cont = LayoutInflater.from(parent.context)
        .inflate(R.layout.browse_item, parent, false) as RelativeLayout

    return ViewHolder(cont)
}


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

}

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


}
Lukas
  • 812
  • 1
  • 14
  • 43
  • I understand using different layouts but the business logic for each of those layouts is too much to include in a single adapter. Which is why I'm asking about fragments. – Saurabh Aug 06 '19 at 22:53