RecyclerView + CardView实现横向卡片式滑动效果

  

现在来介绍两种控件RecyclerView和CardView,并通过实例将它们结合在一起实现一种横向卡片式滑动效果。

  

<强> 1. recyclerview

  

RecyvlerView是android SDK新增加的一种控件,也被官方推荐代替ListView来使用,因为其具有更好的灵活性和代替性。

  

<强> 2. cardview

  

CardView是安卓5.0推出的一种卡片式控件,内部封装了许多有用的方法来实现美观效果。

  

<强> 3。如何使用RecylerView和CardView在android工作室中

  

在build.gradle中添加依赖再编辑即可

        编译“com.android.support: recyclerview-v7:25 +”。   编译的com.android.support: cardview-v7:25      

<>强4。通过实例,使用两种控件实现横向卡片式滑动效果

  

建立main。xml布局文件,代码如下:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android:取向=按怪薄?   android: layout_width=" match_parent "   android: layout_height=" match_parent "   xmlns:应用=" http://schemas.android.com/apk/res-auto "比;      & lt; android.support.v7.widget.RecyclerView   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android: id=癅 + id/recycler_View”   比;      & lt;/android.support.v7.widget.RecyclerView>      & lt;/LinearLayout>      

使用过ListView的同学应该知道还需要一个子布局来填充RecyclerView
  以下为recyclerView_item.xml的代码:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   xmlns:应用=" http://schemas.android.com/apk/res-auto "   android:取向=按怪薄?   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android: id=癅 + id/recyclerview_item”   android:填充=" 30 dp "      比;      & lt; android.support.v7.widget.CardView   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:取向=按怪薄?   应用:contentPadding=" 50 dp”   应用:cardCornerRadius=" 20 dp”   android:点击=" true "      android:前景=" & # 63;android: attr/selectableItemBackground”   应用:cardElevation=" @dimen/cardview_compat_inset_shadow”   应用:cardBackgroundColor=癅color/cardview_light_background”比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=皐rap_content”比;      & lt; TextView   android: id=癅 + id/tv1”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_gravity="中心"   android:文本="作者”   android: textSize=?2 dp/比;   & lt;/LinearLayout>      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" 114 dp”   比;   & lt; TextView   android: id=癅 + id/tv2”   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android: layout_gravity="中心"   android:文本="   锄禾日当午,汗滴禾下土”      android: textSize=?2 dp/比;   & lt;/LinearLayout>   & lt;/android.support.v7.widget.CardView>   & lt;/LinearLayout>      

从代码中,我们会发现使用了CardView控件以及在控件中添加简易的两个TextView

  

现在来介绍CardView的一些常用属性,这也是现在卡片效果的关键所在

  

card_view: contentPadding这个可以给你的内容加补上属性
  card_view: cardBackgroundColor这个可以改变cardview的背景
  card_view: cardCornerRadius这个可以改变cardview圆角的大小
  card_view: cardElevation这个比较难解释,CardView的Z轴阴影,被用来决定阴影的大小以及柔和度,以至于可以逼真的模拟出对于深度效果的描述。说白点可以理解为阴影的大小
  andorid:前景=" & # 63;android: attr/selectableItemBackground”这个可以使CardView被点击后出现波纹效

  

通过以上常用属性可以使CardView出现各种不同的效果

  

现在回到活动中来实现RecyclerView

  

跟ListView的一样,我们需要写一个适配器,代码如下:

        公共类recyclerViewadapter RecyclerView延伸。适配器{   私人List

RecyclerView + CardView实现横向卡片式滑动效果