How to create Google + cards UI in a list view?

前端 未结 7 948
独厮守ぢ
独厮守ぢ 2020-12-12 11:15

I want to create a listView of cards, but after reading this blog post goolge-plus-layout, I\'m cards are a viable solution for a list of anything. The animation part seems

相关标签:
7条回答
  • 2020-12-12 11:56

    You can create a custom drawable, and apply that to each of your listview elements.

    I use this one for cards in my UI. I don't think there is significant performance issues with this approach.

    The drawable code (in drawable\big_card.xml) looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="@color/second_grey" />
            </shape>
        </item>
        <item
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp">
            <shape android:shape="rectangle" >
                <corners android:radius="3dp" />
    
                <solid android:color="@color/card_shadow" />
            </shape>
        </item>
        <item
            android:bottom="12dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp">
            <shape android:shape="rectangle" >
                <corners android:radius="3dp" />
    
                <solid android:color="@color/card_white" />
            </shape>
        </item>
    
    
    </layer-list>
    

    I apply the background to my listview elements like this:

    <ListView
        android:id="@+id/apps_fragment_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:listSelector="@drawable/big_card" />
    

    If you want to add this as a background to any View (not just a list), you just make the custom drawable that View's background element:

    <TextView
            android:id="@+id/any_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/big_card" />
    
    0 讨论(0)
  • 2020-12-12 12:00

    There is a library that helps creating UI cards in android https://github.com/gabrielemariotti/cardslib

    0 讨论(0)
  • 2020-12-12 12:03

    As "android developer" briefly mentions in his answer, the CardView class can be used to easily create card views.

    Just wrap you UI widgets in a CardView element and you are ready to go. See the short introduction to the CardView widget at https://developer.android.com/training/material/lists-cards.html#CardView.

    The CardView class requires a v7 support library, remember to add the dependencies to your .gradle file!

    compile 'com.android.support:cardview-v7:21.0.+'
    
    0 讨论(0)
  • 2020-12-12 12:08

    EDIT: Google provides a nice class called CardView. I didn't check it but it looks promising.

    Here's the previous way, which also works fine (that's what I wrote before the edit) :

    There is a nice tutorial here and a nice sample of it here .

    in short , these are the files you can create:

    listView definition:

    android:divider="@null"
    android:dividerHeight="10dp"
    android:listSelector="@android:color/transparent" 
    android:cacheColorHint="@android:color/transparent"
    android:headerDividersEnabled="true"
    android:footerDividersEnabled="true"
    

    also this:

    m_list.addHeaderView(new View(this));
    m_list.addFooterView(new View(this));
    

    res/drawable/selector_card_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item
          android:state_pressed="true"
          android:drawable="@drawable/layer_card_background_selected" />
    
       <item android:drawable="@drawable/layer_card_background" />
    </selector>
    

    listView item :

    res/layout/list_item_card.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:paddingLeft="15dp"
       android:paddingRight="15dp"
       android:descendantFocusability="beforeDescendants">
    
       <LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:paddingLeft="15dp"
          android:paddingTop="15dp"
          android:paddingBottom="15dp"
          android:paddingRight="15dp"
          android:background="@drawable/selector_card_background"
          android:descendantFocusability="afterDescendants">
    
          <TextView
             android:id="@+id/text1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
    
          <TextView
             android:id="@+id/text2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
    
          <TextView
             android:id="@+id/text3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
       </LinearLayout>
    </FrameLayout>
    

    res/drawable/layer_card_background_selected.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#CABBBBBB"/>
                <corners android:radius="2dp" />
            </shape>
        </item>
    
        <item
            android:left="0dp"
            android:right="0dp"
            android:top="0dp"
            android:bottom="2dp">
            <shape android:shape="rectangle">
                <solid android:color="#CCCCCC"/>
                <corners android:radius="2dp" />
            </shape>
        </item>
    </layer-list>
    

    res/drawable/layer_card_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#CABBBBBB"/>
                <corners android:radius="2dp" />
            </shape>
        </item>
    
        <item
            android:left="0dp"
            android:right="0dp"
            android:top="0dp"
            android:bottom="2dp">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/white"/>
                <corners android:radius="2dp" />
            </shape>
        </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-12-12 12:10

    maybe you can try this cardPlusUI

    I have used this for some my project

    0 讨论(0)
  • 2020-12-12 12:11

    If all you want is a ListView that simulates the cards-look you can use a 9-patch as background for your listitems to make them look like cards. You can find a 9-patch and some more tips and explanation here: http://www.tiemenschut.com/simply-get-cards-ui-look/

    0 讨论(0)
提交回复
热议问题