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<recyclerview.viewholder> {
//item 类型
public static final int ITEM_TYPE_HEADER = 0;
public static final int ITEM_TYPE_CONTENT = 1;
public static final int ITEM_TYPE_BOTTOM = 2;
private ArrayList<string> mDatas = null;
private LayoutInflater mInflater = null;
private OnItemClickListener mOnItemClickListener = null;
private OnItemLongClickListener mOnItemLongClickListener = null;
public RecyclerViewAdapter(Context context, ArrayList<string> datas) {
this.mDatas = datas;
this.mInflater = LayoutInflater.from(context);
}
// 内容长度
public int getContentItemCount(){
return mDatas == null ? 0 : mDatas.size();
}
// 判断当前item是否是HeadView
public boolean isHeaderView(int position) {
return 0 == position;
}
// 判断当前item是否是FooterView
public boolean isBottomView(int position) {
return position == (getContentItemCount() + 1);
}
// 判断当前item类型
@Override
public int getItemViewType(int position) {
if (0 == position) {
// 头部View
return ITEM_TYPE_HEADER;
} else if (getContentItemCount() + 1 == position) {
// 底部View
return ITEM_TYPE_BOTTOM;
} else {
// 内容View
return ITEM_TYPE_CONTENT;
}
}
// 创建新View,被LayoutManager所调用
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vewHolder = null;
View view = null;
if(ITEM_TYPE_HEADER == viewType) {
view = mInflater.inflate(R.layout.recyclerview_header, parent, false);
vewHolder = new HeaderViewHolder(view);
} else if(ITEM_TYPE_BOTTOM == viewType) {
view = mInflater.inflate(R.layout.recyclerview_footer, parent, false);
vewHolder = new FooterViewHolder(view);
} else {
view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
vewHolder = new ViewHolder(view);
}
return vewHolder;
}
// 将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof HeaderViewHolder) {
} else if(holder instanceof FooterViewHolder){
}else {
final ViewHolder viewHolder = (ViewHolder) holder;
String name = mDatas.get(position - 1);
viewHolder.titleTv.setText("Title " + name);
viewHolder.contenTv.setText("content " + name);
// 点击事件注册及分发
if (null != mOnItemClickListener) {
viewHolder.titleTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onClick(viewHolder.titleTv, position);
}
});
viewHolder.contenTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onClick(viewHolder.contenTv, position);
}
});
}
// 长按事件注册及分发
if (null != mOnItemLongClickListener) {
viewHolder.titleTv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return mOnItemLongClickListener.onLongClick(viewHolder.titleTv, position);
}
});
viewHolder.contenTv.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return mOnItemLongClickListener.onLongClick(viewHolder.contenTv, position);
}
});
}
}
}
// 获取数据的数量
@Override
public int getItemCount() {
return getContentItemCount() + 2;
}
// 设置点击事件
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);
if(position != getItemCount()) {
notifyItemRangeChanged(position, getItemCount());
}
}
// 删除对应item
public void removeData(int position) {
mDatas.remove(position);
notifyItemRemoved(position);
if(position != getItemCount()) {
notifyItemRangeChanged(position, getItemCount());
}
}
// 点击事件接口
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);
}
}
// 头部 ViewHolder
public class HeaderViewHolder extends RecyclerView.ViewHolder {
public HeaderViewHolder(View itemView) {
super(itemView);
}
}
// 底部 ViewHolder
public class FooterViewHolder extends RecyclerView.ViewHolder {
public FooterViewHolder(View itemView) {
super(itemView);
}
}
}
</string></string></recyclerview.viewholder>
|