studio recyclerview onitemtouchlistener new item example event activity java android android-recyclerview onclicklistener

java - onitemtouchlistener - recyclerview onitemclicklistener example in android



onClickListener en RecyclerView.ViewHolder (3)

Este es el ejemplo completo de Adaptador personalizado en el que puedo obtener detalles de artículos en particular. En MainActivity necesitas configurar el adaptador:

adapter = new MyAdapter(getApplicationContext(), account_no, title, aFN1, aLN1,aFN2, aLN2,aFN3, aLN3,isavilable,waitlist,flag); adapter.notifyDataSetChanged(); mRecyclerView.setAdapter(adapter);

Ahora vea el código para el Adaptador personalizado:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { String TAG = "MyAdapter"; Context context; private String[] accountNo; private String[] title; private String[] afn1; private String[] aln1; private String[] afn2; private String[] aln2; private String[] afn3; private String[] aln3; private String[] isAvailable; private String[] waitlist; private int flag; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView account_no,title,afn1,afn2,aln1,aln2,color,is_available; private ImageView img_android; CardView cardView; public ViewHolder(CardView v) { super(v); account_no = (TextView)v.findViewById(R.id.acctno); title = (TextView)v.findViewById(R.id.title); afn1 = (TextView) v.findViewById(R.id.afn1); cardView = v; } } // Provide a suitable constructor (depends on the kind of dataset) public MyAdapter(Context context, String[] accountNo,String[] title,String[] afn1,String[] aln1,String[] afn2,String[] aln2,String[] afn3,String[] aln3,String[] isAvailable, String[] waitlist,int flag) { this.context = context; this.accountNo = accountNo; this.title = title; this.afn1 = afn1; this.aln1 = aln1; this.afn2 = afn2; this.aln2 = aln2; this.afn3 = afn3; this.aln3 = aln3; this.isAvailable = isAvailable; this.waitlist = waitlist; this.flag = flag; Log.d(TAG,afn1.toString() +aln1+afn2+aln2+afn3+aln3.toString()+waitlist); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) { CardView cv = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); return new ViewHolder(cv); } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { final CardView cardView = holder.cardView; final TextView accountno = (TextView)cardView.findViewById(R.id.acctno); accountno.setText(accountNo[position]); final TextView titletxt = (TextView)cardView.findViewById(R.id.title); titletxt.setText(title[position]); final TextView afn1txt = (TextView) cardView.findViewById(R.id.afn1); afn1txt.setText(afn1[position]+" "+aln1[position]); Log.d(TAG,waitlist[position]); cardView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int id = getItemViewType(position); SharedPreferences sharedPreferences = context.getSharedPreferences(Constant.MYPREFERENCE,Context.MODE_PRIVATE); SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putString(Constant.ACCOUNT,accountNo[position]); edit.putString(Constant.TITLE,title[position]); edit.putString(Constant.AFN1,afn1[position]); edit.putString(Constant.ALN1,aln1[position]); edit.putString(Constant.AFN2,afn2[position]); edit.putString(Constant.ALN2,aln2[position]); edit.putString(Constant.AFN3,afn3[position]); edit.putString(Constant.ALN3,aln3[position]); edit.putBoolean(Constant.IS_AVAILABLE, Boolean.parseBoolean(isAvailable[position].toUpperCase())); edit.putString(Constant.WAITLIST,waitlist[position]); Log.d("WaitingNo2 :",""+String.valueOf(waitlist[position])); edit.commit(); Intent intent = new Intent(v.getContext(), DetailsActivity.class); v.getContext().startActivity(intent); } }); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return accountNo.length; } }

Después de Todo lo que obtiene el valor de sharedPreferences en la Actividad que se llama en Card Click:

preferences = getSharedPreferences("myshared", Context.MODE_PRIVATE); AccountNo = sharedPreferences.getString(Constant.ACCOUNT,null).toUpperCase(); title = sharedPreferences.getString(Constant.TITLE,null).toUpperCase(); afn1 = sharedPreferences.getString(Constant.AFN1,null).toUpperCase(); aln1 = sharedPreferences.getString(Constant.ALN1,null).toUpperCase(); afn2 = sharedPreferences.getString(Constant.AFN2,null).toUpperCase(); aln2 = sharedPreferences.getString(Constant.ALN2,null).toUpperCase(); afn3 = sharedPreferences.getString(Constant.AFN3,null).toUpperCase(); aln3 = sharedPreferences.getString(Constant.ALN3,null).toUpperCase(); isAvailable = sharedPreferences.getBoolean(Constant.IS_AVAILABLE,isAvailable); waitlist = sharedPreferences.getString(Constant.WAITLIST,waitlist);

