为方便自己以后学习,自己记录学习,大家也可以参考,有什么问题一起探讨。
今天学习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中的代码:
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:id="@+id/id_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
因为RecyclerView不是内置在系统SDK中的,所以需要把完整的包路径写出来。
3、新建实体类,这里以Book类作为演示:
package com.example.dell.practice_recyclerview;
/**
* Created by dell on 2018/6/3.
* Created by qiyueqing on 2018/

本文介绍了如何在Android中使用RecyclerView实现数据的垂直滚动和横向滚动。首先在build.gradle文件中添加RecyclerView依赖,然后在XML布局文件中设置RecyclerView,接着创建Book实体类和自定义布局book_item.xml。通过创建BookAdapter并重写相关方法完成数据绑定,最后在MainActivity中初始化数据并设置LayoutManager为LinearLayoutManager,通过设置orientation属性实现滚动方向切换。
最低0.47元/天 解锁文章
1910

被折叠的 条评论
为什么被折叠?



