Android开发学习之RecyclerView
程序员文章站
2022-06-22 11:48:20
1、在app/build.gradle中添加RecyclerView依赖 注意依赖版本号 2、布局文件activity_main.xml引入RecycleView 3、泛型类型Fruit.java 4、布局fruit_item.xml 5、适配器FruitAdapter.java 6、配置并使用Re ......
1、在app/build.gradle中添加recyclerview依赖
1 implementation 'com.android.support:recyclerview-v7:28.0.0'
注意依赖版本号
2、布局文件activity_main.xml引入recycleview
1 <android.support.v7.widget.recyclerview 2 android:id="@+id/recycler_view" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"></android.support.v7.widget.recyclerview>
3、泛型类型fruit.java
1 package com.example.lalala.recyclerviewdemo; 2 3 public class fruit { 4 private string name; 5 private int imageid; 6 public fruit(string name,int imageid){ 7 this.name = name; 8 this.imageid = imageid; 9 } 10 11 public string getname() { 12 return name; 13 } 14 15 public int getimageid() { 16 return imageid; 17 } 18 }
4、布局fruit_item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="horizontal" android:layout_width="match_parent" 4 android:layout_height="wrap_content"> 5 <imageview 6 android:id="@+id/fruit_image" 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" /> 9 <textview 10 android:id="@+id/fruit_name" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_gravity="center_vertical" 14 android:layout_marginleft="10dp" 15 /> 16 </linearlayout>
5、适配器fruitadapter.java
1 package com.example.lalala.recyclerviewdemo; 2 3 import android.support.annotation.nonnull; 4 import android.support.v7.widget.recyclerview; 5 import android.view.layoutinflater; 6 import android.view.view; 7 import android.view.viewgroup; 8 import android.widget.imageview; 9 import android.widget.textview; 10 11 import java.util.list; 12 13 public class fruitadapter extends recyclerview.adapter<fruitadapter.viewholder> { 14 //适配器保存的数据 15 private list<fruit> mfruitlist; 16 //view容器类,viewholder容纳view 17 static class viewholder extends recyclerview.viewholder{ 18 imageview imageview; 19 textview textview; 20 public viewholder(view view){ 21 super(view); 22 imageview = (imageview)view.findviewbyid(r.id.fruit_image); 23 textview = (textview)view.findviewbyid(r.id.fruit_name); 24 } 25 } 26 //构造函数初始化适配器 27 public fruitadapter(list<fruit> fruitlist){ 28 mfruitlist = fruitlist; 29 } 30 //创建viewholder 31 @nonnull 32 @override 33 public viewholder oncreateviewholder(@nonnull viewgroup viewgroup, int i) { 34 view view = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.fruit_item,viewgroup,false); 35 viewholder viewholder = new viewholder(view); 36 return viewholder; 37 } 38 //绑定数据到viewholder 39 @override 40 public void onbindviewholder(@nonnull viewholder viewholder, int i) { 41 fruit fruit = mfruitlist.get(i); 42 viewholder.imageview.setimageresource(fruit.getimageid()); 43 viewholder.textview.settext(fruit.getname()); 44 } 45 //返回子项个数 46 @override 47 public int getitemcount() { 48 return mfruitlist.size(); 49 } 50 }
6、配置并使用recyclerview
1 package com.example.lalala.recyclerviewdemo; 2 3 import android.support.v7.app.appcompatactivity; 4 import android.os.bundle; 5 import android.support.v7.widget.linearlayoutmanager; 6 import android.support.v7.widget.recyclerview; 7 8 import java.util.arraylist; 9 import java.util.list; 10 11 public class mainactivity extends appcompatactivity { 12 13 private list<fruit> fruitlist = new arraylist<>(); 14 15 @override 16 protected void oncreate(bundle savedinstancestate) { 17 super.oncreate(savedinstancestate); 18 setcontentview(r.layout.activity_main); 19 initfruit(); 20 //使用recyclerview 21 recyclerview recyclerview = (recyclerview)findviewbyid(r.id.recycler_view); 22 //布局管理器 23 linearlayoutmanager layoutmanager = new linearlayoutmanager(this); 24 recyclerview.setlayoutmanager(layoutmanager); 25 fruitadapter adapter = new fruitadapter(fruitlist); 26 recyclerview.setadapter(adapter); 27 } 28 private void initfruit(){ 29 for(int i=0;i<8;i++){ 30 fruit apple = new fruit("apple",r.mipmap.ic_launcher); 31 fruitlist.add(apple); 32 fruit pear = new fruit("pear",r.mipmap.ic_launcher); 33 fruitlist.add(pear); 34 fruit banana = new fruit("banana",r.mipmap.ic_launcher); 35 fruitlist.add(banana); 36 } 37 } 38 }
7、大功告成,运行截图
上一篇: solidworks低版本怎么打开查看高版本创建的模型?
下一篇: Python学习 :格式化输出
推荐阅读
-
Android开发之菜单(menu)用法实例分析
-
Android NDK开发之:配置环境的详解
-
Android开发笔记之: 数据存储方式详解
-
Android开发笔记之:在ImageView上绘制圆环的实现方法
-
Android开发笔记之:Handler Runnable与Thread的区别详解
-
Android开发笔记之:返回键的复写onBackPressed()介绍
-
Android开发笔记之:Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)
-
Android开发笔记之:一分钟学会使用Logcat调试程序的详解
-
Android开发笔记之:如何安全中止一个自定义线程Thread的方法
-
Android学习笔记之AndroidManifest.xml文件解析(详解)