一、用法:
RecyclerView和其他用于界面数据滑动展示的控件(GridView,ListView,Spinner等)一样,都少不了数据源,适配器,以及监听逻辑处理这三块。
下面就来讲解写RecyclerView的使用方法:
1.引用(导包)
2.布局文件引用
3.构造适配器
4.主程序,包括数据源以及逻辑处理等
上两个简单的效果图:
StaggeredGridLayoutManager 以瀑布流方式展示Item
LinerLayoutManager 以垂直或者水平列表方式展示Item
二、贴代码时间:
Step1:引用(导包)
2.1 直接在build.gradle(Module:app)的dependencies添加
implementation ‘androidx.recyclerview:recyclerview:1.2.0-alpha03‘
Step2:布局
2.2 activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
2.3 crush_item.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:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:orientation="horizontal"> <ImageView android:id="@+id/crush_image" android:layout_width="100dp" android:layout_height="150dp" android:layout_gravity="left" tools:background="@drawable/image0" /> <TextView android:id="@+id/crush_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:layout_marginLeft="40dp" tools:text="CrushText" /> </LinearLayout>
Step3:构造适配器
封装 2.4 Crush.java
package com.gatsby.recyclertest; public class Crush { private String name; private int imageId; public Crush(String name, int imageId) { this.name = name; this.imageId = imageId; } public String getName(){ return name; } public int getImageId(){ return imageId; } }
2.5 CrushAdapter.java
继承RecyclerView.Adapter必须要重写的三个方法。 onCreateViewHolder(), onBindViewHolder, getItemCoun
方法 | 作用 |
onCreateViewHolder() | 用于创建ViewHolder实例。这个方法将子项布局crush_item 加载进来并传入ViewHolder的构造函数,最后返回ViewHolder对象 |
onBindViewHolder() | 此方法用于对子项的数据进行赋值,在每个子项滚动到屏幕内的时候执行 |
getItemCount() | 用于返回RecyclerView的长度 |
package com.gatsby.recyclertest; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class CrushAdapter extends RecyclerView.Adapter<CrushAdapter.ViewHolder> { private List<Crush> mCrushList; static class ViewHolder extends RecyclerView.ViewHolder { View crushView; ImageView crushImage; TextView crushName; public ViewHolder(@NonNull View itemView) { super(itemView); crushView = itemView; crushImage = (ImageView) itemView.findViewById(R.id.crush_image); crushName = (TextView) itemView.findViewById(R.id.crush_name); } } public CrushAdapter(List<Crush> crushList) { mCrushList = crushList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.crush_item, parent, false); final ViewHolder holder = new ViewHolder(view); holder.crushView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); Crush crush = mCrushList.get(position); Toast.makeText(v.getContext(), "you clicked view " + crush.getName(), Toast.LENGTH_SHORT).show(); } }); holder.crushImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAbsoluteAdapterPosition(); Crush crush = mCrushList.get(position); Toast.makeText(v.getContext(), "you clicked image " + crush.getName(), Toast.LENGTH_SHORT).show(); } }); return holder; } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Crush crush = mCrushList.get(position); holder.crushImage.setImageResource(crush.getImageId()); holder.crushName.setText(crush.getName()); } @Override public int getItemCount() { return mCrushList.size(); } }