准备工作
添加依赖
implementation “androidx.recyclerview:recyclerview:1.1.0”
定义适配器
这个控件使用方法和ListView差不多,都需要先自定义一个布局和一个适配类型,但是适配器的构造不一样,下面是适配器的代码,解析在注释。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class photo2Adapter extends RecyclerView.Adapter<photo2Adapter.ViewHolder> { private List<photo1> mphoto2List;
static class ViewHolder extends RecyclerView.ViewHolder{ TextView textView; ImageView imageView; public ViewHolder(View view){ super(view); textView = (TextView) view.findViewById(R.id.name); imageView = (ImageView) view.findViewById((R.id.photo1_Image)); } }
public photo2Adapter(List<photo1> photo2List){ mphoto2List = photo2List; }
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){ View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.photo1, parent,false); ViewHolder holder = new ViewHolder(view); return holder; }
public void onBindViewHolder(ViewHolder holder, int position){ photo1 photo2 = mphoto2List.get(position); holder.imageView.setImageResource(photo2.getID()); holder.textView.setText(photo2.getname()); }
public int getItemCount(){ return mphoto2List.size(); } }
|
使用
基本使用
1 2 3 4 5
| RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); photo2Adapter adapter = new photo2Adapter(photo1List); recyclerView.setAdapter(adapter);
|
水平滚动
如何改成水平滚动呢,只需在传送数据那块代码中添加一句代码即可
1 2
| LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
|
点击事件
在适配器中添加代码,看注释:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| View photoView; TextView textView; ImageView imageView; public ViewHolder(View view){ super(view); photoView = view; textView = (TextView) view.findViewById(R.id.name); imageView = (ImageView) view.findViewById((R.id.photo1_Image)); } .... final ViewHolder holder = new ViewHolder(view);
holder.photoView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); photo1 photo2 = mphoto2List.get(position); Toast.makeText(v.getContext(),"你点击的是:"+ photo2.getname(),Toast.LENGTH_LONG).show(); } });
holder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int position = holder.getAdapterPosition(); photo1 photo2 = mphoto2List.get(position); Toast.makeText(v.getContext(),"你点击的是:"+ photo2.getname(),Toast.LENGTH_LONG).show(); } });
return holder;
|
进阶
适配器外的点击事件
在适配器里边的点击事件,直接做一个监听就行了,在外边设置监听,就要写多几行代码(长按事件类同)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| private OnitemClick onitemClick;
public void setOnitemClickLintener (OnitemClick onitemClick) { this.onitemClick = onitemClick; } public interface OnitemClick { void onItemClick(int position); } public void onBindViewHolder(final ViewHolder holder, final int position){ final note_list note_list= mList.get(position); holder.textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onitemClick.onItemClick(position); } }); }
|
1 2 3 4 5 6
| adapter.setOnitemClickLintener(new MyAdapter.OnitemClick() { @Override public void onItemClick(int position) { } });
|
设置item的间距
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int space;
public SpacesItemDecoration(int space) { this.space = space; }
@Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.left = space; outRect.right = space; outRect.bottom = space; if (parent.getChildLayoutPosition(view) == 0) outRect.top = space; } }
|
1 2
| int space = 50; recyclerView.addItemDecoration(new SpacesItemDecoration(space));
|
item的添加与删除
添加
在Adapter添加代码
1 2 3 4 5 6 7
| public void addData(int position, note_list note_list) {
mList.add(position,note_list); notifyItemInserted(position); }
|
在外面的Activity调用
1 2
| adapter.addData(0,note_list);
|
删除
在Adapter添加代码
1 2 3 4 5 6 7 8
| public void removeData(int position) { mList.remove(position); notifyItemRemoved(position); notifyDataSetChanged(); }
|
在外面的Activity调用
1
| adapter.removeData(position);
|