2011-09-14 15 views
6

मेरे पास एक सूचीफलक गतिविधि है।ListFragment में LongPress को ओवरराइड कैसे करें?

मैं onItemClickedLongPress के लिए एक विधि बनाना चाहता हूं, ताकि जब उपयोगकर्ता ऐसा करे। एक मेनू पॉप अप करता है। मैं मेनू बनाने से परिचित हूं।

तो अगर कोई कृपया खुश करेगा, तो सूची सूची में लम्बाई को ओवरराइड करने के तरीके पर मुझे और निर्देश दें?

उत्तर

8

संपादित करें: यह नमूना दिखाता है कि सिस्टम मेनू fx के बाद कुछ और कैसे दिखाना है। से https://github.com/lorensiuswlt/NewQuickAction

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    //....... 
    registerForContextMenu(getListView()); 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    AdapterView.AdapterContextMenuInfo amenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo; 
    Object item = getListAdapter().getItem(amenuInfo.position); 
    //item could be Cursor/String/YourObject it depends on Adapter 
    //show popup fx. QuickAction from https://github.com/lorensiuswlt/NewQuickAction 
    QuickAction qa = new QuickAction(getActivity()); 
    qa.setAnimStyle(QuickAction.ANIM_AUTO); 
    qa.show(amenuInfo.targetView); 
} 

संपादित QuickAction: यह ansewer अच्छा नहीं है ... मैं क्यों इस तरह के अजीब विधि किया था? क्योंकि ग्रहण IntelliSense ListView के लिए propmt नहीं था "अच्छा" setOnLongClickListener (के बाद से ListView कम से कम 2 setOnLongClickListener तरीकों ... View से एक और AdapterView कक्षा से दूसरी है) ... सबसे आसान तरीका है जाने अपने ListFragment लागू AdapterView.OnItemLongClickListener और फिर onViewCreated ऐड में है कोड getListView().setOnLongClickListener(this);

+0

मेनूइन्फो शून्य है? अब क्या करे ? – aProgrammer

5

"लंबे समय तक दबाकर", मुझे लगता है कि आप संदर्भ मेनू का जिक्र कर रहे हैं। एक ListFragment के लिए, तुम सब करने की होनी चाहिए संदर्भ मेनू के लिए रजिस्टर करने के लिए है:

@Override 
public void onActivityCreated(Bundle icicle) {  
    registerForContextMenu(getListView()); 
} 

एक बार जब आप करते हैं, ListFragmentonCreateContextMenu() और onContextItemSelected() बुलाना चाहिए जब यह एक लंबे प्रेस का पता लगाता है।

0

संशोधित एरिक डगलस 'जवाब आगे .. किसी कारण जब तक मैं अपने कोड को संशोधित किया और onViewCreated में पंजीकरण रखा इस प्रकार अपने ही एप्लिकेशन क्रैश के लिए:

@Override 
public void onViewCreated (View view, Bundle savedInstanceState) { 
    registerForContextMenu(getListView()); 
} 
0
getListView().setOnItemLongClickListener(new OnItemLongClickListener() { 
    @Override 
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
     // Show your popout menu here. 
    } 
}); 
संबंधित मुद्दे