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

Android RecyclerView的简单使用

程序员文章站 2023-12-09 18:24:57
本文实例为大家分享了android recyclerview使用的具体代码,供大家参考,具体内容如下 package com.itheima74.recycler...

本文实例为大家分享了android recyclerview使用的具体代码,供大家参考,具体内容如下

package com.itheima74.recyclerview; 
 
import android.os.bundle; 
import android.support.v7.app.appcompatactivity; 
import android.support.v7.widget.gridlayoutmanager; 
import android.support.v7.widget.recyclerview; 
import android.view.view; 
import android.view.viewgroup; 
import android.widget.imageview; 
import android.widget.textview; 
 
import java.util.arraylist; 
 
/** 
 * recyclerview 
 * 1.增强版的listview 
 * 2.官方推荐 
 * 3.使用步骤: 
 * a.build.gradle文件中添加依赖compile 'com.android.support:recyclerview-v7:25.2.0' 
 * b.xml布局中引入,注意完整包名+类名 
 * c.具体见代码 
 */ 
public class mainactivity extends appcompatactivity { 
 
 arraylist<fruit> mfruitlist; 
 private recyclerview recyclerview; 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_main); 
 
 initview(); 
 initdata(); 
 initadapter(); 
 } 
 
 /** 
 * 初始化数据适配器 
 */ 
 private void initadapter() { 
 recyclerview.setadapter(new fruitadapter()); 
 } 
 
 /** 
 * 初始化控件 
 */ 
 private void initview() { 
 recyclerview = (recyclerview) findviewbyid(r.id.recyclerview); 
 
 // 设置为linearlayoutmanager布局方式,线性布局 
 // linearlayoutmanager layoutmanager = new linearlayoutmanager(this); 
 // 两种方式,水平或垂直排布,默认垂直排布 
 // layoutmanager.setorientation(linearlayoutmanager.horizontal); 
 // recyclerview.setlayoutmanager(layoutmanager); 
 
 // 设置为gridlayoutmanager布局方式,3列排布 
 gridlayoutmanager gridlayoutmanager = new gridlayoutmanager(this,3); 
 recyclerview.setlayoutmanager(gridlayoutmanager); 
 } 
 
 /** 
 * 初始化数据,水果集合 
 */ 
 private void initdata() { 
 mfruitlist = new arraylist<>(); 
 mfruitlist.add(new fruit("apple", r.drawable.apple_pic)); 
 mfruitlist.add(new fruit("banana", r.drawable.banana_pic)); 
 mfruitlist.add(new fruit("cherry", r.drawable.cherry_pic)); 
 mfruitlist.add(new fruit("grape", r.drawable.grape_pic)); 
 mfruitlist.add(new fruit("mango", r.drawable.mango_pic)); 
 mfruitlist.add(new fruit("orange", r.drawable.orange_pic)); 
 mfruitlist.add(new fruit("pear", r.drawable.pear_pic)); 
 mfruitlist.add(new fruit("pineapple", r.drawable.pineapple_pic)); 
 mfruitlist.add(new fruit("strawberry", r.drawable.strawberry_pic)); 
 mfruitlist.add(new fruit("watermelon", r.drawable.watermelon_pic)); 
 } 
 
 class fruitadapter extends recyclerview.adapter<viewholder> { 
 
 // 返回viewholder对象 
 @override 
 public viewholder oncreateviewholder(viewgroup parent, int viewtype) { 
 return new viewholder(view.inflate(getapplicationcontext(), r.layout.listview_item, null)); 
 } 
 
 // 刷新界面,为viewholder两个属性设置内容 
 @override 
 public void onbindviewholder(viewholder holder, int position) { 
 fruit fruit = mfruitlist.get(position); 
 holder.iv.setimageresource(fruit.imageid); 
 holder.tv.settext(fruit.name); 
 } 
 
 @override 
 public int getitemcount() { 
 // fruitadapter是mainactivity的内部类,可以直接使用外部类的成员变量 
 return mfruitlist.size(); 
 } 
 } 
 
 static class viewholder extends recyclerview.viewholder { 
 imageview iv; 
 textview tv; 
 
 viewholder(view itemview) { 
 super(itemview); 
 iv = (imageview) itemview.findviewbyid(r.id.iv); 
 tv = (textview) itemview.findviewbyid(r.id.tv); 
 } 
 } 
 
 
} 

package com.itheima74.recyclerview; 
 
/** 
 * created by my on 2017/3/2. 
 */ 
 
class fruit { 
 string name; 
 int imageid; 
 
 fruit(string name, int imageid) { 
 this.name = name; 
 this.imageid = imageid; 
 } 
} 

复制代码 代码如下:
<img src="http://img.blog.csdn.net/20170303085928987?watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvemh1njiwmtk3ng==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center" alt=""> 

效果图:

Android RecyclerView的简单使用

Android RecyclerView的简单使用

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