上下文菜单中列表视图的压力很小

时间:2014-11-10 10:24:50

标签: android listview

但是,我有一个问题。如何在listview或textview中表示短压的上下文菜单?这是我的源代码,所有写的。但对我来说,长期压力超出了上下文菜单。

public class Day extends ListFragment  { 
    @Override 
     public void onCreate(Bundle savedInstanceState) {       
      super.onActivityCreated(savedInstanceState); 

     DayAdapter myListAdapter = new DayAdapter(getActivity(), R.layout.text_fragment, names); 
      setListAdapter(myListAdapter); 
    } 

    @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) {       
       View rootView =inflater.inflate(R.layout.listview, container, false); 
       ListView lv = (ListView)rootView.findViewById(android.R.id.list); 
       lv.setOnItemClickListener(new OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
                int position, long id) { 
            registerForContextMenu(view); 
                        } 

       public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { 
            menu.setHeaderTitle("Hello"); 
            menu.setHeaderIcon(R.drawable.up); 
            menu.add(0, 0, 0, "Item One"); 
            menu.add(0, 1, 0, "Item Two"); 
        } 
    }); 
   return rootView; } } 

1 个答案:

答案 0 :(得分:0)

您可以在onItemClick方法中使用openContextMenu()

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    registerForContextMenu(view);
    openContextMenu(view);
}

请参阅: http://developer.android.com/reference/android/app/Activity.html#openContextMenu(android.view.View)