赞
踩
RecyclerView能够灵活实现大数据集的展示,视图和复用管理比ListView更好,能够显示列表、网格、瀑布流等形式,且不同的ViewHolder能够实现item多元化功能。
但是使用起来会稍微麻烦一点,并且没有类似ListView的onItemClickListener等监听事件,需要开发者自己实现
导包:旧版本的是通过 android.support 导入的,这里介绍新版本如何通过androidx导包
关于AndroidX与Android Support Library库的区别:
1.Android Support Library中提供的库,它们的包名都是以android.support.* 开头的。
而AndroidX库中所有API的包名都变成了在androidx.*下面
2.AndroidX库中命名不再含有操作系统API版本号
AndroidX本质上其实就是对Android Support Library进行的一次升级,升级内容主要在于以下两个方面。
第一,包名。之前Android Support Library中的API,它们的包名都是在android.support.*下面的,
而AndroidX库中所有API的包名都变成了在androidx.*下面。这是一个很大的变化,意味着以后凡是android.*包下面的API
都是随着Android操作系统发布的,而androidx.*包下面的API都是随着扩展库发布的,这些API基本不会依赖于操作系统的具体版本。
第二,命名规则。吸取了之前命名规则的弊端,AndroidX所有库的命名规则里都不会再包含具体操作系统API的版本号了。
比如,像appcompat-v7库,在AndroidX中就变成了appcompat库。
项目中不要把Android Support库和AndroidX混合使用,这可能会带来意想不到的错误。
我们应该尽量采用AndroidX进行开发,官方正在逐渐放弃Android Support Library的维护。
来自guolin的博客(https://blog.csdn.net/guolin_blog/article/details/97142065)
先说一下,我用的这个版本的
这里补充一下,在发完博客几个小时后,呜呜呜~,我手贱把Android Studio更新到3.6了,但是方法还是正确的。
build.gradle文件
apply plugin: 'com.android.application' android { compileSdkVersion 29 buildToolsVersion "29.0.3" defaultConfig { applicationId "com.ylw.helloworld" minSdkVersion 21 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } repositories { mavenCentral() google() } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' implementation 'com.github.bumptech.glide:glide:4.11.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0' implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01' //这个就是导入recyclerview包 }
导包代码
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01'
别的导包方法:
1.点击左上角File->Project Structer…
2.如图所示
3.如图所示后点击ok
4.上一个界面点击ok后,点击这个界面的Apply然后点击ok
5.然后就可以看到自己的build.gradle文件里多了一行导包代码,每个的版本不一样可能会有区别。
RecyclerView用法
列表视图,竖直滚动
activity_linear_recycler_view.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F44336" /> 这里的背景颜色后面分隔线会用到
</RelativeLayout>
效果
layout_linear_item.xml,RecyclerView的具体布局文件
<?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" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:background="#EAD308" 这里颜色和上面的颜色不一样,才能看出后面的分隔线效果 android:textColor="#000" android:textSize="20sp" /> </LinearLayout>
效果
LinearRecyclerViewActivity.java文件
package com.ylw.helloworld.recyclerview; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.graphics.Rect; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.ylw.helloworld.R; public class LinearRecyclerViewActivity extends AppCompatActivity { private RecyclerView mRvMain; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_linear_recycler_view); mRvMain = findViewById(R.id.rv_main); mRvMain.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this)); //设置一个线性布局管理器,有好多种布局管理器 mRvMain.addItemDecoration(new MyDecoration()); //这个方法可以实现分割线,但不是专门用来实现分割线的。 //设置适配器,自定义的,详见LinearAdapter.java文件 //这里传入连个参数,一个是context,一个是监听器(这个监听器也是在adapter里自己写的监听器) mRvMain.setAdapter(new LinearAdapter(LinearRecyclerViewActivity.this, new LinearAdapter.OnItemClickListener() { @Override //这里通过实现自己在LinearAdapter里写的方法监听点击事件 public void onClick(int pos) { Toast.makeText(LinearRecyclerViewActivity.this,"click "+pos,Toast.LENGTH_SHORT).show(); } })); } class MyDecoration extends RecyclerView.ItemDecoration{ //设置间隔 @Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); outRect.set(0,0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight)); //dividerHeight是dimen.xml文件里设置的间隔线高度 //在元素的下方添加了线,实现分隔线,这里的原理是把每个布局中间留一个间隙,把背景露出来,分隔线的颜色其实是背景的颜色 //可以在原来的布局中设置marginTop,更简单,也能达到同样的效果 } } }
value下的dimen.xml文件,设置了间隔高度
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dividerHeight">1dp</dimen>
</resources>
LinearAdapter.java文件
package com.ylw.helloworld.recyclerview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.ylw.helloworld.R; public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> { private Context mContext; private OnItemClickListener mListener; public LinearAdapter(Context context, OnItemClickListener listener){ this.mContext = context; this.mListener = listener; } @NonNull @Override //创建ViewHolder public LinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item,parent,false)); //LayoutInflater布局填充器 } @Override //绑定ViewHolder,设置内容 public void onBindViewHolder(@NonNull LinearAdapter.LinearViewHolder holder, final int position) { holder.textView.setText("Hello World!"); holder.itemView.setOnClickListener(new View.OnClickListener() { //监听点击事件 @Override public void onClick(View v) { mListener.onClick(position); } }); } @Override //获取列表数目 public int getItemCount() { return 30; } class LinearViewHolder extends RecyclerView.ViewHolder{ private TextView textView; public LinearViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.tv_title); } } //创建点击事件监听接口 public interface OnItemClickListener{ void onClick(int pos); } }
最终效果,点击第一个,可以看出序号也是从0 开始
activity_hor_recycler_view.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="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_hor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F80B0B"/>
</LinearLayout>
效果
layout_hor_item.xml,RecyclerView的具体布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" android:background="#03A9F4"> <TextView android:id="@+id/tv_title" android:layout_width="60dp" android:layout_height="50dp" android:gravity="center" android:background="#EAD308" android:textColor="#000" android:textSize="20sp" /> </LinearLayout>
效果
HorRecyclerViewActivity.java文件
package com.ylw.helloworld.recyclerview; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.graphics.Rect; import android.os.Bundle; import android.view.View; import android.widget.LinearLayout; import android.widget.Toast; import com.ylw.helloworld.R; public class HorRecyclerViewActivity extends AppCompatActivity { private RecyclerView mRvHor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hor_recycler_view); mRvHor = findViewById(R.id.rv_hor); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(HorRecyclerViewActivity.this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRvHor.setLayoutManager(linearLayoutManager); mRvHor.addItemDecoration(new MyDecoration()); mRvHor.setAdapter(new HorAdapter(HorRecyclerViewActivity.this, new HorAdapter.OnItemClickListener() { @Override public void onClick(int pos) { Toast.makeText(HorRecyclerViewActivity.this,"click: "+pos,Toast.LENGTH_SHORT).show(); } })); } class MyDecoration extends RecyclerView.ItemDecoration{ //设置间隔 @Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); outRect.set(0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight),0); //在元素的下方添加了线,实现分隔线,这里的原理是把每个布局中间留一个间隙,把背景露出来,分隔线的颜色其实是背景的颜色 //可以在原来的布局中设置marginTop,更简单,也能达到同样的效果 //左上右下 } } }
HorAdapter.java文件
package com.ylw.helloworld.recyclerview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.ylw.helloworld.R; public class HorAdapter extends RecyclerView.Adapter<HorAdapter.LinearViewHolder> { private Context mContext; private OnItemClickListener mListener; public HorAdapter(Context context, OnItemClickListener listener){ this.mContext = context; this.mListener = listener; } @NonNull @Override //创建ViewHolder public HorAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_hor_item,parent,false)); //LayoutInflater布局填充器 } @Override //绑定ViewHolder,设置内容 public void onBindViewHolder(@NonNull HorAdapter.LinearViewHolder holder, final int position) { holder.textView.setText("Hello"); holder.itemView.setOnClickListener(new View.OnClickListener() { //监听点击事件 @Override public void onClick(View v) { mListener.onClick(position); } }); } @Override //获取列表数目 public int getItemCount() { return 30; } class LinearViewHolder extends RecyclerView.ViewHolder{ private TextView textView; public LinearViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.tv_title); } } //创建点击事件监听接口 public interface OnItemClickListener{ void onClick(int pos); } }
效果,可以横向滚动,点击第一个,可以看出编号从0开始
activity_grid_recycler_view.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="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EC0909"/>
</LinearLayout>
效果
layout_grid_item.xml,RecyclerView的具体布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" android:background="#03A9F4"> <TextView android:id="@+id/tv_title" android:layout_width="60dp" android:layout_height="50dp" android:gravity="center" android:background="#EAD308" android:textColor="#000" android:textSize="20sp" /> </LinearLayout>
效果
GridRecyclerViewActivity.java文件
package com.ylw.helloworld.recyclerview; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import android.widget.Toast; import com.ylw.helloworld.R; public class GridRecyclerViewActivity extends AppCompatActivity { private RecyclerView mRvGrid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_grid_recycler_view); mRvGrid = findViewById(R.id.rv_grid); //设置网格布局(上下文,每行展示的个数) mRvGrid.setLayoutManager(new GridLayoutManager(GridRecyclerViewActivity.this,3)); mRvGrid.setAdapter(new GridAdapter(GridRecyclerViewActivity.this, new GridAdapter.OnItemClickListener() { @Override public void onClick(int pos) { Toast.makeText(GridRecyclerViewActivity.this,"click:"+pos,Toast.LENGTH_SHORT).show(); } })); } }
GridAdapter.java文件
package com.ylw.helloworld.recyclerview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.ylw.helloworld.R; public class GridAdapter extends RecyclerView.Adapter<GridAdapter.LinearViewHolder> { private Context mContext; private OnItemClickListener mListener; public GridAdapter(Context context, OnItemClickListener listener){ this.mContext = context; this.mListener = listener; } @NonNull @Override //创建ViewHolder public GridAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_grid_recyclerview_item,parent,false)); //LayoutInflater布局填充器 } @Override //绑定ViewHolder,设置内容 public void onBindViewHolder(@NonNull GridAdapter.LinearViewHolder holder, final int position) { holder.textView.setText("Hello"); holder.itemView.setOnClickListener(new View.OnClickListener() { //监听点击事件 @Override public void onClick(View v) { mListener.onClick(position); } }); } @Override //获取列表数目 public int getItemCount() { return 80; } class LinearViewHolder extends RecyclerView.ViewHolder{ private TextView textView; public LinearViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.tv_title); } } //创建点击事件监听接口 public interface OnItemClickListener{ void onClick(int pos); } }
效果,可以竖直滚动,点击第一个,可以看出编号从0开始
activity_pu_recycler_view.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="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_pu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F44336" />
</LinearLayout>
效果
layout_staggered_grid_recyclerview_item.xml,RecyclerView的具体布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#03A9F4">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
</LinearLayout>
效果。没有内容,看不出来效果,这里就不截图了。
PuRecyclerViewActivity.java文件
package com.ylw.helloworld.recyclerview; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import android.graphics.Rect; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.ylw.helloworld.R; public class PuRecyclerViewActivity extends AppCompatActivity { private RecyclerView mRvPu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pu_recycler_view); mRvPu = findViewById(R.id.rv_pu); //不规则的网格布局(列数或行数,竖直排列或水平排列) mRvPu.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)); mRvPu.addItemDecoration(new MyDecoration()); mRvPu.setAdapter(new StaggeredGridAdapter(PuRecyclerViewActivity.this, new StaggeredGridAdapter.OnItemClickListener() { @Override public void onClick(int pos) { Toast.makeText(PuRecyclerViewActivity.this,"click:"+pos,Toast.LENGTH_SHORT).show(); } })); } class MyDecoration extends RecyclerView.ItemDecoration{ @Override public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); int gap = getResources().getDimensionPixelOffset(R.dimen.dividerHeight2); outRect.set(gap,gap,gap,gap); } } }
StaggeredGridAdapter.java文件
package com.ylw.helloworld.recyclerview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.ylw.helloworld.R; public class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.LinearViewHolder> { private Context mContext; private OnItemClickListener mListener; public StaggeredGridAdapter(Context context, OnItemClickListener listener){ this.mContext = context; this.mListener = listener; } @NonNull @Override //创建ViewHolder public StaggeredGridAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_staggered_grid_recyclerview_item,parent,false)); //LayoutInflater布局填充器 } @Override //绑定ViewHolder,设置内容 public void onBindViewHolder(@NonNull StaggeredGridAdapter.LinearViewHolder holder, final int position) { if (position%2 != 0){ holder.imageView.setImageResource(R.drawable.image1); }else{ holder.imageView.setImageResource(R.drawable.image2); } holder.itemView.setOnClickListener(new View.OnClickListener() { //监听点击事件 @Override public void onClick(View v) { mListener.onClick(position); } }); } @Override //获取列表数目 public int getItemCount() { return 30; } class LinearViewHolder extends RecyclerView.ViewHolder{ private ImageView imageView; public LinearViewHolder(@NonNull View itemView) { super(itemView); imageView = itemView.findViewById(R.id.iv); } } //创建点击事件监听接口 public interface OnItemClickListener{ void onClick(int pos); } }
效果,可以竖直滚动,点击第一个,可以看出编号从0开始。
增加layout_linear_item2.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" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:background="#EAD308" android:textColor="#000" android:textSize="20sp" /> <ImageView android:id="@+id/iv_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> </LinearLayout>
效果
LinearAdapter.java
package com.ylw.helloworld.recyclerview; import android.content.Context; 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 com.ylw.helloworld.R; public class LinearAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context mContext; private OnItemClickListener mListener; public LinearAdapter(Context context, OnItemClickListener listener){ this.mContext = context; this.mListener = listener; } @NonNull @Override //创建ViewHolder public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { if (viewType == 0){ return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item,parent,false)); //LayoutInflater布局填充器 }else{ return new LinearViewHolder2(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item_2,parent,false)); //LayoutInflater布局填充器 } } @Override //绑定ViewHolder,设置内容 public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) { if (getItemViewType(position) == 0){ ((LinearViewHolder)holder).textView.setText("Hello World!"); }else{ ((LinearViewHolder2)holder).textView.setText("我的第一个APP"); } holder.itemView.setOnClickListener(new View.OnClickListener() { //监听点击事件 @Override public void onClick(View v) { mListener.onClick(position); } }); } @Override public int getItemViewType(int position) { if (position %2 == 0){ return 0; }else { return 1; } } @Override //获取列表数目 public int getItemCount() { return 30; } class LinearViewHolder extends RecyclerView.ViewHolder{ private TextView textView; public LinearViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.tv_title); } } class LinearViewHolder2 extends RecyclerView.ViewHolder{ private TextView textView; private ImageView imageView; public LinearViewHolder2(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.tv_title); imageView = itemView.findViewById(R.id.iv_image); } } //创建点击事件监听接口 public interface OnItemClickListener{ void onClick(int pos); } }
效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。