mikepenz/FastAdapter

Is there any way to add multiple row?

yilmazgokhan opened this issue · 5 comments

About this issue

I will use Native Ad on my app. I need to add 2 type in the adapter but I didn't find any sample about that. Is that possible or is there any sample code about that?

implementation "com.mikepenz:fastadapter:5.3.5" implementation "com.mikepenz:fastadapter-extensions-binding:5.3.5" implementation "com.mikepenz:fastadapter-extensions-expandable:5.3.5" implementation "com.mikepenz:fastadapter-extensions-utils:5.3.5"

Checklist

I am not 100% sure I understand this report.

If you have a RV item which is meant to always have 2 rows, you can adjust its layout to look like this.

If you have different types of items inside the RV you can do so by having a more generic adapter, allowing different item types:
https://github.com/mikepenz/FastAdapter/blob/develop/app/src/main/java/com/mikepenz/fastadapter/app/AdvancedSampleActivity.kt

Hi @mikepenz thanks for your reply. I will review the class you sent. I am attaching more detailed information about my problem below.

I have a list with 200 items. While showing these items in Recycler View -for example- I want to show a Native Ad after showing 10 items. I mean 10 items - 1 Native Ad - 10 items - 1 Native Ad - ..

I tried something like that:

class AnAdapter : AbstractItem<RecyclerView.ViewHolder>() {

    //region vars
    ...
    var flag: Boolean = true
    //endregion

    override val layoutRes: Int
        get() {
            return if (flag)
                R.layout.item_an
            else
                R.layout.ad_unified
        }

    override val type: Int
        get() = R.id.fastadapter_item_adapter

    override fun getViewHolder(v: View): RecyclerView.ViewHolder {
        return if (flag)
            ViewHolder(v)
        else
            ViewHolderNativeAd(v)
    }

    class ViewHolder(view: View) : FastAdapter.ViewHolder<AnAdapter>(view) {

        ...

        override fun bindView(item: AnAdapter, payloads: List<Any>) {
            //Set UI
        }

        override fun unbindView(item: AnAdapter) {
            //Clear UI
        }
    }

    class ViewHolderNativeAd(view: View) : FastAdapter.ViewHolder<AnAdapter>(view) {

        ...

        override fun bindView(item: AnAdapter, payloads: List<Any>) {
            //Set ad UI
        }

        override fun unbindView(item: AnAdapter) {
            //Clear ad UI
        }
    }
}

I checked the layout with the flag value. But "ViewHolderNativeAd" never works. I mean, that the "ViewHolder" layout is showed on each row of the Recycler View. When the flag is false, it still shows an empty ViewHolder instead of ViewHolderNativeAd.

@yilmazgokhan I would not recommend doing it like this. if you have 2 different layouts use 2 different items. the recyclerview reuses views from the same type, and getViewHolder will only be called when an item is created initially, and afterwards the views are always re-used (that's not part of the library but how RecyclerViews work).

you may also want to refer to the RecyclerView library and it's internals: https://developer.android.com/guide/topics/ui/layout/recyclerview

This library only provides the Adapter part (which is one part of the RecyclerView), more specifically this library helps with structuring the code better by making it easy to write re-useable items.

I shared a medium article about how to show Native Ad as an item in Recycler View without any library, taking reference from the documents shared by Google. Can you take a look at this gist about this side?

https://gist.githubusercontent.com/yilmazgokhan/e86c684354c96afc0a4e9b4bc916d728/raw/853ce279e5d1ac78d043c57a8513aa19c2a483ee/CountryAdapter.java

I tried something similar in fast-adapter, but unfortunately not the same output. I will try it again with 2 different items as you said. Thanks @mikepenz