适配器My Adapter.java
两个布局文件我就写了,和上次发布的博客一样,
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
MainActivity.java
package com.example.recyclerviewdemo;
//Edit By DY.memory
import androidx.appcompat.app.AlertDialog;
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.content.DialogInterface;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.DialogPreference;
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 ArrayList> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<>();
//准备数据源
initData();
//获取recyclerView控件
RecyclerView recyclerView = this.findViewById(R.id.recyclerView);
//创建布局管理器
// LinearLayoutManager layoutManager = new LinearLayoutManager(this);
GridLayoutManager layoutManager = new GridLayoutManager(this,4);
//StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
// layoutManager.setOrientation(RecyclerView.HORIZONTAL);
// layoutManager.setReverseLayout(true);
//设置布局管理器
recyclerView.setLayoutManager(layoutManager);
//创建适配器
final MyAdapter adapter = new MyAdapter(list);
adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, final int position) {
//Toast.makeText(MainActivity.this,"点击了第"+position+"个item",Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("RecycleView");
builder.setMessage("are you sure delate the item");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
list.remove(position);
adapter.notifyDataSetChanged();
}
});
builder.setNeutralButton("cancel",null);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"点击了第"+position+"个item",Toast.LENGTH_SHORT).show();
}
});
builder.create().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)
list = new ArrayList>();
Map map = new HashMap();
map.put("icon",R.drawable.cat);
map.put("title","小猫");
map.put("content","这是一只可爱的小猫。这是一只可爱的小猫。这是一只可爱的小猫。这是一只可爱的小猫。这是一只可爱的小猫。");
list.add(map);
map = new HashMap();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
list.add(map);
map = new HashMap();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
list.add(map);
map = new HashMap();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
list.add(map);
map = new HashMap();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
list.add(map);
map = new HashMap();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
list.add(map);
map = new HashMap();
map.put("icon",R.drawable.fawn);
map.put("title","小鹿");
map.put("content","这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。这是一只可爱的小鹿。");
list.add(map);
}
}
截图