[英]How to add menu to Android List item via long click
I'm attempting to add a menu to a list view. 我试图将菜单添加到列表视图。 When the user long click's on a list item I want to have that menu populate (really I'm showing a list of items the user wants to buy, and the menu will only display "Remove")... This is how i'm displaying the list, how would I add the the listenener for the long click? 当用户长按列表项时,我要填充该菜单(确实是在显示用户要购买的项的列表,并且菜单将仅显示“删除”)...这就是我的操作方式在显示列表时,如何为长按添加监听器?
public static class CheckoutFragment extends ListFragment {
/**
*
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public List<Entry> cart;
JSONObject json;
jsonParser jParser = new jsonParser();
JSONArray directory;
/**
* Returns a new instance of this fragment for the given section number.
*/
public static Fragment newInstance(int sectionNumber) {
CheckoutFragment fragment = new CheckoutFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public CheckoutFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
cart = Dashboard.datasource.getAllProducts();
View v = inflater.inflate(R.layout.fragment_checkout, container,
false);
Button order = (Button) v.findViewById(R.id.order);
order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Order Submitted",
Toast.LENGTH_SHORT).show();
submitOrder();
flag = false;
}
});
CheckoutArrayAdapter checkoutAdapter = new CheckoutArrayAdapter(
container.getContext(), R.layout.fragment_checkout,
R.layout.checkout_list_item, cart);
setListAdapter(checkoutAdapter);
Dashboard.datasource = DisplayProduct.datasource;
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
return false;
// Show menu for item in position.
}
});
return v;
}
protected void submitOrder() {
OrderTask order = new OrderTask();
order.execute();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((Dashboard) activity).onSectionAttached(getArguments().getInt(
ARG_SECTION_NUMBER));
}
public class CheckoutArrayAdapter extends ArrayAdapter<Entry> {
private final LayoutInflater mInflater;
private List<Entry> cart;
public CheckoutArrayAdapter(Context context, int listViewLayout,
int listItemLayout, List<Entry> cartItems) {
super(context, listViewLayout, listItemLayout, cartItems);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cart = cartItems;
}
/**
* Populate new items in the list.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(R.layout.checkout_list_item,
parent, false);
} else {
view = convertView;
}
Entry item = getItem(position);
((TextView) view.findViewById(R.id.cart_entry)).setText(item
.getQty()
+ "x "
+ item.getName()
+ "\t"
+ item.getSku() + " ");
return view;
}
}
You can do this with the ListView's setOnItemLongClickListener()
. 您可以使用ListView的setOnItemLongClickListener()
。 Like this: 像这样:
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
// Show menu for item in position.
}
});
At the end of onViewCreated()
would be the appropriate time to execute this code, ie 在onViewCreated()
的结尾将是执行此代码的适当时间,即
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
getListView().setOnItemLongClickListener(...);
}
There is a tutorial for Long Press Context Menu: http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/ 有一个针对长按上下文菜单的教程: http : //www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/
There is a similar question: creating a menu after a long click event on a list view 有一个类似的问题: 在列表视图中长按事件后创建菜单
Here is a code for longpress action for any row of your liistview 这是liistview的任何行的长按动作代码
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.