public class MyAdapter extends RecyclerView.Adapterimplements View.OnLongClickListener, View.OnClickListener
{//数据库
private ListmList;
public MyAdapter(ListmList) {
this.mList = mList;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy_item, parent, false);
Holder holder = new Holder(view);
view.setOnClickListener(this);
view.setOnLongClickListener(this);
return holder;
}
@Override
public void onBindViewHolder(Holder holder, int position) {
holder.mText.setText(mList.get(position).getSource() + "");
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return mList.isEmpty() ? 0 : mList.size();
}
class Holder extends RecyclerView.ViewHolder {
private TextView mText;
public Holder(View itemView) {
super(itemView);
mText = (TextView) itemView.findViewById(R.id.Recy_Text);
}
}
//这是短按点击事件 短按做Toast
public interface OnItemClick {
void setOnItemClick(View v, int position);
}
//这是长按点击事件 长按添加到数据库
public interface OnLongItemClick {
void setOnLongItemClick(View v, int position);
}
private OnItemClick onItemClick;
private OnLongItemClick onLongItemClick;
@Override
public void onClick(View v) {
if (onItemClick != null) {
onItemClick.setOnItemClick(v, (int) v.getTag());
}
}
@Override
public boolean onLongClick(View v) {
if (onLongItemClick != null) {
onLongItemClick.setOnLongItemClick(v, (int) v.getTag());
}
return true;
j }
public void setOnItem(OnItemClick onItem) {
this.onItemClick = onItem;
}
public void setOnLongItem(OnLongItemClick onLongItem) {
this.onLongItemClick = onLongItem;
}