Groupie: grid layout manager

Created on 30 Sep 2016  Â·  6Comments  Â·  Source: lisawray/groupie

Is a grid layout manager supported. To order the groups as a grid?

question

Most helpful comment

@lisawray is right, but that is only part of the answer. things have probably changed in the passed 4 years hahaha...

when you're setting up your RecyclerView

// set the layout manager on recycler view
val layoutManager = GridLayoutManager(this, 3)
binding.recyclerview.layoutManager = layoutManager

// set groupie adapter on recycler view
val adapter = GroupAdapter<GroupieViewHolder>()
binding.recyclerview.adapter = adapter

// tell the adapter what spancount you're using
adapter.spanCount = layoutManager.spanCount

// tell layout manager to get item span sizes from groupie adapter
layoutManager.spanSizeLookup = adapter.spanSizeLookup

when you implement your groupie items

class HeaderItem(private val headerText: String) : BindableItem<ListitemHeaderBinding>() {

    override fun getLayout():Int = R.layout.listitem_header

    override fun initializeViewBinding(view:View) = ListitemHeaderBinding.bind(view)

    override fun bind(viewBinding:ListitemHeaderBinding, position:Int)
    {
        viewBinding.title.text = headerText
    }

    // for the sake of this example.....override, and call super...
    // by default, super will make the item span accross all columns
    override fun getSpanSize(spanCount: Int, position: Int): Int =
        super.getSpanSize(spanCount, position)
}

class ContentItem(private val description: String) : BindableItem<ListitemContentBinding>() {

    override fun getLayout():Int = R.layout.listitem_content

    override fun initializeViewBinding(view:View) =  ListitemContentBinding.bind(view)

    override fun bind(viewBinding:ListitemContentBinding, position:Int)
    {
        viewBinding.description.text = description
    }

    // each content item should only span one column
    override fun getSpanSize(spanCount: Int, position: Int): Int = 1
}

All 6 comments

Yes! Just set a GridLayoutManager on your RecyclerView and then override Item.getSpanSize() to tell each item how many columns it should span.

And a grid inside the section will work as well?

But the sample sections are orderered vertical and not as a Grid.

There are examples of both. The line I sent you creates a column with the
items ordered as a typical grid, filling the rows left to right.

On Fri, Sep 30, 2016 at 12:35 PM, Fabian Terhorst [email protected]
wrote:

But the sample sections are orderered vertical and not as a Grid.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/Genius/groupie/issues/5#issuecomment-250791675, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ABZesbRb39H9ZLzUdLRmy1ZQkPdpvkv6ks5qvTpngaJpZM4KKjgG
.

@lisawray is right, but that is only part of the answer. things have probably changed in the passed 4 years hahaha...

when you're setting up your RecyclerView

// set the layout manager on recycler view
val layoutManager = GridLayoutManager(this, 3)
binding.recyclerview.layoutManager = layoutManager

// set groupie adapter on recycler view
val adapter = GroupAdapter<GroupieViewHolder>()
binding.recyclerview.adapter = adapter

// tell the adapter what spancount you're using
adapter.spanCount = layoutManager.spanCount

// tell layout manager to get item span sizes from groupie adapter
layoutManager.spanSizeLookup = adapter.spanSizeLookup

when you implement your groupie items

class HeaderItem(private val headerText: String) : BindableItem<ListitemHeaderBinding>() {

    override fun getLayout():Int = R.layout.listitem_header

    override fun initializeViewBinding(view:View) = ListitemHeaderBinding.bind(view)

    override fun bind(viewBinding:ListitemHeaderBinding, position:Int)
    {
        viewBinding.title.text = headerText
    }

    // for the sake of this example.....override, and call super...
    // by default, super will make the item span accross all columns
    override fun getSpanSize(spanCount: Int, position: Int): Int =
        super.getSpanSize(spanCount, position)
}

class ContentItem(private val description: String) : BindableItem<ListitemContentBinding>() {

    override fun getLayout():Int = R.layout.listitem_content

    override fun initializeViewBinding(view:View) =  ListitemContentBinding.bind(view)

    override fun bind(viewBinding:ListitemContentBinding, position:Int)
    {
        viewBinding.description.text = description
    }

    // each content item should only span one column
    override fun getSpanSize(spanCount: Int, position: Int): Int = 1
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jordond picture jordond  Â·  6Comments

spectrumIG picture spectrumIG  Â·  5Comments

MirzaAhmedBaig picture MirzaAhmedBaig  Â·  6Comments

lisawray picture lisawray  Â·  7Comments

hardysim picture hardysim  Â·  3Comments