Ahora haz lo que quieras y disfruta el código.

Tengo un RecyclerView.ViewHolder y RecyclerView.Adapter, lo necesito después de hacer clic en el elemento y luego enviar información sobre este elemento a otra Activity .

PlacesAdapter.java

public class PlacesAdapter extends RecyclerView.Adapter<PlacesViewHolder> { private PlacesActivity placesActivity; Context context; private int position; List<Places> places; public PlacesAdapter(List<Places> places) { this.places = places;} @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @Override public PlacesViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.places_view, viewGroup, false); PlacesViewHolder pvh = new PlacesViewHolder(v); return pvh; } @Override public void onBindViewHolder(PlacesViewHolder personViewHolder, int i) { personViewHolder.name.setText(places.get(i).name); personViewHolder.address.setText(places.get(i).address); Picasso.with(personViewHolder.itemView.getContext()) .load(places.get(i).photo) .into(personViewHolder.getPhoto()); } @Override public int getItemCount() { return places.size(); } }

PlacesViewHolder.java

En esta línea "intent.putExtra(PlacesDetail.PLACES_NAME,);" ¿Cómo puedo enviar un nombre?

public class PlacesViewHolder extends RecyclerView.ViewHolder { CardView cv; public TextView name; public TextView address; public ImageView photo; public PlacesViewHolder(final View itemView) { super(itemView); cv = (CardView)itemView.findViewById(R.id.cv); name = (TextView)itemView.findViewById(R.id.person_name); address = (TextView)itemView.findViewById(R.id.person_age); photo = (ImageView)itemView.findViewById(R.id.person_photo); itemView.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Context context = v.getContext(); Intent intent = new Intent(context, PlacesDetail.class); intent.putExtra(PlacesDetail.PLACES_NAME,); context.startActivity(intent); } }); } public TextView getAddress() { return address; } public TextView getName() { return name; } public ImageView getPhoto() { return photo; } }


Puede lograrlo creando una interfaz dentro de su adaptador para un itemclicklistener y luego puede configurar onItemClickListener desde su PlacesActivity .

En algún lugar dentro de su PlacesAdapter necesitaría lo siguiente:

private onRecyclerViewItemClickListener mItemClickListener; public void setOnItemClickListener(onRecyclerViewItemClickListener mItemClickListener) { this.mItemClickListener = mItemClickListener; } public interface onRecyclerViewItemClickListener { void onItemClickListener(View view, int position, String places_name); }

Luego, dentro de su ViewHolder (que he agregado como clase interna dentro de mi adaptador), debe aplicar el oyente a los componentes en los que desea que el usuario haga clic, así:

class PlacesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { PlacesViewHolder(View view) { super(view); view.setOnClickListener(this); } @Override public void onClick(View v) { if (mItemClickListener != null) { mItemClickListener.onItemClickListener(v, getAdapterPosition(), PlacesDetail.PLACES_NAME); } } }

Este ejemplo muestra un onClickListener aplicado a la vista dentro de PlacesViewHolder .

recyclerView.setAdapter(adapter);// set adapter on recyclerview adapter.notifyDataSetChanged();// Notify the adapter adapter.setOnItemClickListener(new PlacesAdapter.onRecyclerViewItemClickListener() { @Override public void onItemClickListener(View view, int position, String places_name) { //perform click logic here (places_name is passed) } });

Para implementar este código, debe setOnItemClickListener a su adaptador dentro de PlacesActivity como se muestra arriba.


prueba esto, es correcto para mí

Crea una nueva clase y este código

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { private OnItemClickListener mListener; public interface OnItemClickListener { public void onItemClick(View view, int position); public void onLongItemClick(View view, int position); } GestureDetector mGestureDetector; public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) { mListener = listener; mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { return true; } @Override public void onLongPress(MotionEvent e) { View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); if (child != null && mListener != null) { mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child)); } } }); } @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) { View childView = view.findChildViewUnder(e.getX(), e.getY()); if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { mListener.onItemClick(childView, view.getChildAdapterPosition(childView)); return true; } return false; } @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } @Override public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){} }

Y en su Actividad, agregue esto a la implementación de su adaptador

mRecyclerView.addOnItemTouchListener( new RecyclerItemClickListener(getActivity(), mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { // do something } } } @Override public void onLongItemClick(View view, int position) { // do whatever } }) );