我希望我的弹出菜单显示在点击的行旁边。点击“...”按钮后查看图片: http://postimg.org/image/s35znsaq3/
我添加了初始化每行的适配器。当我点击moreActionsButton时,监听器调用打开菜单的方法showPopUpMenu。菜单里面我会做一些工作人员。
这是弹出菜单的代码:
public class ContactsListActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set layout for this activity
setContentView(R.layout.contacts_list);
// Connect between buttons to layout id
list = (ListView) findViewById(R.id.my_user_list);
spinner = (ProgressBar) findViewById(R.id.spinner);
// Setting adapter and creating contacts list
if (selectedGroupId != null) {
Model.getInstance().getAllUsersByGroupId(selectedGroupId, new Model.userListReturnedListener() {
@Override
public void userListReturned(List<User> userList) {
spinner.setVisibility(View.GONE);
data = userList;
adapter = new MyAdapter();
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
class MyAdapter extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
// Creates a ViewHolder and store references to layouts we want to bind data to.
holder = new ViewHolder();
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.contacts_row, null);
// Connect between buttons to layout id
holder.contactName = (TextView) convertView.findViewById(R.id.contactName);
holder.contactImage = (ImageView) convertView.findViewById(R.id.contactImage);
holder.moreActionsButton = (Button) convertView.findViewById(R.id.moreActions);
//Save holder
convertView.setTag(holder);
} else {
// Get the ViewHolder back
holder = (ViewHolder) convertView.getTag();
}
// Setting user
final User us = data.get(position);
holder.contactName.setText(us.getFname() + " " + us.getLname());
holder.moreActionsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopUpMenu(us);
}
});
// Setting the popUpMenu buttons
popUpMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_skype:
AppsCommunication.onSkypeClick(ContactsListActivity.this, user.getSkype());
break;
case R.id.1:
//Do something
break;
case R.id.2:
//Do something
break;
case R.id.3:
//Do something
default:
break;
}
return true;
}
});
Object menuHelper;
Class[] argTypes;
try {
Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
fMenuHelper.setAccessible(true);
menuHelper = fMenuHelper.get(popUpMenu);
argTypes = new Class[]{boolean.class};
menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
} catch (Exception e) {
// Possible exceptions are NoSuchMethodError and NoSuchFieldError
//
// In either case, an exception indicates something is wrong with the reflection code, or the
// structure of the PopupMenu class or its dependencies has changed.
//
// These exceptions should never happen since we're shipping the AppCompat library in our own apk,
// but in the case that they do, we simply can't force icons to display, so log the error and
// show the menu normally.
}
popUpMenu.show();
}
// Class to handle row buttons
class ViewHolder {
private TextView contactName;
private ImageView contactImage;
private ImageButton saveContactButton;
private ImageButton callButton;
private ImageButton smsButton;
private Button moreActionsButton;
private ProgressBar contactImageProgressbar;
}
}