RecyclerView实现纵向和横向滚动实现纵向和横向滚动
为方便自己以后学习,自己记录学习,大家也可以参考,有什么问题一起探讨。
今天学习RecyclerView,下边来说一下实现数据垂直滚动和数据横向滚动。先上图为敬:
所用工具:Android Studio
纵向滚动纵向滚动
1、添加依赖库:
打开app/build.gradle文件,在dependencies闭包中添加如下内容(compile ‘com.android.support:recyclerview-v7:24.2.1’为添
加的内容)
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support:recyclerview-v7:24.2.1'
testCompile 'junit:junit:4.12'
}
添加完之后点击一下Sync Now来进行同步;
2、修改activity_main.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dell.practice_recyclerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
因为RecyclerView不是内置在系统SDK中的,所以需要把完整的包路径写出来。
3、新建实体类,这里以Book类作为演示: