-4

I want load data from server and show it into Recyclerview but I should show RecyclerView such as this design :
Click link to see image

My json :

{
    "android": [{
        "ver": "1.5",
        "name": "Cupcake",
        "api": "API level 3"
    }, {
        "ver": "1.6",
        "name": "Donut",
        "api": "API level 4"
    }, {
        "ver": "2.0 - 2.1",
        "name": "Eclair",
        "api": "API level 5 - 7"
    }, {
        "ver": "2.2",
        "name": "Froyo",
        "api": "API level 8"
    }, {
        "ver": "2.3",
        "name": "Gingerbread",
        "api": "API level 9 - 10"
    }, {
        "ver": "3.0 - 3.2",
        "name": "Honeycomb",
        "api": "API level 11 - 13"
    }, {
        "ver": "4.0",
        "name": "Ice Cream Sandwich",
        "api": "API level 14 - 15"
    }, {
        "ver": "4.1 - 4.3",
        "name": "JellyBean",
        "api": "API level 16 - 18"
    }, {
        "ver": "4.4",
        "name": "KitKat",
        "api": "API level 19"
    }, {
        "ver": "5.0 - 5.1",
        "name": "Lollipop",
        "api": "API level 21 - 22"
    }, {
        "ver": "6.0",
        "name": "Marshmallow",
        "api": "API level 23"
    }, {
        "ver": "7.0 - 7.1",
        "name": "Nougat",
        "api": "API level 24 - 25"
    }]
}

I want show fist data into big layout and when get new data from server, show previous in small layout and show new data into big layout.

I know I want use Multipiew view in recyclerview, but how??!

I am amateur and I really nned this, please help me

Community
  • 1
  • 1
Dongle
  • 23
  • 1
  • 4
  • 1
    You would get the answer with a basic query of google. like this http://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – schinj May 16 '17 at 10:16

1 Answers1

1

You need to use a LayoutManager to achieve what you want.

For your case, you may want to use a GridLayoutManager, because it allows yo define a span size for each item.

The span for an item in a GridLayoutManager is the amount of GridLayoutManager.span this item will take.

For example, if your GridLayoutManager has a span of 2, your first item (the one full width) will have a span of 2, and the next ones will have a span of 1.

Edit :

Assuming you declared layoutManager and recyclerView fields previously in your code, here is the few lines that should do what you want :

layoutManager = new GridLayoutManager(getContext(), 2)  {
    @Override
    public boolean canScrollVertically() {
        return false;
    }
};

/**
* overriding getSpanSize allows to have different number of items per rows
* this is useful since you want one item full width on first line and 2 items on other lines and take
* more size than other items.
* @param position the position of the {@link RecyclerView} item in the list of items
* @return the span size
*/
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        // if position is 0, i.e., first item of recycler, it has span of 2. 
        // Else, it has span of 1 (1/2 of the total GridLayoutManager span)
        return (position == 0) ? 2 : 1;
    }
});

// then tell the recyclerview to display itself using its layout manager
recyclerView.setLayoutManager(layoutManager);
Maxime Flament
  • 721
  • 1
  • 7
  • 24
  • If this solved your problem, please accept this ansxer so further users can quickly know that my answer is a solution to this problem. Also, please read the FAQ before you post, because your question doesn't respect StackOverflow standards. – Maxime Flament May 16 '17 at 17:04
  • Thanks man, but not found `setSpanSizeLookup` method. can you send me this method codes? please – Dongle May 17 '17 at 05:57