分享一个recycleview的课堂案例,个人感觉特别好,把recycleview的排列方式都有讲解,同时实现了单击和长按的事件,当然我们也可以实现点击对应的item弹出一个对话框来经行是否删除该item,或者点击item进入一个新的界面,等,看你们的需求和能力哦!
layout_grid_itm.xml
矩阵排列
瀑布流排列
MainActivity.java
package com.example.recyclerviewdemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private List
MyAdapter
package com.example.recyclerviewdemo;
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 java.util.List;
import java.util.Map;
public class MyAdapter extends RecyclerView.Adapter {
private List> list;
private MyAdapter.OnItemClickListener onItemClickListener;//事件回调监听
public MyAdapter(List> list) {
this.list = list;
}
//用于加载Item界面的布局文件,相当于getView方法中的创建View和ViewHolder对象
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//View view = View.inflate(parent.getContext(),R.layout.layout_item,null);//反射行布局
View view = View.inflate(parent.getContext(),R.layout.layout_grid_item,null);//反射行布局
// View view = View.inflate(parent.getContext(),R.layout.layout_staggered_item,null);//反射行布局
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
//绑定数据和行布局
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
holder.icon.setImageResource((Integer) list.get(position).get("icon"));
holder.title.setText((CharSequence) list.get(position).get("title"));
// holder.content.setText((CharSequence) list.get(position).get("content"));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemClick(holder.itemView,pos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemLongClick(holder.itemView,pos);
return true;
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView icon;
TextView title;
// TextView content;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
icon = itemView.findViewById(R.id.icon);
title = itemView.findViewById(R.id.title);
// content = itemView.findViewById(R.id.content);
}
}
public interface OnItemClickListener{
void onItemClick(View view,int position);
void onItemLongClick(View view,int position);
}
public void setOnItemClickListener(MyAdapter.OnItemClickListener listener){
this.onItemClickListener = listener;
}
}
截图