我正在尝试将菜单添加到列表视图中。当用户长按一下列表项时,我希望该菜单填充(实际上我正在显示用户想要购买的项目列表,菜单只显示“删除”)...这就是我的方式在显示列表时,如何为长按一下添加监听器?
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;
}
}
答案 0 :(得分:0)
您可以使用ListView的setOnItemLongClickListener()
执行此操作。像这样:
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
// Show menu for item in position.
}
});
在onViewCreated()
结尾处是执行此代码的适当时间,即
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
getListView().setOnItemLongClickListener(...);
}
答案 1 :(得分:0)
有长按上下文菜单的教程:http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/
有一个类似的问题:creating a menu after a long click event on a list view
以下是针对liistview的任何行的长按操作的代码
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
});