You can achieve this by creating an interface inside
your adapter for an itemclicklistener and then you can
set onItemClickListener from your MainActivity. Somewhere inside your RecyclerViewAdapter you would
need the following: private onRecyclerViewItemClickListener mItemClickListener; public void setOnItemClickListener(onRecyclerViewItemClickListener mItemClickListener) { this.mItemClickListener = mItemClickListener; } public interface onRecyclerViewItemClickListener { void onItemClickListener(View view, int position); } Then inside your ViewHolder (which I've added as an inner class inside my adapter),
you would apply the listener to the components you'd like the user to click, like so:
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public ImageView imageview; RecyclerViewHolder(View view) { super(view); this.imageview = (ImageView) view .findViewById(R.id.image); imageview.setOnClickListener(this); } @Override public void onClick(View v) { if (mItemClickListener != null) { mItemClickListener.onItemClickListener(v, getAdapterPosition()); } } } This example shows an onClickListener being applied to the image inside a ViewHolder. To implement this code, you would setOnItemClickListener to your adapter
inside MainActivity as shown above. recyclerView.setAdapter(adapter);// set adapter on recyclerview
adapter.notifyDataSetChanged();// Notify the adapter adapter.setOnItemClickListener(new RecyclerViewAdapter.onRecyclerViewItemClickListener() { @Override public void onItemClickListener(View view, int position) { Switch (view.getId()) { case R.id.letter: //logic here break; case R.id.firstname:
//logic here break; .... } } });
No comments:
Post a Comment