public class RecyclerViewAdapter extends RecyclerView.Adapter implements View.OnClickListener {
private List mList;
private PackagePreference mPreference;
private OnRecyclerViewItemClickListener mListener;
private static int mFlag;
public final static int NO_CHECK = 0;
public final static int CHECK = 1;
public final static int PLUS = 2;
public RecyclerViewAdapter(List list, int flag) {
this.mList = list;
this.mFlag = flag;
mPreference = PackagePreference.getInstance(MyApplication.getContext());
}
/**
* 渲染具体的ViewHolder
*
* @param parent ViewHolder的容器
* @param viewType 根据该标志实现渲染不同类型的ViewHolder
* @return
*/
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//加载数据item的布局,生成VH返回
if (viewType == NO_CHECK) {
return new NoCheckViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card_no_check, parent, false));
} else if (viewType == CHECK) {
return new CheckViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card_check, parent, false));
} else if (viewType == PLUS) {
return new PlusViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card_plus, parent, false));
} else {
return null;
}
}
/**
* 绑定ViewHolder的数据
*
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
PackageMessage message = mList.get(position);
//数据绑定
if (holder instanceof NoCheckViewHolder) {
bindNoCheckViewHolder(message, (NoCheckViewHolder) holder);
} else if (holder instanceof CheckViewHolder) {
bindCheckViewHolder(message, (CheckViewHolder) holder);
}
holder.itemView.setTag(position);
holder.itemView.setOnClickListener(this);
}
@Override
public int getItemCount() {
if (null == mList) {
return 0;
}
return mList.size();
}
/**
* 决定元素的布局使用哪种类型
*
* @param position
* @return 传递给onCreateViewHolder的第二个参数
*/
@Override
public int getItemViewType(int position) {
if (mFlag == NO_CHECK) {
if (position == mList.size() - 1) {
return PLUS;
}
return NO_CHECK;
} else if (mFlag == CHECK) {
return CHECK;
} else {
return 100;
}
}
private void bindNoCheckViewHolder(PackageMessage message, NoCheckViewHolder holder) {
holder.tv.setText(message.getLabel());
holder.img.setImageDrawable(message.getIcon());
}
private void bindCheckViewHolder(PackageMessage message, final CheckViewHolder holder) {
holder.tv2.setText(message.getLabel());
holder.img2.setImageDrawable(message.getIcon());
final String packageName = message.getPackageName();
holder.checkBox2.setChecked(mPreference.getPackageMessage(packageName));
holder.checkBox2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.checkBox2.isChecked()) {
mPreference.putPackageMessage(packageName, true);
} else {
mPreference.putPackageMessage(packageName, false);
}
}
});
}
public static class NoCheckViewHolder extends RecyclerView.ViewHolder {
public ImageView img;
public TextView tv;
public NoCheckViewHolder(View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.img_card);
tv = (TextView) itemView.findViewById(R.id.tv_card);
}
}
public static class CheckViewHolder extends RecyclerView.ViewHolder {
public CheckBox checkBox2;
public ImageView img2;
public TextView tv2;
public CheckViewHolder(View itemView) {
super(itemView);
checkBox2 = (CheckBox) itemView.findViewById(R.id.checkbox_card2);
img2 = (ImageView) itemView.findViewById(R.id.img_card2);
tv2 = (TextView) itemView.findViewById(R.id.tv_card2);
}
}
public static class PlusViewHolder extends RecyclerView.ViewHolder {
public ImageView img_plus_card;
public PlusViewHolder(View itemView) {
super(itemView);
img_plus_card = (ImageView) itemView.findViewById(R.id.img_plus_card);
}
}
@Override
public void onClick(View v) {
if (null != mListener) {
mListener.onItemClick(v, (Integer) v.getTag());
}
}
public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
this.mListener = listener;
}
public interface OnRecyclerViewItemClickListener {
void onItemClick(View view, int position);
}
}
由于RecyclerView没有item的点击事件,所以此处在Adapter里面添加item的点击事件,定义一个接口OnRecyclerViewItemClickListener,写一个抽象方法onItemClick(View view,int position)把当前item的position设置进去,在onBindView里面监听item的点击事件。