對于Android Studio導入比較簡單,在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' }紅色標記為添加的代碼,添加好后,我們就可以使用其控件。
2.單獨使用RecyclerView
布局文件中導入其控件:
<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并無不同。
因為谷歌實現RecyclerView是擴展ListView功能,使其功能更加的豐富,所以,使用RecylerView也需要一個適配器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);        }    }}其適配器強制我們使用ViewHolder模式優化ListView。當然優點就應該強制使用。其使用方式與BaseAdapter大同小異。
下面我們來實現自身的適配器,代碼如下:
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);        //創建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的實體類如下:
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;    }}其實RecyclerView的優點就是將ListView的items布局分離出來,好讓我們實現如下三種效果。
GridLayoutManager(網格布局)
下面的代碼我們將Activity界面初始化分離出來:
public void initView() {    initActionBar();    String[] strTitle = {"慵懶慢時光,爵士中文", "【華語】曾經的你已杳無音信", "粵語歌中的華語鏡像", "凡走過必留下痕跡", "搖滾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);//網格布局,每行為3    recyclerView.setLayoutManager(layout);    recyclerView.setHasFixedSize(true);//適配器內容改變,不會改變RecyclerView的大小    adapter = new MyAdapter(musicItems);    recyclerView.setAdapter(adapter);}對于設置ListView的適配器代碼不需要過多解釋,其他的代碼都有注釋。運行后界面如下(是不是有網易云音樂首首頁選項列表的感覺):

LinearLayoutManager(垂直布局、水平布局)
只需要更改上面代碼中網格布局的那行代碼,如下:
RecyclerView.LayoutManager layout=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
參數1:上下文
參數2:水平還是垂直
參數3:為false表示數據按輸入的順序顯示,為true表示數據逆向顯示。
運行程序當得到如下界面:

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

這里不好看的原因是沒有結合CardView一起使用,下面將介紹如何整合RecyclerView與CardView。
3.當RecyclerView遇上CardView
CardView繼承自Framelayout,所以FrameLayout所有屬性CardView均可以直接拿來用,不過CardView還有自己獨有的屬性。下面我們將item布局的父標簽換成CardView。并且用RelativeLayout包裹兩個子控件。代碼如下:
<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>運行效果圖如下:

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

轉載請注明:Android開發中文站 ? RecyclerView與CardView的使用
新聞熱點
疑難解答