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 RecyclerViewAdapter(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;
}
// 点击事件接口
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);
}
}
}
</string></string></recyclerviewadapter.viewholder>
|