Search code examples
androidandroid-recyclerviewcardslib

In general, how do I use a custom card with a recyclerview?


I want to use cardslib with an existing RecyclerView.

https://github.com/gabrielemariotti/cardslib

I have a working program with the standard android CardView and I want to instead use the cardslib cards. I have a ViewAdapter class, and I am guessing that I will need to change a few methods in there, and of course my layout file for the cards. Would that be it? Which methods would I change, and in general, how should I change it?

I think I will need to change my

1) inner class CardDataObjectHolder which extends RecyclerView.Viewholder

2) my onCreateViewHolder which returns the CardDataObjectHolder object

and 3) my onBindViewHolder

I'd love to see some generic code that would go in those, plus any other methods I may have overlooked.

Thanks!

Edit: Ahh, I have just seen that cardslib also has a custom recyclerview. I will give that a try tonight. Can probably hold off on answers here for a bit.


Solution

  • In general check before if you really need the additional functionality that the cardlib can offer.

    The library has a module which works with the RecyclerView.

    Just add in your dependencies:

    dependencies {
        //Optional for RecyclerView
        compile 'com.github.gabrielemariotti.cards:cardslib-recyclerview:2.1.0'
    }
    

    Here you can find all the info but it is quite simple:

    • Use a CardRecyclerView instead of a RecyclerView in your layout
    • Build your CardArrayRecyclerViewAdapter
    • Bind the adapter to the view mRecyclerView.setAdapter(mCardArrayAdapter);

    The library provides a BaseRecyclerViewAdapter if you would like to customize the adapter.