Use the GitHub dependency library to implement Android5.0 new features--cardview

Source: Internet
Author: User

The CardView effect was achieved by using the CardView of the support-v7. Later on GitHub found someone who wrote good library support, perfect for cardview backwards compatibility. SUPPORT-V7 is no longer needed and is more convenient to implement. The Reference GitHub homepage is: Https://github.com/gabrielemariotti/cardslib. Thank the blogger. The specific implementation steps are as follows:

(1) Create a new project in Android Studio, add the following code to Build.gradle (Module:app), and note that it is in the gradle of a Module, not in the gradle of project above.

dependencies {    Compile filetree (dir: ' Libs ', include: [' *.jar '])    compile ' com.android.support:appcompat-v7 : 22.2.0 '    //core Card library    compile ' com.github.gabrielemariotti.cards:cardslib-core:2.1.0 '    // Optional for built-in cards    compile ' com.github.gabrielemariotti.cards:cardslib-cards:2.1.0 '    //optional for Recyclerview    compile ' com.github.gabrielemariotti.cards:cardslib-recyclerview:2.1.0 '    //optional for Staggered grid view support    compile ' com.github.gabrielemariotti.cards:cardslib-extra-staggeredgrid:2.1.0 '    //optional for drag and drop support    compile ' com.github.gabrielemariotti.cards:cardslib-extra-dragdrop : 2.1.0 '    //optional for Twowayview support (coming soon)    //compile ' com.github.gabrielemariotti.cards: cardslib-extra-twoway:2.1.0 '}
The code above is to add a library dependency to the project, just as adding a library dependency from Support-v7, after adding the code, rebuilding the project from Build-->rebuild Project, and then downloading the dependent library from GitHub, the process may be a bit slow and wait patiently. After that, you'll find many more files in the external libraries.


(2) write the XML layout file as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > < It.gmariotti.cardslib.library.view.CardViewNative android:id= "@+id/carddemo" android:layout_width= "Match_par Ent "android:layout_height=" wrap_content "android:layout_marginleft=" 12DP "android:layout_marginright            = "12DP" android:layout_margintop= "12DP" > <textview android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "This is the first CardView" android:textsize= "20SP"/&G        T <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an Droid:layout_gravity= "Right" android:src= "@mipmap/image"/> <edittext android:layout_wi Dth= "Match_parent" android:layout_height= "Wrap_Content "android:layout_gravity=" bottom "android:hint=" input box: Please enter "/> <button Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "bo Ttom|right "android:text=" This is the first button "/> </it.gmariotti.cardslib.library.view.CardViewNative> <i T.gmariotti.cardslib.library.view.cardviewnative android:id= "@+id/carddemo2" android:layout_width= "Match_par Ent "android:layout_height=" wrap_content "android:layout_marginleft=" 12DP "android:layout_marginright            = "12DP" android:layout_margintop= "12DP" > <textview android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "This is the second CardView" android:textsize= "20SP"/&G        T <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an Droid:layout_gRavity= "Right" android:src= "@mipmap/image2"/> <edittext android:layout_width= "Match_pa Rent "android:layout_height=" wrap_content "android:layout_gravity=" Bottom "android:hint=" Input box: Please enter the content "/> <button android:layout_width=" wrap_content "android:layout_height=" Wrap_c Ontent "android:layout_gravity=" Bottom|right "android:text=" This is the second button "/> </it.gmariotti.card Slib.library.view.cardviewnative></linearlayout>

(3) Write Java code:

public class Mainactivity extends Activity {    private context context;//context;    @Override    protected void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        This.context = this;        Card = new card (context);//create a card, this is a cards, then put the card into the CardView space;        Cardheader header = new Cardheader ( context);//create a cardheader; If you do not use header headings, you can also annotate the code;        Card.addcardheader (header)//add header to card; add caption to Annotate ;        //set card in the CardView        cardviewnative CardView = (cardviewnative) Findviewbyid (R.id.carddemo);        Cardviewnative cardView2 = (cardviewnative) Findviewbyid (R.ID.CARDDEMO2);        Cardview.setcard (card);//bind;        cardview2.setcard (card);}    }

(4) Then run the code, you can achieve the effect of multiple card cards on the interface. The use is very flexible and can be freely edited individually:


This case is just a demonstration, the overall is relatively simple, the later beautiful layout can be achieved through the card. Again, many references to the code: Https://github.com/gabrielemariotti/cardslib. Thanks Gabriele Mariotti.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use the GitHub dependency library to implement Android5.0 new features--cardview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.