gogoWebsite

Add click event and long press event to RecyclerView

Updated to 20 days ago
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; // There are two data sources here (system application list and user application list) private ArrayList<AppInfo> mUserList; // Collection of all installed user applications private ArrayList<AppInfo> mSystemList; // Collection of all installed system applications 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); /** * Add click and long press event listening to the layout that displays application information */ 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("User Application(" +mUserList.size() + ")"); } else { ((HeadHolder) holder).tvHead.setText("System Application(" + mSystemList.size() + ")"); } break; case TYPE_APPS: AppInfo info; if (position < mUserList.size() + 1) { info = mUserList.get(position - 1); // Get application information from user application list } else { info = mSystemList.get(position - mUserList.size() - 2); // Get application information from the system application list } // Set the control content ((AppsHolder) holder).tvName.setText(info.name); ((AppsHolder) holder).ivIcon.setImageDrawable(info.icon); if (info.isRom) { ((AppsHolder) holder).tvLocation.setText("Built-in memory card"); } else { ((AppsHolder) holder).tvLocation.setText("External memory card"); } /** * Save position in the itemView's Tag for getting when clicked */ holder.itemView.setTag(position); break; } } @Override public int getItemCount() { return mUserList.size() + mSystemList.size() + 2; // Add two header layout entries } /**************************************** * 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 */ /** * Add click events manually */ 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()); } } /** * Add a long press event manually */ 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()); } // Consumption of events, otherwise, after long pressing the logic to execute, the logical processing of the click event will be entered. return true; } }