原理:
使用FilmReviewInfo的coverImgType字段进行条目类型判断,适配器FilmReviewAdapter的getItemViewType方法返回不同的条目供onCreateViewHolder进行呈现
MainActivity
package com.fkm.recyclerviewmultitype;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.fkm.recyclerviewmultitype.adapter.FilmReviewAdapter;
import com.fkm.recyclerviewmultitype.model.FilmReviewInfo;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private FilmReviewAdapter adapter;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.mRecyclerView);
getData();
}
/**
* 模拟请求数据
*/
private void getData() {
final List<FilmReviewInfo> mList = new ArrayList<>();
/** 模拟数据开始 */
FilmReviewInfo filmReviewInfo1 = new FilmReviewInfo();
filmReviewInfo1.setTitle("《拆弹专家2》呈现了一场多变而又扎实的跳脱与反观");
filmReviewInfo1.setCoverImg("https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2621379901.webp");
filmReviewInfo1.setCoverImgType(2); // 视图显示类型
filmReviewInfo1.setAuthor("徐若风");
filmReviewInfo1.setPublishedDate("2021-1-15");
mList.add(filmReviewInfo1);
FilmReviewInfo filmReviewInfo2 = new FilmReviewInfo();
filmReviewInfo2.setTitle("在我的人生里,有过一场重大的幻灭。");
filmReviewInfo2.setCoverImg("https://img3.doubanio.com/view/thing_review/l/public/5427631.jpg");
filmReviewInfo2.setCoverImgType(1); // 视图显示类型
filmReviewInfo2.setAuthor("林探惜");
filmReviewInfo2.setPublishedDate("2021-1-15");
mList.add(filmReviewInfo2);
FilmReviewInfo filmReviewInfo3 = new FilmReviewInfo();
filmReviewInfo3.setTitle("看似“套路”独立电影,胜在举重若轻,游牧者永远在路上");
filmReviewInfo3.setCoverImg("https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2628328069.webp");
filmReviewInfo3.setCoverImgType(2); // 视图显示类型
filmReviewInfo3.setAuthor("陀螺电影");
filmReviewInfo3.setPublishedDate("2021-1-15");
mList.add(filmReviewInfo3);
FilmReviewInfo filmReviewInfo4 = new FilmReviewInfo();
filmReviewInfo4.setTitle("解压神片,看完想去搓个澡");
filmReviewInfo4.setCoverImg("https://img2.doubanio.com/view/photo/s_ratio_poster/public/p2627788612.webp");
filmReviewInfo4.setCoverImgType(2); // 视图显示类型
filmReviewInfo4.setAuthor("吉撸万岁");
filmReviewInfo4.setPublishedDate("2021-1-15");
mList.add(filmReviewInfo4);
FilmReviewInfo filmReviewInfo5 = new FilmReviewInfo();
filmReviewInfo5.setTitle("《Soul》的剧情分析与讨论,篇幅很长,感谢耐心阅读。");
filmReviewInfo5.setCoverImg("https://img3.doubanio.com/view/thing_review/l/public/5506580.webp");
filmReviewInfo5.setCoverImgType(1); // 视图显示类型
filmReviewInfo5.setAuthor("弗吉尼亚的灵魂");
filmReviewInfo5.setPublishedDate("2021-1-15");
mList.add(filmReviewInfo5);
FilmReviewInfo filmReviewInfo6 = new FilmReviewInfo();
filmReviewInfo6.setTitle("《1Soul》的剧情分析与讨论,篇幅很长,感谢耐心阅读。");
filmReviewInfo6.setCoverImg("https://img3.doubanio.com/view/thing_review/l/public/5506580.webp");
filmReviewInfo6.setCoverImgType(1); // 视图显示类型
filmReviewInfo6.setAuthor("弗吉尼亚的灵魂");
filmReviewInfo6.setPublishedDate("2021-1-15");
mList.add(filmReviewInfo6);
FilmReviewInfo filmReviewInfo7 = new FilmReviewInfo();
filmReviewInfo7.setTitle("《2Soul》的剧情分析与讨论,篇幅很长,感谢耐心阅读。");
filmReviewInfo7.setCoverImg("https://img3.doubanio.com/view/thing_review/l/public/5506580.webp");
filmReviewInfo7.setCoverImgType(1); // 视图显示类型
filmReviewInfo7.setAuthor("弗吉尼亚的灵魂");
filmReviewInfo7.setPublishedDate("2021-1-15");
mList.add(filmReviewInfo7);
/** 模拟数据结束 */
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 1));
adapter = new FilmReviewAdapter(this, mList);
mRecyclerView.setAdapter(adapter);
adapter.setItemClickListener(new FilmReviewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
String title = mList.get(position).getTitle();
Toast.makeText(MainActivity.this, "" + title, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/mRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
FilmReviewAdapter
package com.fkm.recyclerviewmultitype.adapter;
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.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.fkm.recyclerviewmultitype.R;
import com.fkm.recyclerviewmultitype.model.FilmReviewInfo;
import java.util.List;
public class FilmReviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private List<FilmReviewInfo> mList;
private FilmReviewInfo data;
private OnItemClickListener mOnItemClickListener;
private static int VIEW_TYPE_1 = 1;
private static int VIEW_TYPE_2 = 2;
public FilmReviewAdapter(Context context, List<FilmReviewInfo> mList) {
this.mContext = context;
this.mList = mList;
}
@Override
public int getItemViewType(int position) {
if (mList.get(position).getCoverImgType() == 2) {
return VIEW_TYPE_2;
}
return VIEW_TYPE_1;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_2) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_film_review2, parent, false);
ViewHolder2 holder = new ViewHolder2(view);
return holder;
} else {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_film_review1, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
data = mList.get(position);
if (holder instanceof ViewHolder) {
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.movie_bg)
.error(R.drawable.movie_bg);
Glide.with(mContext).load(data.getCoverImg()).apply(options).into(((ViewHolder) holder).iv_cover);
((ViewHolder) holder).tv_author.setText(data.getAuthor());
((ViewHolder) holder).tv_published_date.setText(data.getPublishedDate());
((ViewHolder) holder).tv_title.setText(data.getTitle());
} else if (holder instanceof ViewHolder2) {
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.movie_bg)
.error(R.drawable.movie_bg);
Glide.with(mContext).load(data.getCoverImg()).apply(options).into(((ViewHolder2) holder).iv_cover);
((ViewHolder2) holder).tv_author.setText(data.getAuthor());
((ViewHolder2) holder).tv_published_date.setText(data.getPublishedDate());
((ViewHolder2) holder).tv_title.setText(data.getTitle());
}
if (mOnItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, pos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemLongClick(holder.itemView, pos);
return false;
}
});
}
}
@Override
public int getItemCount() {
return mList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView iv_cover;
private TextView tv_author;
private TextView tv_published_date;
private TextView tv_title;
public ViewHolder(View itemView) {
super(itemView);
iv_cover = (ImageView) itemView.findViewById(R.id.iv_cover);
tv_author = (TextView) itemView.findViewById(R.id.tv_author);
tv_published_date = (TextView) itemView.findViewById(R.id.tv_published_date);
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
}
}
public static class ViewHolder2 extends RecyclerView.ViewHolder {
private ImageView iv_cover;
private TextView tv_author;
private TextView tv_published_date;
private TextView tv_title;
public ViewHolder2(View itemView) {
super(itemView);
iv_cover = (ImageView) itemView.findViewById(R.id.iv_cover);
tv_author = (TextView) itemView.findViewById(R.id.tv_author);
tv_published_date = (TextView) itemView.findViewById(R.id.tv_published_date);
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
}
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public void setItemClickListener(OnItemClickListener mOnItemClickListener) {
this.mOnItemClickListener = mOnItemClickListener;
}
}
item_film_review1.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:cardBackgroundColor="@color/colorWhite"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="4"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_published_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:maxLength="10"
android:textColor="@android:color/black"
android:textSize="14sp" />
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1">
<ImageView
android:id="@+id/iv_cover"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="fitXY"
android:src="@drawable/img_default_movie_v"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"
app:layout_constraintDimensionRatio="27:40"
tools:background="@color/color_page_bg" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
item_film_review2.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:cardBackgroundColor="@color/colorWhite"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="4"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_published_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:maxLength="10"
android:textColor="@android:color/black"
android:textSize="14sp" />
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1">
<ImageView
android:id="@+id/iv_cover"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="fitXY"
android:src="@drawable/img_default_movie_v"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"
app:layout_constraintDimensionRatio="27:40"
tools:background="@color/color_page_bg" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
BaseModel
package com.fkm.recyclerviewmultitype.model;
import java.io.Serializable;
/**
* @description 实体基类
*/
public class BaseModel implements Serializable {
private static final long serialVersionUID = 1L;
}
FilmReviewInfo
package com.fkm.recyclerviewmultitype.model;
/**
* 影评信息表
*/
public class FilmReviewInfo extends BaseModel {
// 标题
private String title;
// 作者
private String author;
// 封面图片
private String coverImg;
// 封面图片类型
private int coverImgType;
// 发表日期
private String publishedDate;
// 跳转类型
private int jumpType;
// 文本内容
private String textContent;
// 内容图片Url
private String contentImgUrl;
// 浏览数量
private int pageView;
// 点赞数量
private int likesAmount;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getCoverImg() {
return coverImg;
}
public void setCoverImg(String coverImg) {
this.coverImg = coverImg;
}
public int getCoverImgType() {
return coverImgType;
}
public void setCoverImgType(int coverImgType) {
this.coverImgType = coverImgType;
}
public String getPublishedDate() {
return publishedDate;
}
public void setPublishedDate(String publishedDate) {
this.publishedDate = publishedDate;
}
public int getJumpType() {
return jumpType;
}
public void setJumpType(int jumpType) {
this.jumpType = jumpType;
}
public String getTextContent() {
return textContent;
}
public void setTextContent(String textContent) {
this.textContent = textContent;
}
public String getContentImgUrl() {
return contentImgUrl;
}
public void setContentImgUrl(String contentImgUrl) {
this.contentImgUrl = contentImgUrl;
}
public int getPageView() {
return pageView;
}
public void setPageView(int pageView) {
this.pageView = pageView;
}
public int getLikesAmount() {
return likesAmount;
}
public void setLikesAmount(int likesAmount) {
this.likesAmount = likesAmount;
}
}