欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

RecyclerView实现纵向和横向滚动

程序员文章站 2022-03-08 17:17:05
为方便自己以后学习,自己记录学习,大家也可以参考,有什么问题一起探讨。 今天学习recyclerview,下边来说一下实现数据垂直滚动和数据横向滚动。先上图为敬: &n...

为方便自己以后学习,自己记录学习,大家也可以参考,有什么问题一起探讨。

今天学习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实现纵向和横向滚动

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。