對(duì)于Android Studio導(dǎo)入比較簡(jiǎn)單,在build.gradle(Module:app)中添加如下代碼:
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:recyclerview-v7:21.0.0' compile 'com.android.support:cardview-v7:21.0.0' }紅色標(biāo)記為添加的代碼,添加好后,我們就可以使用其控件。
2.單獨(dú)使用RecyclerView
布局文件中導(dǎo)入其控件:
<android.support.v7.widget.RecyclerView android:id="@+id/lyj_recycler" android:scrollbars="vertical" android:layout_below="@+id/activity_main_toolbar" android:layout_width="match_parent" android:padding="5dp" android:layout_height="wrap_content"/>都是一些基本的屬性,與ListView并無(wú)不同。
因?yàn)楣雀鑼?shí)現(xiàn)RecyclerView是擴(kuò)展ListView功能,使其功能更加的豐富,所以,使用RecylerView也需要一個(gè)適配器Adapter,RecyclerView適配器為RecyclerView.Adapter,代碼基本格式如下:
public class LYJAdapter extends RecyclerView.Adapter<LYJAdapter.ViewHolder> { @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return null; } @Override public void onBindViewHolder(ViewHolder holder, int position) { } @Override public int getItemCount() { return 0; } class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View view){ super(view); } }}其適配器強(qiáng)制我們使用ViewHolder模式優(yōu)化ListView。當(dāng)然優(yōu)點(diǎn)就應(yīng)該強(qiáng)制使用。其使用方式與BaseAdapter大同小異。
下面我們來(lái)實(shí)現(xiàn)自身的適配器,代碼如下:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { PRivate List<MusicItem> data; public MyAdapter(List<MusicItem> data) { this.data = data; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { //綁定布局 View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cv_main, null); //創(chuàng)建ViewHolder ViewHolder viewHolder = new ViewHolder(itemLayoutView); return viewHolder; } @Override public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) { viewHolder.info.setText(data.get(i).getTitle()); viewHolder.image.setImageResource(data.get(i).getResId()); } @Override public int getItemCount() { return data.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView info; public ImageView image; public ViewHolder(View itemLayoutView) { super(itemLayoutView); info = (TextView) itemLayoutView.findViewById(R.id.lyj_txt); image=(ImageView)itemLayoutView.findViewById(R.id.lyj_image); } }}布局cv_main代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp"> <ImageView android:id="@+id/lyj_image" android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_marginRight="5dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/lyj_txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/lyj_image" android:layout_gravity="center" /></RelativeLayout>RecyclerView的實(shí)體類(lèi)如下:
public class MusicItem { private String title; private int resId; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getResId() { return resId; } public void setResId(int resId) { this.resId = resId; }}其實(shí)RecyclerView的優(yōu)點(diǎn)就是將ListView的items布局分離出來(lái),好讓我們實(shí)現(xiàn)如下三種效果。
GridLayoutManager(網(wǎng)格布局)
下面的代碼我們將Activity界面初始化分離出來(lái):
public void initView() { initActionBar(); String[] strTitle = {"慵懶慢時(shí)光,爵士中文", "【華語(yǔ)】曾經(jīng)的你已杳無(wú)音信", "粵語(yǔ)歌中的華語(yǔ)鏡像", "凡走過(guò)必留下痕跡", "搖滾Live也可以溫柔到骨子里", "人生中的舍得與難舍"}; int[] resId={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six}; for(int i=0;i<6;i++){ MusicItem item=new MusicItem(); item.setTitle(strTitle[i]); item.setResId(resId[i]); musicItems.add(item); } recyclerView = (RecyclerView) findViewById(R.id.lyj_recycler); RecyclerView.LayoutManager layout = new GridLayoutManager(this,3);//網(wǎng)格布局,每行為3 recyclerView.setLayoutManager(layout); recyclerView.setHasFixedSize(true);//適配器內(nèi)容改變,不會(huì)改變RecyclerView的大小 adapter = new MyAdapter(musicItems); recyclerView.setAdapter(adapter);}對(duì)于設(shè)置ListView的適配器代碼不需要過(guò)多解釋?zhuān)渌拇a都有注釋。運(yùn)行后界面如下(是不是有網(wǎng)易云音樂(lè)首首頁(yè)選項(xiàng)列表的感覺(jué)):

LinearLayoutManager(垂直布局、水平布局)
只需要更改上面代碼中網(wǎng)格布局的那行代碼,如下:
RecyclerView.LayoutManager layout=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
參數(shù)1:上下文
參數(shù)2:水平還是垂直
參數(shù)3:為false表示數(shù)據(jù)按輸入的順序顯示,為true表示數(shù)據(jù)逆向顯示。
運(yùn)行程序當(dāng)?shù)玫饺缦陆缑妫?/p>

當(dāng)你將參數(shù)2更改成垂直,那么運(yùn)行后的界面就如listView一樣了。這里就不截圖了。
StaggeredGridLayoutManager(瀑布流布局)
如2(二)所示,也只需要更改一條代碼即可:
StaggeredGridLayoutManager layout=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
參數(shù)1:為每行幾個(gè)元素。
參數(shù)2:為垂直瀑布還是水平瀑布
運(yùn)行后界面如下:

這里不好看的原因是沒(méi)有結(jié)合CardView一起使用,下面將介紹如何整合RecyclerView與CardView。
3.當(dāng)RecyclerView遇上CardView
CardView繼承自Framelayout,所以FrameLayout所有屬性CardView均可以直接拿來(lái)用,不過(guò)CardView還有自己獨(dú)有的屬性。下面我們將item布局的父標(biāo)簽換成CardView。并且用RelativeLayout包裹兩個(gè)子控件。代碼如下:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/lyj_image" android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_marginRight="5dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/lyj_txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/lyj_image" android:layout_marginTop="40dp" /> </RelativeLayout></android.support.v7.widget.CardView>運(yùn)行效果圖如下:

其每個(gè)item變成了一個(gè)卡片的樣式,如果色彩搭配的好,那么界面將如上線的APP一樣酷炫。
下面來(lái)介紹CardView獨(dú)有的屬性:
app:cardElevation 陰影的大小 app:cardMaxElevation 陰影最大高度 app:cardBackgroundColor 卡片的背景色 app:cardCornerRadius 卡片的圓角大小 app:contentPadding 卡片內(nèi)容于邊距的間隔?card_view:contentPaddingBottom app:contentPaddingTop app:contentPaddingLeft app:contentPaddingRight app:contentPaddingStart app:contentPaddingEnd app:cardUseCompatPadding 設(shè)置內(nèi)邊距,V21+的版本和之前的版本仍舊具有一樣的計(jì)算方式 app:cardPreventConrerOverlap 在V20和之前的版本中添加內(nèi)邊距,這個(gè)屬性為了防止內(nèi)容和邊角的重疊下面簡(jiǎn)單設(shè)置幾個(gè)屬性:
app:cardBackgroundColor=”#EEC900″:卡片背景為黃色。app:cardCornerRadius=”10dp”:卡片圓角半徑為10dp。app:cardPreventCornerOverlap=”true”:防止內(nèi)容與邊角重疊app:cardUseCompatPadding=”true”:設(shè)置邊距app:contentPadding=”10dp”:邊距的間隔大小為10dp運(yùn)行一下效果圖如下所示:

轉(zhuǎn)載請(qǐng)注明:Android開(kāi)發(fā)中文站 ? RecyclerView與CardView的使用
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注