RecyclerView
RecycleView是Android5.0后谷歌推出的一个用于在有限的窗口中展示大量数据集的控件。它可以实现与ListView和GridView一样的效果,提供了一种插拔式的体验,高度的解耦,异常的灵活,只需设置其提供的不同的LayoutManager,ItemAnimator和ItemDecoration,就能实现不同的效果。
RecyclerView的优点
- 支持局部刷新
- 可以自定义item增删时的动画
- 能够实现item拖拽和侧滑删除等功能
- 默认已实现View的复用,而且回收机制更加完善
使用方法
布局<主界面Activity>
<LinearLayout
android:layout_width="336dp"
android:layout_height="366dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="14dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button04">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list001"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="48dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp" />
</LinearLayout>
创建一个xml,做嵌入使用
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="100dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/newsPic"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="false" />
<TextView
android:id="@+id/newsTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
创建一个adapter类(主要的代码)
/*继承RecyclerView.Adapter
* NewsAdapter.ViewHolder 是一个内部static类,继承了
* RecyclerView.ViewHolder
*/
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private List<News> newsList;
//定义一个OnItemClickListener接口,这个接口是自定义的
private OnItemClickListener onItemClickListener;
//定义OnItemClickListener的操作方法
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
//构造函数,将需要显示的数组放进去。数据一般存储对象,对象里面定义需要显示的元素。在onBindViewHolder取出来使用
public NewsAdapter(List<News> newsList) {
this.newsList = newsList;
}
//创建ViewHolder,后续item布局里控件都是从ViewHolder中取出
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item,viewGroup,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
//绑定操作
@Override
public void onBindViewHolder(final ViewHolder viewHolder, int i) {
News news = newsList.get(i);
viewHolder.newsImage.setImageResource(news.getPic());
viewHolder.newsTitle.setText(news.getTitle());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListener != null){
onItemClickListener.onItemClick(v,viewHolder.getAdapterPosition()+1);
}
}
});
}
@Override
public int getItemCount() {
return newsList.size();
}
/**
* 自定义的ViewHolder
*/
static class ViewHolder extends RecyclerView.ViewHolder {
ImageView newsImage;
TextView newsTitle;
public ViewHolder(View itemView) {
super(itemView);
newsImage = itemView.findViewById(R.id.newsPic);
newsTitle = itemView.findViewById(R.id.newsTitle);
}
}
}
主程序:
//自定义的一个class,比较随意
List<News> newList = new ArrayList();
newList.add(new News("新闻1", R.drawable.pic001));
newList.add(new News("新闻2", R.drawable.pic001));
newList.add(new News("新闻3", R.drawable.pic001));
newList.add(new News("新闻4", R.drawable.pic001));
newList.add(new News("新闻5", R.drawable.pic001));
//创建一个NewsAdapter,将List放进去
NewsAdapter newsAdapter = new NewsAdapter(newList);
//实现OnClick操作
newsAdapter.setOnItemClickListener(new NewsAdapter.OnItemClickListener(){
public void onItemClick(View view, int position){
Toast.makeText(getApplicationContext(),"第" + position + "条数据", Toast.LENGTH_SHORT).show();
}
});
//获取控件
recView = findViewById(R.id.list001);
//创建一个LinearLayoutManager
LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
//设置控件布局
recView.setLayoutManager(layoutmanager);
//将Adapter绑到控件上
recView.setAdapter(newsAdapter);
//设置水平方向的隔离线
recView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
添加和删除条目
在Adapter里添加code
/* 添加数据*/
public void addItem() {
newsList.add(new News("新的", R.drawable.pic001));
notifyItemInserted(0);
}
/* 移除数据*/
public void removeItem(int position) {
newsList.remove(position);
notifyItemRemoved(position);
}
在主程序开启动态功能
recView.setItemAnimator(new DefaultItemAnimator());
自己画两个Button,执行addItem和removeItem即可!