Sharing a class case of recycleview, I personally feel very good, the arrangement of recycleview is explained, and the events of click and long-press are realized at the same time, of course, we can also click on the corresponding item to pop up a dialog box to delete the row. The item, or click the item to enter a new interface, wait, see your needs and abilities!
layout_grid_itm.xml
matrix arrangement
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
app:cardUseCompatPadding="true">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/cat"
android:layout_gravity="center"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_margin="10dp"
android:gravity="center"
android:text="标题"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
waterfall flow arrangement
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
app:cardUseCompatPadding="true">
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<ImageView
android:id="@+id/icon"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:src="@drawable/cat"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_below="@id/icon"
android:layout_margin="10dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="标题"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
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<Map<String,Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//准备数据源
initData();
//获取recyclerView控件
RecyclerView recyclerView = this.findViewById(R.id.recyclerView);
//创建布局管理器
// LinearLayoutManager layoutManager = new LinearLayoutManager(this);
GridLayoutManager layoutManager = new GridLayoutManager(this,5);
//StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
// layoutManager.setOrientation(RecyclerView.HORIZONTAL);
// layoutManager.setReverseLayout(true);
//设置布局管理器
recyclerView.setLayoutManager(layoutManager);
//创建适配器
MyAdapter adapter = new MyAdapter(data);
adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this,"点击了第"+position+"个item",Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(MainActivity.this,"长按了第"+position+"个item",Toast.LENGTH_SHORT).show();
}
});
//设置适配器
recyclerView.setAdapter(adapter);
}
private void initData() {
//准备数据源(List<Map>)
data = new ArrayList<Map<String, Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("icon",R.drawable.cat);
map.put("title","小猫");
map.put("content","这是一只可爱的小猫。这是一只可爱的小猫。这是一只可爱的小猫。这是一只可爱的小猫。这是一只可爱的小猫。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.tiger);
map.put("title","老虎");
map.put("content","这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.siberiankusky);
map.put("title","哈士奇");
map.put("content","这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.siberiankusky);
map.put("title","哈士奇");
map.put("content","这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.tiger);
map.put("title","老虎");
map.put("content","这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.tiger);
map.put("title","老虎");
map.put("content","这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.tiger);
map.put("title","老虎");
map.put("content","这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.tiger);
map.put("title","老虎");
map.put("content","这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。这是一只威猛的老虎。");
data.add(map);
map = new HashMap<String ,Object>();
map.put("icon",R.drawable.siberiankusky);
map.put("title","哈士奇");
map.put("content","这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。这是一只呆萌的哈士奇。");
data.add(map);
}
}
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<MyAdapter.MyViewHolder> {
private List<Map<String,Object>> list;
private MyAdapter.OnItemClickListener onItemClickListener;//事件回调监听
public MyAdapter(List<Map<String, Object>> 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;
}
}
screenshot