一、创建通用 RecyclerView.Adapter
public class CommonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = AppConstants.APP_TAG + " CommonAdapter ";
private List<BaseBean> mDatas;
private Context mContext;
private LayoutInflater mLayoutInfater;
private BaseHolder.OnRecyclerViewItemClickListener mOnItemClickListener;
/**
* Construct function.
*/
public CommonAdapter(Context context, List<BaseBean> baseBeans) {
LogUtil.d(TAG + "CommonAdapter ");
this.mContext = context;
mLayoutInfater = LayoutInflater.from(mContext);
this.mDatas = baseBeans;
}
public void setDatas(List<BaseBean> baseBeans) {
this.mDatas = baseBeans;
}
/**
* Add item data.
*
* @param baseBean BaseBean
*/
public void addItem(int position, BaseBean baseBean) {
if (mDatas == null) {
mDatas = new ArrayList<>();
}
int count = mDatas.size();
mDatas.add(position, baseBean);
notifyItemInserted(position);
notifyItemRangeChanged(position, count - position);
}
public void removeItem(int position) {
if (mDatas == null) {
mDatas = new ArrayList<>();
}
int count = mDatas.size();
mDatas.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(0, count - position);
}
public void editItem(int position, BaseBean baseBean) {
mDatas.set(position, baseBean);
notifyItemChanged(position, SendReviewHolder.NOTIFY_ALL); //整个item局部刷新
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LogUtil.d(TAG + "onCreateViewHolder viewType:" + viewType);
switch (viewType) {
case BaseBean.SEND_REVIEW:
view = mLayoutInfater.inflate(R.layout.layout_send_review_item, parent, false);
return new SendReviewHolder(view, mOnItemClickListener);
case BaseBean.TO_SEND:
view = mLayoutInfater.inflate(R.layout.layout_to_send_item, parent, false);
return new ToSendHolder(view, mOnItemClickListener);
default:
LogUtil.d(TAG + "onCreateViewHolder error can not find viewType:" + viewType);
break;
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
LogUtil.d(TAG + "onBindViewHolder(..) position:" + position);
int viewType = getItemViewType(position);
((BaseHolder) holder).bindHolder(mDatas.get(position), position);
}
// @Override
// public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) {
// LogUtil.d(TAG + "onBindViewHolder(...) position:" + position);
// ((BaseHolder) holder).bindHolder(mDatas.get(position), position, payloads);
// }
@Override
public int getItemCount() {
return mDatas.size();
}
@Override
public int getItemViewType(int position) {
return mDatas.get(position).getType();
}
public void setOnItemClickListener(
BaseHolder.OnRecyclerViewItemClickListener listener) {
this.mOnItemClickListener = listener;
}
}
二、创建 BaseHolder 及其 子类 ToSendHolder和条目布局
ToSendHolder 用来刷新一个条目界面
public abstract class BaseHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private static final String TAG = AppConstants.APP_TAG + "BaseHolder ";
protected OnRecyclerViewItemClickListener mItemClickListener = null;
public BaseHolder(View itemView,
OnRecyclerViewItemClickListener recyclerViewItemClickListener) {
super(itemView);
mItemClickListener = recyclerViewItemClickListener;
}
public abstract void bindHolder(BaseBean model, int position);
public <T extends View> T getView(int viewId) { //将view存储到数组中,每次从数组中读取view
View view = sparseArray.get(viewId);
if (view == null) {
view = itemView.findViewById(viewId);
sparseArray.put(viewId, view);
}
LogUtil.d(TAG + " getView() viewId:" + viewId + " view:" + view);
return (T) view;
}
public abstract void bindHolder(BaseBean model, int position, @NonNull List<Object> payloads);
public interface OnRecyclerViewItemClickListener {
void onItemClick(View view, SurpriseBean surpriseBean);
}
public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
mItemClickListener = listener;
}
@Override
public void onClick(View v) {
mItemClickListener.onItemClick(v, (SurpriseBean) v.getTag());
}
}
public class ToSendHolder extends BaseHolder {
private static final String TAG = AppConstants.APP_TAG + "ToSendHolder ";
private TextView mTextMsgContent;
/**
* Construct function.
*
* @param itemView item view.
* @param recyclerViewItemClickListener recyclerViewItemClickListener.
*/
public ToSendHolder(View itemView,
OnRecyclerViewItemClickListener
recyclerViewItemClickListener) {
super(itemView, recyclerViewItemClickListener);
this.mItemClickListener = recyclerViewItemClickListener;
mTextMsgContent = getView(R.id.txt_msg_content); //通过父类中 getView 方法获取view,不用每次从父布局中遍历
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mItemClickListener.onItemClick(itemView, (SurpriseBean) itemView.getTag());
//itemView.setBackgroundColor(ResourcesUtil.getColor(
// MyApplication.getAppContext(), R.color.colorPrimary));
}
});
}
@Override
public void bindHolder(BaseBean model, int position) {
LogUtil.d(TAG + " bindHolder() 111 position:" + position
+ " model:" + model + " mTextMsgContent:" + mTextMsgContent);
doBindHolder(model, position, 0);
}
@Override
public void bindHolder(BaseBean model, int position, @NonNull List<Object> payloads) {
doBindHolder(model, position, 0);
}
private void doBindHolder(BaseBean model, int position, int nofifyType) {
if (!(model instanceof ToSendBean)) {
LogUtil.d(TAG + " bindHolder() 222 not ToSendBean return");
return;
}
ToSendBean toSendBean = (ToSendBean) model;
if (null == toSendBean) {
return;
}
SurpriseBean surpriseBean = toSendBean.getSurpriseBean();
if (null == surpriseBean || null == mTextMsgContent) {
LogUtil.d(TAG + " bindHolder() 333 return!");
return;
}
String content = surpriseBean.getMContent();
long id = surpriseBean.getMId();
LogUtil.d(TAG + " bindHolder() 444 content:" + content + " id:" + id);
itemView.setTag(surpriseBean);
mTextMsgContent.setText(content);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="wrap_content"
android:layout_height="@dimen/to_send_sub_item_height"
android:clickable="true"
android:background="@color/listItemColor"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="@+id/txt_msg_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:gravity="center"
android:text="@string/txt_happy_birthday"
android:textSize="@dimen/text_size_large" />
</RelativeLayout>
相关bean自己定义
三、activity中 调用加载RecyclerView
private void initCommonAdapter() {
LogUtil.d(TAG + "initCommonAdapter");
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.addItemDecoration(new SpacesItemDecoration(getResources()
.getDimensionPixelSize(R.dimen.list_view_item_padding)));
mDatas = SurpriseMessageUtil.getToSendMessageList();//此处获取自定义bean列表
mCommonAdapter = new CommonAdapter(this, mDatas);
setAdapterListener();
mRecyclerView.setAdapter(mCommonAdapter);
}
调用addItem方法可以动态添加列表条目
调用removeItem方法可以动态删除列表条目
调用editItem方法可以动态编辑列表条目