के साथ कस्टम सूचीदृश्य पर चयनित आइटम मैंने हाल ही में एंड्रॉइड 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>
अब अगर मैं किसी सूची आइटम पर एक लंबे क्लिक कर सीएबी पता चलता है के रूप में उम्मीद:
मैं एक MultiChoiceModeListener का उपयोग लेकिन दुर्भाग्य से चयनित सूची आइटम यहाँ उदाहरण की तरह पृष्ठभूमि में परिवर्तन नहीं करते (एक आइटम के बाद हल्के नीले रंग की पृष्ठभूमि चयन किया जाता है):
मैं एक कस्टम चयनकर्ता का उपयोग करने के लिए है? या क्या एक मानक प्रक्रिया है कि एंड्रॉइड इसे कैसे संभालता है और मुझे अपने लाइनरलाइट्स को पारदर्शी बनाने की आवश्यकता है?
ListView item background via custom selector
यह किसी को सही दिशा में मुझे बिंदु सकता है बहुत अच्छा होगा: मैं भी निम्न लेकिन कोई सफलता के साथ की कोशिश की। अगर आपको अधिक एप्लिकेशन कोड या एक्सएमएल फाइलों की आवश्यकता है तो कृपया मुझे बताएं।
मुझे एक स्टैक ओवरफ्लो त्रुटि मिलती है यदि मैं 'ऑनटैम चेकस्टेड चेंज()': 'listView.setItem चेक (स्थिति, चेक) के भीतर निम्न कार्य करता हूं; ' – domi
nvm। यह सिर्फ इसके साथ काम करता है: 'एंड्रॉइड: पृष्ठभूमि ="? एंड्रॉइड: एटीआर/सक्रिय बैकग्राउंड इंडिकेटर "'बहुत बहुत धन्यवाद! – domi
डोमी की टिप्पणी के अतिरिक्त: यदि आप चयनित पंक्तियों के रूप को अनुकूलित करना चाहते हैं तो आप पंक्ति के लेआउट के अपने कार्यान्वयन का उपयोग करके ऐसा कर सकते हैं, क्योंकि यह इस ट्यूटोरियल में वर्णित है: http: // www। marvinlabs.com/2010/10/custom-listview-ability-check-items/ – Konsumierer