为RecyclerView添加点击事件、长按事件

时间:2025-04-02 10:18:21
public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener, View.OnLongClickListener { public static final int TYPE_HEAD = 0; public static final int TYPE_APPS = 1; // 这里数据源有两个(系统应用列表和用户应用列表) private ArrayList<AppInfo> mUserList; // 所有已安装用户应用的集合 private ArrayList<AppInfo> mSystemList; // 所有已安装系统应用的集合 public AppAdapter(ArrayList<AppInfo> userList, ArrayList<AppInfo> systemList) { mUserList = userList; mSystemList = systemList; } @Override public int getItemViewType(int position) { if (position == 0 || position == mUserList.size() + 1) { return TYPE_HEAD; } else { return TYPE_APPS; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = null; RecyclerView.ViewHolder holder = null; switch (viewType) { case TYPE_HEAD: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_header, null); holder = new HeadHolder(view); break; case TYPE_APPS: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_appinfo, null); /** * 为展示应用信息的布局添加点击和长按事件监听 */ view.setOnClickListener(this); view.setOnLongClickListener(this); holder = new AppsHolder(view); break; } return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { switch (getItemViewType(position)) { case TYPE_HEAD: if (0 == position) { ((HeadHolder) holder).tvHead.setText("用户应用(" +mUserList.size() + ")"); } else { ((HeadHolder) holder).tvHead.setText("系统应用(" + mSystemList.size() + ")"); } break; case TYPE_APPS: AppInfo info; if (position < mUserList.size() + 1) { info = mUserList.get(position - 1); // 从用户应用列表中获取应用信息 } else { info = mSystemList.get(position - mUserList.size() - 2); // 从系统应用列表中获取应用信息 } // 设置控件内容 ((AppsHolder) holder).tvName.setText(info.name); ((AppsHolder) holder).ivIcon.setImageDrawable(info.icon); if (info.isRom) { ((AppsHolder) holder).tvLocation.setText("内置存储卡"); } else { ((AppsHolder) holder).tvLocation.setText("外部存储卡"); } /** * 将position保存在itemView的Tag中以便点击时获取 */ holder.itemView.setTag(position); break; } } @Override public int getItemCount() { return mUserList.size() + mSystemList.size() + 2; // 加上两条头布局条目 } /**************************************** * Holder */ class HeadHolder extends RecyclerView.ViewHolder { @BindView(R.id.tv_head) TextView tvHead; public HeadHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } class AppsHolder extends RecyclerView.ViewHolder { @BindView(R.id.tv_name) TextView tvName; @BindView(R.id.iv_icon) ImageView ivIcon; @BindView(R.id.tv_location) TextView tvLocation; public AppsHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } /**************************************** * Listener */ /** * 手动添加点击事件 */ interface OnClickListener { void onClick(View view, int position); } private OnClickListener mOnClickListener = null; public void setOnClickListener(OnClickListener listener) { mOnClickListener = listener; } @Override public void onClick(View view) { if (null != mOnClickListener) { mOnClickListener.onClick(view, (int) view.getTag()); } } /** * 手动添加长按事件 */ interface OnLongClickListener { void onLongClick(View view, int position); } private OnLongClickListener mOnLongClickListener = null; public void setOnLongClickListener(OnLongClickListener listener) { mOnLongClickListener = listener; } @Override public boolean onLongClick(View view) { if (null != mOnLongClickListener) { mOnLongClickListener.onLongClick(view, (int) view.getTag()); } // 消耗事件,否则长按逻辑执行完成后还会进入点击事件的逻辑处理 return true; } }