1、依赖:implementation ‘com.android.support:recyclerview-v7:28.0.0-rc01’,后面的版本号,与implementation 'com.android.support:appcompat-v7:28.0.0-rc01’的版本号保持一致,
2、Adapter:
package com.fpt.myrecycletest;
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.TextView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> list;
private Context context;
private OnItemClickListener onItemClickListener;//外部接口调用
public MyAdapter(List<String> list, Context context){
this.list=list;
this.context=context;
}
@NonNull
@Override
public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
// 这里的i最好不为final,
View view= LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
MyAdapter.ViewHolder viewHolder=new MyAdapter.ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.ViewHolder viewHolder, int i) {
//这里的i可以为final,但是上面的方法中,最好不要设置为final,要不然点击的时候,不同item点击的时候,i一直未某一个参数
//这里可以进行监听事件的接口接入
viewHolder.mText.setText(list.get(i));
viewHolder.mTextsetTag(i);//将各个text进行标记
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView mText;
ViewHolder(View itemView) {
super(itemView);
mText = itemView.findViewById(R.id.mian_tv);
//设置点击事件应该在这里进行初始化
mtext.setOnClickListener(MyAdapter.this);
}
}
@Override
public void onClick(View v) {
int position= (int) v.getTag();
if (onItemClickListener != null) {
onItemClickListener.OnItemClick(v, position);
}
}
public void setLayoutOnlickListener(OnItemClickListener onlickListener){
onItemClickListener=onlickListener;
}
}
3、main中:
recyclerView=findViewById(R.id.recycle);
list=new ArrayList<>();
// recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));//设置横向
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));//设置3排;
// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));//设置瀑布流:
recyclerView.setItemAnimator(new DefaultItemAnimator());
for (int i=0;i<80;i++){
list.add("冯绍峰"+i);
}
MyAdapter adapter=new MyAdapter(list,getBaseContext());
recyclerView.setAdapter(adapter);
4、在第一次了解调试recycle时,需要注意的是:在main.xml中,
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycle">
</android.support.v7.widget.RecyclerView>
height的高度最好为wrap_content,要不然,一item占满了整个屏幕,看似只有一个item,其实已经成功了,但你还在那捣鼓半天,还不知道自己已经成功了。
详细的可以参照这位大神的博客:https://blog.csdn.net/tuike/article/details/79064750
public class WchatDetailMainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int File_type = 0;
private static final int Advertise_type = 1;
private static final int Simple_type = 2;
private List<WchatDetailInfo> list;
private Context context;
private int type=0;//0表示微信跳转,1表示qq跳转
public WchatDetailMainAdapter(Context context, List<WchatDetailInfo> list,int type) {
this.list = list;
this.context = context;
this.type=type;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holer = null;
if (viewType == File_type) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_wchat_detail_main_file_type, parent, false);
holer = new FileViewHolder(view);
} else if (viewType == Advertise_type) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_wchat_detail_main_advertise_type, parent, false);
holer = new AdvertiseViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_wchat_detail_main_simple_type, parent, false);
holer = new SimpleViewHolder(view);
}
return holer;
}
/**
* 不同item管理
* */
@Override
public int getItemViewType(int position) {
if (position == 0) {
return File_type;
} else if (position == 1) {
return Advertise_type;
}
return Simple_type;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof FileViewHolder) {
FileViewHolder fileViewHolder = (FileViewHolder) holder;
fileViewHolder.tv_size.setText(list.get(position).getSizeString());
fileViewHolder.btn_do.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if ("0B".equals(list.get(position).getSizeString())) {
ToastUtils.INSTANCE.showCustomToast("暂无文件", context);
return;
}
if (type==0){
JumpUtils.INSTANCE.fileDetailJump("其他文件", 7);
}else if (type==1){
JumpUtils.INSTANCE.fileDetailJump("其他文件", 16);
}
}
});
return;
}
if (holder instanceof AdvertiseViewHolder) {
AdvertiseViewHolder advertiseViewHolder = (AdvertiseViewHolder) holder;
return;
}
if (holder instanceof SimpleViewHolder) {
SimpleViewHolder simpleViewHolder = (SimpleViewHolder) holder;
simpleViewHolder.tv_simple_hint.setText(list.get(position).getHint());
simpleViewHolder.tv_simple_size.setText(list.get(position).getSizeString());
simpleViewHolder.tv_simple_type.setText(list.get(position).getType());
simpleViewHolder.simple_btn_do.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (type==0){
switch (position) {
case 2:
// 图片
if ("0B".equals(list.get(position).getSizeString())) {
ToastUtils.INSTANCE.showCustomToast("暂无文件", context);
return;
}
Log.d("----", "onClick图片: ");
JumpUtils.INSTANCE.fileDetailJump("图片", 8);
break;
case 3:
// 视频
if ("0B".equals(list.get(position).getSizeString())) {
ToastUtils.INSTANCE.showCustomToast("暂无文件", context);
return;
}
JumpUtils.INSTANCE.fileDetailJump("视频", 9);
break;
case 4:
// 文档
if ("0B".equals(list.get(position).getSizeString())) {
ToastUtils.INSTANCE.showCustomToast("暂无文件", context);
return;
}
Log.d("----", "onClick文档: ");
JumpUtils.INSTANCE.fileDetailJump("文档", 10);
break;
case 5:
// 音频
if ("0B".equals(list.get(position).getSizeString())) {
ToastUtils.INSTANCE.showCustomToast("暂无文件", context);
return;
}
JumpUtils.INSTANCE.fileDetailJump("音频", 11);
break;
}
}else if (type==1){
// qq跳转
switch (position) {
case 2:
// 图片
if ("0B".equals(list.get(position).getSizeString())) {
ToastUtils.INSTANCE.showCustomToast("暂无文件", context);
return;
}
Log.d("----", "onClick图片: ");
JumpUtils.INSTANCE.fileDetailJump("图片", 12);
break;
case 3:
// 视频
if ("0B".equals(list.get(position).getSizeString())) {
ToastUtils.INSTANCE.showCustomToast("暂无文件", context);
return;
}
JumpUtils.INSTANCE.fileDetailJump("视频", 13);
break;
case 4:
// 文档
if ("0B".equals(list.get(position).getSizeString())) {
ToastUtils.INSTANCE.showCustomToast("暂无文件", context);
return;
}
Log.d("----", "onClick文档: ");
JumpUtils.INSTANCE.fileDetailJump("文档", 14);
break;
case 5:
// 音频
if ("0B".equals(list.get(position).getSizeString())) {
ToastUtils.INSTANCE.showCustomToast("暂无文件", context);
return;
}
JumpUtils.INSTANCE.fileDetailJump("音频", 15);
break;
}
}
}
});
FragmentHomeFileAdapter adapter = new FragmentHomeFileAdapter(new OnRecycleItemClickListener<FileInfo>() {
@Override
public void onItemClick(@NotNull View view, int position, FileInfo data) {
}
});
List<FileInfo> listInfo = new ArrayList<>();
if (list.get(position).getList().size()>4){
for (int i = 0; i < 4; i++) {
listInfo.add(list.get(position).getList().get(i));
}
}else {
for (int i=0;i<list.get(position).getList().size();i++){
listInfo.add(list.get(position).getList().get(i));
}
}
adapter.setTotalNumber(list.get(position).getList().size());
simpleViewHolder.recyclerView.setLayoutManager(new GridLayoutManager(context, 4));
simpleViewHolder.recyclerView.setAdapter(adapter);
simpleViewHolder.recyclerView.setNestedScrollingEnabled(false);
adapter.appendToList(listInfo);
adapter.notifyDataSetChanged();
return;
}
}
@Override
public int getItemCount() {
return list.size();
}
public class FileViewHolder extends RecyclerView.ViewHolder {
TextView tv_size;
TextView btn_do;
public FileViewHolder(@NonNull View itemView) {
super(itemView);
tv_size = itemView.findViewById(R.id.item_wchat_detail_file_tv_size);
btn_do = itemView.findViewById(R.id.item_wchat_detail_file_btn);
}
}
public class AdvertiseViewHolder extends RecyclerView.ViewHolder {
public AdvertiseViewHolder(@NonNull View itemView) {
super(itemView);
}
}
public class SimpleViewHolder extends RecyclerView.ViewHolder {
TextView tv_simple_size, tv_simple_type, tv_simple_hint;
TextView simple_btn_do;
RecyclerView recyclerView;
public SimpleViewHolder(@NonNull View itemView) {
super(itemView);
tv_simple_hint = itemView.findViewById(R.id.item_wchat_main_simple_tv_hint);
tv_simple_type = itemView.findViewById(R.id.item_wchat_main_simple_tv_type);
tv_simple_size = itemView.findViewById(R.id.item_wchat_main_simple_tv_size);
simple_btn_do = itemView.findViewById(R.id.item_wchat_main_simple_btn);
recyclerView = itemView.findViewById(R.id.item_wchat_main_simple_recycle);
}
}
}