我正在列表视图中使用上下文菜单处理列表视图中的每个项目;但是,单击某个项目时,上下文菜单不会显示。我在菜单目录下创建了一个list_menu.xml文件,并且我使用了自定义适配器。自定义适配器xml文件由图像和textview组成。最后,我想在点击图片时显示菜单,但是现在,我想在点击列表项时显示菜单。但没有任何反应。如何显示上下文菜单?谢谢 main xml
package org.pctechtips.menulistview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList hosts;
String ipAdd = "192.168.10.";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hosts = new ArrayList<String>();
for(int i = 0; i < 255; i++) {
hosts.add(ipAdd+i);
}
ListView list = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.ip_address, hosts);
list.setAdapter(adapter);
registerForContextMenu(list);
}
/*
* Infating the menu for toolbar
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.list_menu, menu);
}
}
适配器
package org.pctechtips.menulistview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import static org.pctechtips.menulistview.R.drawable.computer_48_dp;
/**
* Created by George on 8/8/2017.
*/
public class ListAdapter extends ArrayAdapter<String>{
public ListAdapter(Context context, int num, ArrayList<String> hosts) {
super(context, 0, hosts);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
ImageView iconImgView = (ImageView) convertView.findViewById(R.id.host_icon);
TextView ipAddTxtView = (TextView) convertView.findViewById(R.id.ip_address);
iconImgView.setImageResource(R.drawable.computer_48_dp);
ipAddTxtView.setText(item);
return convertView;
}
}
list_item xml文件(适配器)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/host_icon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="7dp"
android:src="@drawable/computer_48_dp"/>
<TextView
android:id="@+id/ip_address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:textSize="18dp"
tools:text="192.168.10.100"
/>
</LinearLayout>
</LinearLayout>
list_menu.xml文件
<!-- listview menu for host scan options eg:
change hostname, icon, notification etc -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/change_icon"
android:title="change icon"
/>
<item android:id="@+id/set_hostname"
android:title="set hostname"
/>
<itme android:id="@+id/notifications"
android:title="notifications"
/>
</menu>
答案 0 :(得分:2)
上下文菜单可以长按列表项而不是短按。如果您希望它能够在短按工具onListItemClick
方法上工作,如下所示
public void onListItemClick(ListView l, View v, int position, long id){
list.showContextMenuForChild(v);
}
如果你想在某个其他元素上打开相同的元素,比如按钮,你可以这样做 -
public void onButtonClickEvent(View sender)
{
registerForContextMenu(sender);
openContextMenu(sender);
unregisterForContextMenu(sender);
}
否则您也可以使用PopupMenu
在listview项目中添加布局为 -
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_overflow_holo_dark"
android:contentDescription="@string/descr_overflow_button"
android:onClick="showPopup" />
然后实现showPopup方法,如下所示 -
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.actions, popup.getMenu());
popup.show();
}
答案 1 :(得分:0)
使用PopupMenu
用于项目点击,长按使用ContextMenu
,
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, v);
//Inflating popup menu
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//add OnMenuItemClickListener to popup
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();