41

के साथ कस्टम सूचीदृश्य पर चयनित आइटम मैंने हाल ही में एंड्रॉइड actionbars और contextual action bars (CAB) का उपयोग शुरू किया।प्रासंगिक कार्रवाई बार

मेरे पास केवल एक गतिविधि है जो एक सूची गतिविधि है। मूल रूप से मैं सीएबी "को सक्रिय" के लिए निम्न कोड कतरना का उपयोग करें:

ListView listView = getListView(); 
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { 

    @Override 
    public void onItemCheckedStateChanged(ActionMode mode, int position, 
             long id, boolean checked) { 
     // Here you can do something when items are selected/de-selected, 
     // such as update the title in the CAB 
    } 

    @Override 
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
     // Respond to clicks on the actions in the CAB 
     switch (item.getItemId()) { 
      case R.id.menu_delete: 
       deleteSelectedItems(); 
       mode.finish(); // Action picked, so close the CAB 
       return true; 
      default: 
       return false; 
     } 
    } 

    @Override 
    public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
     // Inflate the menu for the CAB 
     MenuInflater inflater = mode.getMenuInflater(); 
     inflater.inflate(R.menu.context, menu); 
     return true; 
    } 

    @Override 
    public void onDestroyActionMode(ActionMode mode) { 
     // Here you can make any necessary updates to the activity when 
     // the CAB is removed. By default, selected items are deselected/unchecked. 
    } 

    @Override 
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
     // Here you can perform updates to the CAB due to 
     // an invalidate() request 
     return false; 
    } 
}); 

सूची के लेआउट:

<ImageView 
    android:id="@+id/message_on_clipboard_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:minWidth="30dp" 
    android:padding="7sp" 
    android:src="@android:drawable/presence_online" > 
</ImageView> 

<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/listitem_background" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/message" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5sp" 
     android:fadingEdge="horizontal" 
     android:singleLine="true" 
     android:text="TextView" 
     android:textAppearance="?android:attr/textAppearanceLarge" > 
    </TextView> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/listitem_background" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/message_length" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5sp" 
      android:text="@string/message_length" 
      android:textAppearance="?android:attr/textAppearanceSmall" > 
     </TextView> 

     <TextView 
      android:id="@+id/message_count" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5sp" 
      android:text="TextView" 
      android:textAppearance="?android:attr/textAppearanceSmall" > 
     </TextView> 

     <TextView 
      android:id="@+id/date_label" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5sp" 
      android:text="@string/date_label" > 
     </TextView> 

     <TextView 
      android:id="@+id/date_message" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TextView" > 
     </TextView> 
    </LinearLayout> 
</LinearLayout> 

और main.xml भीतर:

<ListView 
    android:id="@+android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
> 
</ListView> 

अब अगर मैं किसी सूची आइटम पर एक लंबे क्लिक कर सीएबी पता चलता है के रूप में उम्मीद:

cab

मैं एक MultiChoiceModeListener का उपयोग लेकिन दुर्भाग्य से चयनित सूची आइटम यहाँ उदाहरण की तरह पृष्ठभूमि में परिवर्तन नहीं करते (एक आइटम के बाद हल्के नीले रंग की पृष्ठभूमि चयन किया जाता है):

enter image description here

मैं एक कस्टम चयनकर्ता का उपयोग करने के लिए है? या क्या एक मानक प्रक्रिया है कि एंड्रॉइड इसे कैसे संभालता है और मुझे अपने लाइनरलाइट्स को पारदर्शी बनाने की आवश्यकता है?

ListView item background via custom selector

यह किसी को सही दिशा में मुझे बिंदु सकता है बहुत अच्छा होगा: मैं भी निम्न लेकिन कोई सफलता के साथ की कोशिश की। अगर आपको अधिक एप्लिकेशन कोड या एक्सएमएल फाइलों की आवश्यकता है तो कृपया मुझे बताएं।

उत्तर

52

मैंने कभी भी CHOICE_MODE_SINGLE में इसका परीक्षण किया है, लेकिन उस स्थिति में यह निम्न कार्य करके कार्य करता है।

  • जब आप किसी सूची आइटम का चयन करें, कोड में, कहते हैं "setItemChecked (स्थिति, जाँच)" विधि (ListView उदाहरण पर) सूची में उस आइटम के लिए।

  • व्यक्ति ListView मदों के लिए एक्सएमएल को यह करें:
    android:background="?android:attr/activatedBackgroundIndicator"

+6

मुझे एक स्टैक ओवरफ्लो त्रुटि मिलती है यदि मैं 'ऑनटैम चेकस्टेड चेंज()': 'listView.setItem चेक (स्थिति, चेक) के भीतर निम्न कार्य करता हूं; ' – domi

+8

nvm। यह सिर्फ इसके साथ काम करता है: 'एंड्रॉइड: पृष्ठभूमि ="? एंड्रॉइड: एटीआर/सक्रिय बैकग्राउंड इंडिकेटर "'बहुत बहुत धन्यवाद! – domi

+0

डोमी की टिप्पणी के अतिरिक्त: यदि आप चयनित पंक्तियों के रूप को अनुकूलित करना चाहते हैं तो आप पंक्ति के लेआउट के अपने कार्यान्वयन का उपयोग करके ऐसा कर सकते हैं, क्योंकि यह इस ट्यूटोरियल में वर्णित है: http: // www। marvinlabs.com/2010/10/custom-listview-ability-check-items/ – Konsumierer

0

केवल आईसीएस उपकरणों और समर्थन करने के लिए कोशिश कर रहा है लेकिन इस नए स्वरूप का आप को प्राप्त करने के लिए क्या प्रयास कर रहे हैं चयनित लाइन ध्यान में रखते हुए करता है पर प्रकाश डाला।

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@color/highlight" android:state_activated="true"/> 
    <item android:drawable="@color/normal"/> 
</selector> 

और अपने माता पिता के लेआउट पर पृष्ठभूमि के रूप में सेट करें::

adapter = new SimpleCursorAdapter(getActivity(), 
    android.R.layout.simple_list_item_activated_1, 
    null, 
    from, 
    to, 
    0); 
12

बस आ drawable रास्ते पर custom_background बनाएं जिसका नाम

android:background="@drawable/custom_background" 
+0

बिल्कुल सही! मैंने चयनकर्ता बनाया और इसे ग्रिड व्यू आइटम पृष्ठभूमि के रूप में लागू किया। धन्यवाद। –

0

किसी के लिए अभी भी इस समस्या से मिलने, कृपया जांच करें कि आपके एंड्रॉयड: minSdkVersion। यदि यह बहुत कम है, तो चयनकर्ता पृष्ठभूमि रंग काम नहीं कर सकता है।

संबंधित मुद्दे