RecyclerView+CardView实现横向卡片式滑动效果实现横向卡片式滑动效果
主要为大家详细介绍了RecyclerView+CardView实现横向卡片式滑动效果,具有一定的参考价值,感兴趣的小
伙伴们可以参考一下
现在来介绍两种控件RecyclerView和CardView,并通过实例将它们结合在一起实现一种横向卡片式滑动效果.
1.RecyclerView
RecyvlerView是android SDK 新增加的一种控件,也被官方推荐代替ListView来使用,因为其具有更好的灵活性和代替性。
2.CardView
CardView是安卓5.0推出的一种卡片式控件,内部封装了许多有用的方法来实现美观效果。
3.如何使用如何使用RecylerView和和CardView在在android studio中中
在build.gradle中添加依赖再编辑即可
compile 'com.android.support:recyclerview-v7:25.+'
compile 'com.android.support:cardview-v7:25
4.通过实例,使用两种控件实现横向卡片式滑动效果通过实例,使用两种控件实现横向卡片式滑动效果
建立main.xml布局文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_View"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
使用过ListView的同学应该知道还需要一个子布局来填充RecyclerView
以下为recyclerView_item.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerview_item"
android:padding="30dp"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:contentPadding="50dp"
app:cardCornerRadius="20dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardElevation="@dimen/cardview_compat_inset_shadow"
app:cardBackgroundColor="@color/cardview_light_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"