Android开发学习之RecyclerView
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、大功告成,运行截图