The Google I/O 2014 conference announced the Android L system, as well as the material design's new style. and material design card-style designs. Google Play App Store and g+ The app has become such a card style. To tie in with material Design, Google intentionally added Recyclerview and CardView to the SDK, except that it was added to the Support-v7 library, possibly to accommodate the older version number. Google official has said clearly how to use, here: https://developer.android.com/preview/material/get-started.html, just not complete.
Here's how to use Recyclerview and CardView to display a card-style listview. The idea is exactly the same as the ListView. I'm using Android Studio, with Eclipse's friends ... Go to Android Studio and get to it.
1, the SDK is updated to the latest. API 20 and build Tool 20.0.0
2, create a common Project,api level>=15.
SUPPORT-V7 is introduced into the 3,build.gradle.
dependencies { compile ' com.android.support:recyclerview-v7:+ ' compile ' com.android.support:cardview-v7:+ '}
The following error is assumed to be the only way to write:
Error:execution failed for task ': App:processdebugmanifest '.
> Manifest merger failed:uses-sdk:minsdkversion cannot be smaller than version L declared in library Com.android.s Upport:recyclerview-v7:21.0.0-rc1
This is due to the import of V7 21.0.0rc1, but this does not support API 15. There is a hack workaround: write in the Androidmanifest.xml file:
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " package=" robam.org.ltest "><uses-sdk tools:node=" Replace "/></manifest >
Click sync button to import V7 's Recyclerview and CardView into project.
4, Next, the activity of the layout file, equivalent to the ListView:
<android.support.v7.widget.recyclerview android:id= "@+id/my_recycler_view" android:layout_width= " Match_parent " android:layout_height=" match_parent " android:scrollbars=" vertical "/>
5, in the next, write the item layout file, the item layout root node is CardView, so there will be a card-style shadow effect.
<?xml version= "1.0" encoding= "Utf-8"? ><android.support.v7.widget.cardview xmlns:android= "/http Schemas.android.com/apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools="/HTTP/ Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "android:layou t_marginleft= "16DP" android:layout_marginright= "16DP" android:layout_margintop= "4DP" android:layout_marginbottom= "4DP" app:cardcornerradius= "3DP" > <textview android:id= "@+id/text" android:layout_width= "match_p Arent "android:layout_height=" wrap_content "android:paddingleft="?Android:listpreferreditempaddingleft "android:paddingright="? Android:listpreferreditempaddingright "Android: paddingtop= "16DP" android:paddingbottom= "16DP" android:textcolor= "@android: Color/holo_blue_light" too ls:text= "Item"/></android.support.v7.widget.cardview>
6, it's time to write activity. For example, the following:
public class Mainactivity extends Activity {public static final list<string> data; static {data = new arraylist<string> (); Data.add ("Pompous-Eason Chan"); Data.add ("Long time no see-Eason Chan"); Data.add ("Where the time has gone-Reno"); Data.add ("Miss Dong-Song Dongno"); Data.add ("Love as Tide-Jeff Chang"); Data.add ("Give me a Song of time-Jay Chou"); Data.add ("Dark black-stefanie Sun"); Data.add ("Pity not You-fish Leong"); Data.add ("Too wronged-Tao crystal clear"); Data.add ("Diligent-Zhang Yu"); Data.add ("Lying-lin Jia"); Data.add ("Exclusive Memory-Kikiaelita"); } Linearlayoutmanager LayoutManager; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Get recyclerview Recyclerview Recyclerview = (recyclerview) Findviewbyid (R.id.my_recycler_view); Recyclerview.sethasfixedsize (TRUE); LayoutManager = new Linearlayoutmanager (this); Recyclerview.setlayoutmanager (LayoutManager); RecycLerview.setadapter (new Myadapter (data)); @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem item) {return super.onoptionsitemselected (item); } private static Class Myadapter extends Recyclerview.adapter<myadapter.viewholder> {private list<st Ring> data; Public Myadapter (list<string> data) {this.data = data; } @Override Public Viewholder oncreateviewholder (viewgroup viewgroup, int i) {//loads the layout of item. Used in Layout of the real CardView. View view = Layoutinflater.from (Viewgroup.getcontext ()). Inflate (r.layout.item_robam_l, ViewGroup, false); Viewholder the number of references must be the root node of item. return new Viewholder (view); } @Override public void Onbindviewholder (Viewholder viewholder, int i) {ViewHolder.text.setText ( DatA.get (i)); } @Override public int getitemcount () {return data.size (); } static class Viewholder extends Recyclerview.viewholder {TextView text; Public Viewholder (View itemview) {//Super This parameter must be aware that the root node of item has to be. Otherwise there will be inexplicable FC. Super (Itemview); Text = (TextView) Itemview.findviewbyid (R.id.text); } } }}
After that, the effect is exactly the same as the ListView.
The above source code stamp here: Http://pan.baidu.com/s/1mgoN8uc
This sample is from: Https://github.com/evant/recyclerview-sample/tree/master
Also enclosed is an English course: http://antonioleiva.com/recyclerview/
In fact, there are many foreign Daniel wrote an open source library has achieved this effect, and there are a lot of card effect. Explore it slowly.
Https://github.com/gabrielemariotti/cardslib
Android Recyclerview and CardView