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类作为演示:
package com.example.dell.practice_recyclerview; /** * created by dell on 2018/6/3. * created by qiyueqing on 2018/6/3. */ public class book { private string name; private int imageid; public book(string name, int imageid) { this.name = name; this.imageid = imageid; } public string getname() { return name; } public int getimageid() { return imageid; } }
4、为listview的子项制定一个我们自定义的布局:
在layout目录下新建book_item.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <imageview android:id="@+id/id_book_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/id_book_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginleft="10dp"/> </linearlayout>
5、为recyclerview准备一个适配器:
新建bookadapter类,让这个类继承recyclerview.adapter,并将泛型指定为bookadapter.viewholder;
里边自定义一个内部类viewholder,里边的构造参数传入view参数,这个参数就是recyclerview的最外层布局,这样就可以通过findviewbyid()来货渠道布局中的imageview和textview的实例了;
bookadapter中的构造函数,这个方法吧要展示的数据源传进来,并赋值给一个全局变量mbookadapter,我们后继的所有操作都将在这个数据源的基础上进行;重写三个方法;
package com.example.dell.practice_recyclerview; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; import java.util.list; /** * created by dell on 2018/6/3. * created by qiyueqing on 2018/6/3. */ public class bookadapter extends recyclerview.adapter<bookadapter.viewholder>{ private list<book> mbooklist; static class viewholder extends recyclerview.viewholder{ imageview bookimage; textview bookname; public viewholder(view view){ super(view); bookimage=view.findviewbyid(r.id.id_book_image); bookname=view.findviewbyid(r.id.id_book_name); } } public bookadapter(list<book> booklist){ mbooklist=booklist; } @override public bookadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.book_item,parent,false); recyclerview.viewholder holder=new viewholder(view); return (viewholder) holder; } @override public void onbindviewholder(bookadapter.viewholder holder, int position) { book book=mbooklist.get(position); holder.bookimage.setimageresource(book.getimageid()); holder.bookname.settext(book.getname()); } @override public int getitemcount() { return mbooklist.size(); } }
6、修改mainactivity中的代码:
package com.example.dell.practice_recyclerview; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { private list<book> booklist=new arraylist<>(); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initbooks(); recyclerview recyclerview= (recyclerview) findviewbyid(r.id.id_recycler_view); linearlayoutmanager layoutmanager=new linearlayoutmanager(this); //linearlayoutmanager中定制了可扩展的布局排列接口,子类按照接口中的规范来实现就可以定制出不同排雷方式的布局了 //配置布局,默认为vertical(垂直布局),下边这句将布局改为水平布局 //layoutmanager.setorientation(linearlayoutmanager.horizontal); recyclerview.setlayoutmanager(layoutmanager); bookadapter adapter=new bookadapter(booklist); recyclerview.setadapter(adapter); } private void initbooks(){ for (int i=1;i<11;i++){ book book=new book("春起之苗"+i,r.drawable.icon_book); booklist.add(book); } } }
此时运行即可看到纵向的展示样例了。
实现横向滚动:
修改book_item中的代码:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="100dp" android:layout_height="wrap_content"> <imageview android:id="@+id/id_book_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> <textview android:id="@+id/id_book_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginleft="10dp"/> </linearlayout>
2、修改mainactivity中的代码
package com.example.dell.practice_recyclerview; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { private list<book> booklist=new arraylist<>(); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initbooks(); recyclerview recyclerview= (recyclerview) findviewbyid(r.id.id_recycler_view); linearlayoutmanager layoutmanager=new linearlayoutmanager(this); //linearlayoutmanager中定制了可扩展的布局排列接口,子类按照接口中的规范来实现就可以定制出不同排雷方式的布局了 //配置布局,默认为vertical(垂直布局),下边这句将布局改为水平布局 layoutmanager.setorientation(linearlayoutmanager.horizontal); recyclerview.setlayoutmanager(layoutmanager); bookadapter adapter=new bookadapter(booklist); recyclerview.setadapter(adapter); } private void initbooks(){ for (int i=1;i<11;i++){ book book=new book("春起之苗"+i,r.drawable.icon_book); booklist.add(book); } } }
例子下载地址:recyclerview实现纵向和横向滚动
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
为Word文档巧设不同页面方向实现既有纵向又有横向
-
Android中TextView实现垂直滚动和上下滚动效果
-
iOS使用UICollectionView实现横向滚动照片效果
-
小程序scroll-view安卓机隐藏横向滚动条的实现详解
-
JS实现利用闭包判断Dom元素和滚动条的方向示例
-
Android使用Recyclerview实现图片水平自动循环滚动效果
-
Word2007技巧:在同一文档中使用纵向和横向方向
-
Android控件gridview实现单行多列横向滚动效果
-
jquery简单实现纵向的无缝滚动代码实例
-
Android使用CardView作为RecyclerView的Item并实现拖拽和左滑删除