列表的数据往往会跟随业务逻辑不断刷新,所呈现出来的数据需要动态更新,那么RecyclerView是如何动态更新数据的呢?
之前在学习ListView的时候如果数据改变,需要调用notifyDataSetChanged()方法来刷新数据,而在RecyclerView中当数据改变时分别调用notifyItemChanged、notifyItemInserted和notifyItemRemoved方法来更新页面数据。
接下来通过一个案例来学习如何动态更新数据,当单击某个item时则在其下方插入一个item,如果长压某个item时则删除对应item。
继续使用上期的案例,首先在RecyclerViewAdapter类中新增一个插入和删除处理的公开方法,RecyclerViewAdapter类修改后的代码如下:
package com.jinyu.cqkxzsxy.android.advancedviewsample.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.jinyu.cqkxzsxy.android.advancedviewsample.R;
import java.util.ArrayList;
/**
* @创建者 鑫鱻
* @描述 Android零基础入门到精通系列教程
* 首发微信公众号分享达人秀(ShareExpert)
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<String> mDatas = null;
private LayoutInflater mInflater = null;
private OnItemClickListener mOnItemClickListener = null;
private OnItemLongClickListener mOnItemLongClickListener = null;
public RecyclerViewAdapter2(Context context, ArrayList<String> datas) {
this.mDatas = datas;
this.mInflater = LayoutInflater.from(context);
}
// 创建新View,被LayoutManager所调用
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
ViewHolder vewHolder = new ViewHolder(view);
return vewHolder;
}
// 将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
String name = mDatas.get(position);
holder.titleTv.setText("Title " + name);
holder.contenTv.setText("content " + name);
// 点击事件注册及分发
if(null != mOnItemClickListener) {
holder.titleTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onClick(holder.titleTv, position);
}
});
holder.contenTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onClick(holder.contenTv, position);
}
});
}
// 长按事件注册及分发
if(null != mOnItemLongClickListener) {
holder.titleTv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return mOnItemLongClickListener.onLongClick(holder.titleTv, position);
}
});
holder.contenTv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return mOnItemLongClickListener.onLongClick(holder.contenTv, position);
}
});
}
}
// 获取数据的数量
@Override
public int getItemCount() {
return mDatas == null ? 0 : mDatas.size();
}
// 设置点击事件
public void setOnItemClickListener(OnItemClickListener l) {
this.mOnItemClickListener = l;
}
// 设置长按事件
public void setOnItemLongClickListener(OnItemLongClickListener l) {
this.mOnItemLongClickListener = l;
}
// 在对应位置增加一个item
public void addData(int position) {
mDatas.add(position, "Insert " + position);
notifyItemInserted(position);
}
// 删除对应item
public void removeData(int position) {
mDatas.remove(position);
notifyItemRemoved(position);
}
// 点击事件接口
public interface OnItemClickListener {
void onClick(View parent, int position);
}
// 长按事件接口
public interface OnItemLongClickListener {
boolean onLongClick(View parent, int position);
}
// 自定义的ViewHolder,持有每个Item的的所有界面组件
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView titleTv = null;
public TextView contenTv = null;
public ViewHolder(View itemView) {
super(itemView);
titleTv = (TextView) itemView.findViewById(R.id.title_tv);
contenTv = (TextView) itemView.findViewById(R.id.content_tv);
}
}
}
然后在Activity中事件回调的时候进行插入和删除处理,RecyclerViewActivity修改后的代码如下:
package com.jinyu.cqkxzsxy.android.advancedviewsample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.jinyu.cqkxzsxy.android.advancedviewsample.adapter.RecyclerViewAdapter;
import com.jinyu.cqkxzsxy.android.advancedviewsample.view.RecyclerViewItemDivider;
import java.util.ArrayList;
/**
* @创建者 鑫鱻
* @描述 Android零基础入门到精通系列教程,欢迎关注微信公众号ShareE