我在CardView
RecyclerView
上显示了联系人列表,现在我想在特定OnLongClick
上调用CardView
事件时打开上下文菜单。
喜欢编辑,删除选项。
在CardView
布局中,我添加了两个布局,左侧联系人显示,右侧两个按钮添加了不同事件处理程序处理的历史记录和状态。
如果我长按CardView
一个弹出窗口打开,我可以编辑和删除联系人,并从这些选项中删除或编辑点击事件将处理特定联系人。
ContactAdapter
代码:
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;
}
}
}
答案 0 :(得分: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 );
}
使用带有按钮的弹出窗口定义showDialog函数,以完成上述伪代码。