0

J'ai montré une liste de contacts dans CardView sur RecyclerView et maintenant je veux ouvrir un menu contextuel lorsque j'appelle OnLongClick événement sur CardView notamment.Comment ouvrir le menu contextuel après l'événement onLongClick sur une liste CardView android?

Comme éditer, supprimez les options. Mise en page J'ai ajouté deux mises en page, le côté gauche du contact s'affiche et deux boutons de droite ajoutent l'historique et l'état où les différents gestionnaires d'événements gèrent. Si j'appuie longuement sur CardView une fenêtre pop-up ouverte où je peux éditer et supprimer le contact et de ces options comme - supprimer ou éditer l'événement de clic traitera pour un contact particulier.

ContactAdapter Code:

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> { 
ArrayList<Contact> contacts = new ArrayList<Contact>(); 
Context ctx; 

public ContactAdapter(ArrayList<Contact> contacts,Context ctx) { 
    this.contacts = contacts; 
    this.ctx = ctx; 
} 

@Override 
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout, parent, false); 
    ContactViewHolder contactViewHolder = new ContactViewHolder(view,ctx,contacts); 
    return contactViewHolder; 
} 

@Override 
public void onBindViewHolder(ContactViewHolder holder, int position) { 
    Contact CON = contacts.get(position); 
    holder.person_name.setText(CON.getName()); 
    holder.person_email.setText(CON.getEmail()); 
} 

@Override 
public int getItemCount() { 
    return contacts.size(); 
} 

public static class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { 
    TextView person_name, person_email,btn_history,btn_task_update; 
    ArrayList<Contact> contacts = new ArrayList<Contact>(); 
    Context ctx; 
    CardView card_view; 

    public ContactViewHolder(View view, Context ctx ,ArrayList<Contact> contacts) { 
     super(view); 
     this.contacts=contacts; 
     this.ctx = ctx; 
     person_name = (TextView) view.findViewById(R.id.person_name); 
     person_email = (TextView) view.findViewById(R.id.person_email); 
     card_view = (CardView) view.findViewById(R.id.card_view); 
     btn_history = (TextView) view.findViewById(R.id.btn_history); 
     btn_task_update= (TextView) view.findViewById(R.id.btn_task_update); 
     card_view.setOnClickListener(this); 
     card_view.setOnLongClickListener(this); 
     btn_history.setOnClickListener(this); 
     btn_task_update.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     int position = getAdapterPosition(); 
     Contact contact = this.contacts.get(position); 
     if(v.getId()==R.id.card_view) { 
      Toast.makeText(this.ctx, "position Card View" + position, Toast.LENGTH_SHORT).show(); 
     } 
     else if(v.getId()==R.id.btn_history) 
     { 
      Toast.makeText(this.ctx, "position History" + position, Toast.LENGTH_SHORT).show(); 
     } 
     else if(v.getId()==R.id.btn_task_update) 
     { 
      Toast.makeText(this.ctx, "position Task Status" + position, Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(this.ctx, task_detail.class); 
      intent.putExtra("name", contact.getName()); 
      intent.putExtra("email", contact.getEmail()); 
      this.ctx.startActivity(intent); 
     } 
    } 

    @Override 
    public boolean onLongClick(View v) { 
     int position = getAdapterPosition(); 
     Contact contact = this.contacts.get(position); 
     Toast.makeText(this.ctx, "position " + position, Toast.LENGTH_SHORT).show(); 
     return true; 
    } 
} 
} 

Répondre

0
 @Override 
     public boolean onLongClick(View v) { 
      if(v.getId() == R.id.card_view) { 
       int position = getAdapterPosition(); 
       Contact contact = this.contacts.get(position); 
       showDialog(contact, new CustomActionListener() { 
        void onAction(int actionType) { 
        switch(actionType) { 
         case OPTION_EDIT : 
         //edit contact here 
         break; 
         case OPTION_DELETE : 
         //delete contact here 
         break; 
         default : 
         //On dialog dismiss - do nothing 
         break; 
        } 
        } 
       }); 
       /* show your popup dialog here, it would be good 
       if you have an interface defined for the kind of action that was performed in dialog, using that, 
       perform required action here. */ 
      } 

     return true; 
    } 

     // Your custom interface 
     public interface CustomActionListener { 
      void onAction(int actionType); 
     } 

Définir une fonction showDialog avec fenêtre avec des boutons pour compléter le code de pseudo ci-dessus.