RecyclerView 复杂页面实现详解
代码
Activity
package com.wjn.viewlistdemo.activity.recyclerview.complex;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.wjn.viewlistdemo.R;
import com.wjn.viewlistdemo.activity.recyclerview.MyRecycleViewAdapter;
import com.wjn.viewlistdemo.activity.recyclerview.RecycleViewAdapterInterface;
import com.wjn.viewlistdemo.activity.recyclerview.RecyclerViewActivity;
import com.wjn.viewlistdemo.activity.recyclerview.Student;
import java.util.List;
public class ComplexActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
private List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complex);
initView();
}
/**
* 初始化各种View
*/
private void initView() {
recyclerView = findViewById(R.id.activity_complex_recycleview);
list = ComplexUtils.getInstance().getList();
//1.设置LinearLayoutManager ListView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//2.设置固定大小
recyclerView.setHasFixedSize(true);
//3.自定义分割线
DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.dy_line));
recyclerView.addItemDecoration(divider);
//4.设置Adapter
adapter = new RecyclerViewAdapter(this, list);
recyclerView.setAdapter(adapter);
//5.Item点击以及长按监听
setRecycleViewItemClickAndLongClick();
}
/**
* Item点击或者长按
*/
private void setRecycleViewItemClickAndLongClick() {
if (null != adapter) {
adapter.setItemClickCallBack(new ItemClickCallBack() {
@Override
public void onItemCilck(View view, int position, int type) {
String text = "点击了:" + " Position:" + position + " Type:" + type;
Toast.makeText(ComplexActivity.this, text, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongCilck(View view, int position, int type) {
String text = "长按了:" + " Position:" + position + " Type:" + type;
Toast.makeText(ComplexActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}
}
}
Activity布局
Adapter
package com.wjn.viewlistdemo.activity.recyclerview.complex;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.wjn.viewlistdemo.R;
import com.wjn.viewlistdemo.activity.recyclerview.MyRecycleViewAdapter;
import com.wjn.viewlistdemo.activity.recyclerview.Student;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter {
public static final int TYPE_ONE = 1;
public static final int TYPE_TWO = 2;
public static final int TYPE_THREE = 3;
private ItemClickCallBack itemClickCallBack;
public void setItemClickCallBack(ItemClickCallBack itemClickCallBack) {
this.itemClickCallBack = itemClickCallBack;
}
private Context context;
private List list;
private LayoutInflater mInflater;
public RecyclerViewAdapter(Context context, List list) {
this.context = context;
this.list = list;
mInflater = LayoutInflater.from(context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == TYPE_ONE) {
View view = mInflater.inflate(R.layout.complex_item1, viewGroup, false);
OneViewHolder oneViewHolder = new OneViewHolder(view);
return oneViewHolder;
} else if (i == TYPE_TWO) {
View view = mInflater.inflate(R.layout.complex_item2, viewGroup, false);
TwoViewHolder twoViewHolder = new TwoViewHolder(view);
return twoViewHolder;
} else if (i == TYPE_THREE) {
View view = mInflater.inflate(R.layout.complex_item3, viewGroup, false);
ThreeViewHolder threeViewHolder = new ThreeViewHolder(view);
return threeViewHolder;
} else {
return null;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (null == viewHolder) {
return;
}
int viewType = getItemViewType(i);
switch (viewType) {
case TYPE_ONE://类型1
//转换ViewHolder
OneViewHolder oneViewHolder = (OneViewHolder) viewHolder;
//绑定数据
oneViewHolder.textView1.setText(list.get(i).getName() + "【" + (i + 1) + "】");
oneViewHolder.textView2.setText(list.get(i).getDescribe());
oneViewHolder.circleImageView.setBackgroundResource(R.mipmap.patient_ava);
//绑定事件
setClick(oneViewHolder, i, viewType);
break;
case TYPE_TWO://类型2
//转换ViewHolder
TwoViewHolder twoViewHolder = (TwoViewHolder) viewHolder;
//绑定数据
twoViewHolder.textView.setText(list.get(i).getDescribe());
//绑定事件
setClick(twoViewHolder, i, viewType);
break;
case TYPE_THREE://类型3
//转换ViewHolder
ThreeViewHolder threeViewHolder = (ThreeViewHolder) viewHolder;
//绑定数据
threeViewHolder.textView.setText(list.get(i).getName());
threeViewHolder.imageView.setBackgroundResource(R.mipmap.home_body);
//绑定事件
setClick(threeViewHolder, i, viewType);
break;
}
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public int getItemViewType(int position) {
return list.get(position).getType();
}
/**
* OneViewHolder
*/
public class OneViewHolder extends RecyclerView.ViewHolder {
private ImageView circleImageView;
private TextView textView1;
private TextView textView2;
public OneViewHolder(View itemView) {
super(itemView);
circleImageView = itemView.findViewById(R.id.recycleview_circleimageview);
textView1 = itemView.findViewById(R.id.recycleview_item_textview1);
textView2 = itemView.findViewById(R.id.recycleview_item_textview2);
}
}
/**
* TwoViewHolder
*/
public class TwoViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public TwoViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.complex_item2_textview);
}
}
/**
* ThreeViewHolder
*/
public class ThreeViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ThreeViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.complex_item3_imageview);
textView = itemView.findViewById(R.id.complex_item3_textview);
}
}
/**
* 捕获RecycleView的Item点击以及长按事件
*/
private void setClick(RecyclerView.ViewHolder viewHolder, final int position, final int type) {
if (null == viewHolder || position < 0) {
return;
}
//Item点击
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != itemClickCallBack) {
itemClickCallBack.onItemCilck(v, position, type);
}
}
});
//Item长按
viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (null != itemClickCallBack) {
itemClickCallBack.onItemLongCilck(v, position, type);
}
return true;
}
});
}
}
Item1布局
Item2布局
Item3布局
接口
package com.wjn.viewlistdemo.activity.recyclerview.complex;
import android.view.View;
public interface ItemClickCallBack {
void onItemCilck(View view, int position, int type);
void onItemLongCilck(View view, int position, int type);
}
工具类
package com.wjn.viewlistdemo.activity.recyclerview.complex;
import com.wjn.viewlistdemo.activity.recyclerview.Student;
import java.util.ArrayList;
import java.util.List;
public class ComplexUtils {
private ComplexUtils() {
}
private static class Holder {
private static ComplexUtils complexUtils = new ComplexUtils();
}
public static ComplexUtils getInstance() {
return Holder.complexUtils;
}
public List getList() {
List list = new ArrayList<>();
Student student1 = new Student();
student1.setName("詹姆斯");
student1.setType(1);
student1.setDescribe(student1.getName() + ":很懒什么也没留下!");
Student student2 = new Student();
student2.setName("韦德");
student2.setType(1);
student2.setDescribe(student2.getName() + ":很懒什么也没留下!");
Student student3 = new Student();
student3.setName("山东鲁能");
student3.setType(2);
student3.setDescribe(student3.getName() + ":很懒什么也没留下!");
Student student4 = new Student();
student4.setName("保罗");
student4.setType(2);
student4.setDescribe(student4.getName() + ":很懒什么也没留下!");
Student student5 = new Student();
student5.setName("安东尼");
student5.setType(3);
student5.setDescribe(student5.getName() + ":很懒什么也没留下!");
Student student6 = new Student();
student6.setName("蒿俊闵");
student6.setType(1);
student6.setDescribe(student6.getName() + ":很懒什么也没留下!");
Student student7 = new Student();
student7.setName("李霄鹏");
student7.setType(2);
student7.setDescribe(student7.getName() + ":很懒什么也没留下!");
for (int i = 0; i < 20; i++) {
list.add(student1);
list.add(student2);
list.add(student3);
list.add(student4);
list.add(student5);
list.add(student6);
list.add(student7);
}
return list;
}
}
实体类
package com.wjn.viewlistdemo.activity.recyclerview;
public class Student {
private String ava;
private String name;
private String describe;
private int type;
public String getAva() {
return ava;
}
public void setAva(String ava) {
this.ava = ava;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}