buggydroid
buggydroid

Reputation: 1870

Android: Showing cards like layout as in Google Keep

I was wondering how to show the snapshot of my notes, checklists, images in the grid view. I have no trouble creating the grid view but creating a preview of it is the challenge I'm facing right now as I need to add different type of views(text, checklist, imageview) to the grid according to their time of creation. Works fine for a single type of view like image or text only.

Or in other words. How do I add the different views dynamically to the GridView Adapter?

All or any help is appreciated.

Thanks.

enter image description here

Upvotes: 16

Views: 9406

Answers (4)

陈宇明
陈宇明

Reputation: 9

recyclerView= (RecyclerView) findViewById(R.id.recycler);
recyclerView.setLayoutManager(new                  
StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));

        initData();
        MasonryAdapter adapter=new MasonryAdapter(productList);
        recyclerView.setAdapter(adapter);

        SpacesItemDecoration decoration=new SpacesItemDecoration(16);
        recyclerView.addItemDecoration(decoration);

Upvotes: 1

Alexandro
Alexandro

Reputation: 170

Take a look at this recycle view with stagged

This manage make all what you need StaggeredGridLayoutManager or look at this sorce Heterogenous Layouts inside RecyclerView

Upvotes: 1

Gatekeeper
Gatekeeper

Reputation: 7138

More than likely Google is using something similar to GridLayout, if not actually using a GridLayout.

Chances are the layout is also created programmatically to allow for the widgets to be different sizes based on the content that they each display.

Upvotes: 1

Wolfram Rittmeyer
Wolfram Rittmeyer

Reputation: 2402

They surely do not use a GridView for google Keep - but probably an Adapter nonetheless.

To use different Views with your Adapter you can use different View types:

  1. Override getViewTypeCount() to return the number of View types you support.
  2. Return the appropriate type in getItemViewType(int position) for this specific item.
  3. Create the correct View in getView(int position, View convertView, ViewGroup parent).

Keep in mind that the rules for recycling, ViewHolder and so on still apply. For multiple types recycling only happens for the correct type.

Marcus Körner shared a gist by Jake Wharton on G+ recently that might be useful.

Upvotes: 3

Related Questions