如何为列表视图中的每个列表项添加三点菜单?

我想为列表视图的每个列表项添加三点菜单。


**这是我的listview适配器getView方法**


 @Override

public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = activity.getLayoutInflater();

    View inflate = inflater.inflate(R.layout.book_item_new, null, false);

    findViews(inflate);


    bookTitle.setText(books.get(position).getName());

    bookPrice.setText(books.get(position).getPrice() + " ₹");

    semBranch.setText(books.get(position).getSemester() + " Sem " + books.get(position).getBranch());

    date.setText(books.get(position).getpDate());

    senderName.setText(books.get(position).getSender());


    return inflate;

}


暮色呼如
浏览 109回答 3
3回答

拉莫斯之舞

在布局中添加ImageView带有三点图像的book_item_new。<ImageView&nbsp; &nbsp; android:id="@+id/imbPopUp"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_alignParentEnd="true"&nbsp; &nbsp; android:layout_centerVertical="true"&nbsp; &nbsp; android:paddingLeft="18dp"&nbsp; &nbsp; android:paddingRight="18dp"&nbsp; &nbsp; android:src="@drawable/icon_menu_dot" />&nbsp;并在您的内部OnclickListener设置如下所示。imageviewgetView()imageView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showPopupMenu(imageView, position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Showing popup menu when tapping on 3 dots&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private void showPopupMenu(View view, int position) {&nbsp; &nbsp; &nbsp; &nbsp; PopupMenu popup = new PopupMenu(context, view, Gravity.END);&nbsp; &nbsp; &nbsp; &nbsp; MenuInflater inflater = popup.getMenuInflater();&nbsp; &nbsp; &nbsp; &nbsp; inflater.inflate(R.menu.favourite_menu, popup.getMenu());&nbsp; &nbsp; &nbsp; &nbsp; //set menu item click listener here&nbsp; &nbsp; &nbsp; &nbsp; popup.setOnMenuItemClickListener(new MyMenuItemClickListener(position));&nbsp; &nbsp; &nbsp; &nbsp; popup.show();&nbsp; &nbsp; }现在为您的菜单项单击创建单击侦听器类,如下所示。/**&nbsp; &nbsp; &nbsp;* Click listener for popup menu items&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {&nbsp; &nbsp; &nbsp; &nbsp; int position;&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param position&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; MyMenuItemClickListener(int position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.position = position;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Click listener for popup menu items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onMenuItemClick(MenuItem menuItem) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (menuItem.getItemId()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.view:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.edit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.delete:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.favourite:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

明月笑刀无情

你可以尝试这个,在你的设计xml中添加以下点代码<Textview&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/textViewOptions"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="0dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_alignParentRight="true"&nbsp; &nbsp; &nbsp; &nbsp; android:text="&#8942;"&nbsp; &nbsp; &nbsp; &nbsp; android:textAppearance="?android:textAppearanceLarge"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center_horizontal|center_vertical"/>之后在您的 on bind 方法中添加以下代码,holder.textViewOptions.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //creating a popup menu&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PopupMenu popup = new PopupMenu(context, holder.textViewOptions);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //inflating menu from xml resource&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; popup.inflate(R.menu.menu_options);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //adding click listener&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onMenuItemClick(MenuItem item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (item.getItemId()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.action_delete:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //handle menu1 click&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.action_edit:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //handle menu2 click&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; popup.show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });

白衣非少年

只需将按钮添加到列表项中并将 onClickListener 设置为按钮即可。像这样&nbsp; button.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; ContextThemeWrapper wrapper =&nbsp; &nbsp; &nbsp; new ContextThemeWrapper();&nbsp; &nbsp; &nbsp; PopupMenu popupMenu = new PopupMenu(wrapper, 'yourItemView');&nbsp; &nbsp; &nbsp; popupMenu.inflate(R.menu.'your_menu');&nbsp; &nbsp; &nbsp; popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onMenuItemClick(MenuItem item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // implement menu click here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; popupMenu.show();&nbsp; &nbsp; }&nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java