3 回答
TA贡献1820条经验 获得超10个赞
在布局中添加ImageView带有三点图像的book_item_new。
<ImageView
android:id="@+id/imbPopUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:src="@drawable/icon_menu_dot" />
并在您的内部OnclickListener设置如下所示。imageviewgetView()
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupMenu(imageView, position);
}
});
/**
* Showing popup menu when tapping on 3 dots
*/
private void showPopupMenu(View view, int position) {
PopupMenu popup = new PopupMenu(context, view, Gravity.END);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.favourite_menu, popup.getMenu());
//set menu item click listener here
popup.setOnMenuItemClickListener(new MyMenuItemClickListener(position));
popup.show();
}
现在为您的菜单项单击创建单击侦听器类,如下所示。
/**
* Click listener for popup menu items
*/
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
int position;
/**
* @param position
*/
MyMenuItemClickListener(int position) {
this.position = position;
}
/**
* Click listener for popup menu items
*/
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.view:
// ...
return true;
case R.id.edit:
// ...
return true;
case R.id.delete:
// ...
return true;
case R.id.favourite:
// ...
return true;
default:
}
return false;
}
}
TA贡献1828条经验 获得超4个赞
你可以尝试这个,在你的设计xml中添加以下点代码
<Textview
android:id="@+id/textViewOptions"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="⋮"
android:textAppearance="?android:textAppearanceLarge"
android:gravity="center_horizontal|center_vertical"/>
之后在您的 on bind 方法中添加以下代码,
holder.textViewOptions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(context, holder.textViewOptions);
//inflating menu from xml resource
popup.inflate(R.menu.menu_options);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_delete:
//handle menu1 click
return true;
case R.id.action_edit:
//handle menu2 click
return true;
default:
return false;
}
}
});
popup.show();
}
});
TA贡献1155条经验 获得超0个赞
只需将按钮添加到列表项中并将 onClickListener 设置为按钮即可。
像这样
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContextThemeWrapper wrapper =
new ContextThemeWrapper();
PopupMenu popupMenu = new PopupMenu(wrapper, 'yourItemView');
popupMenu.inflate(R.menu.'your_menu');
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// implement menu click here
return true
}
});
popupMenu.show();
}
});
添加回答
举报