4

How to create multi-layout card view in android? I created same layout card view which is similar to grid view using recycler view and cardview. Now i want to create a view of cards where first card takes full width and some height, others take half width but different heights. As mentioned in below image where each card has varying layouts and dimensions.

Any guidance is really helpful. Any sample code will be great.

enter image description here

Mitesh Sharma
  • 261
  • 4
  • 14

2 Answers2

0

If you are using a RecyclerView to display the cards, you can set the layout manager to a StaggeredGridLayoutManager:

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

// use a linear layout manager
mLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);

If your really want to shake things up, you could change the widths of the grid items dynamically from within the adapter. You would need to use LayoutParams to accomplish this. LayoutParams documentation can be found here: http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

Hope that answers your question!

Shubhang Desai
  • 329
  • 3
  • 17
  • 1
    StaggeredGridLayoutManager only one constructor which is StaggeredGridLayoutManager(int spanCount, int orientation), where i need to define spancount which fixes number of cards come on screen during vertical or horizontal orientation. I need to change this number dynamically based on my card type so that i can have different spanCount in each row of list. How can i achieve this? Any help will be of great help. I can achieve different height elements but row or column needs to be fixed based on spanCount and orientation, which is not issue i want to resolve. – Mitesh Sharma Jul 27 '15 at 20:10
  • Sorry, forgot about those constructor params. You could try having a different RecyclerView each time you want a different spanCount, but that's obviously very messy and not the right way to do it. Here's an answer that may help: http://stackoverflow.com/questions/27744788/changing-number-of-columns-in-recyclerview-gridlayout – Shubhang Desai Jul 27 '15 at 20:20
  • 1
    Thanks man, tried all given solutions .. it didn't work. They just help change spanCount on runtime for one recyclerview, but not for different columns in same view. – Mitesh Sharma Jul 28 '15 at 07:30
  • I see. Sorry I couldn't answer your question correctly. Please keep me updated if you find the solution. – Shubhang Desai Jul 28 '15 at 14:45
0

Take a look at this recycle view with stagged

This manage make all what you need StaggeredGridLayoutManager

Alexandro
  • 170
  • 3
  • 12