12

जोड़ने के बाद काम नहीं करता मैं एक ListView (my_list.xml) है:ListView onClickListener() RadioButton

<ListView 
     android:id="@+id/my_list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:choiceMode="singleChoice" 
     /> 

प्रत्येक सूची आइटम के लिए लेआउट (list_item.xml) है:

my_list = (ListView) findViewById(R.id.my_list); 

SimpleAdapter adapter = new SimpleAdapter(context, getOptions(), 
      R.layout.list_item, 
      new String[] { "icon1","str1" }, 
      new int[] {R.id.my_icon, R.id.my_str }); 

my_list.setAdapter(adapter); 

//onClickListener does not work after I added RadioButton in list item layout 
my_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

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

      Log.v("SELECTED", position+""); 
     } 
    }); 
:
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp" 
    > 

    <ImageView 
      android:id = "@+id/my_icon" 
      android:layout_width ="wrap_content" 
      android:layout_height ="wrap_content" 
      android:layout_centerVertical="true" 
    /> 
    <TextView 
     android:id="@+id/my_str" 
     android:layout_width="wrap_content" 
     android:layout_height = "wrap_content" 
     android:layout_toRightOf="@id/my_icon" 
    /> 

    <!--This radio button makes the list item unselectable, why?--> 
    <RadioButton 
     android:id="@+id/my_radio_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     /> 
</RelativeLayout> 

जावा कोड में, मैं सूची के लिए SimpleAdapter का उपयोग

जैसा कि आप देखते हैं, उपरोक्त कोड में, सूची आइटम लेआउट में, मैंने RadioButton जोड़ा, इस बटन को जोड़ने के बाद, मेरी सूची onClickListener अब और काम नहीं करती है, क्यों ?? (यह काम करता है अगर यह सूची आइटम लेआउट पर RadioButton बिना है)

+1

क्योंकि अब हो रहा ** फोकस ** है कि आइटम – waqaslam

+0

के अंदर अपने सूचीदृश्य के मद और रेडियो बटन के बीच की अपनी एक संघर्ष तो इस समस्या से छुटकारा पाने के लिए? मुझे सूची आइटम पर रेडियो बटन चाहिए, और जब उपयोगकर्ता आइटम क्षेत्र पर क्लिक करता है तो मुझे रेडियो बटन का चयन करने की आवश्यकता होती है। –

+0

क्लिक श्रोता केवल तभी काम करता है जब कोई अन्य दृश्य फोकस करने योग्य न हो। अपने चेकबॉक्स को फोकस करने योग्य = "झूठी" को आपके लिए चाल करना चाहिए http://stackoverflow.com/questions/1121192/android-custom-listview-unable-to-click-on-items –

उत्तर

24

अपने RadioButton के लिए निम्न गुण सेट करें:

android:focusable="false" 
android:focusableInTouchMode="false" 

और अपने OnItemClickListener में, आप रेडियो बटन के जाँच निर्धारित करने की आवश्यकता कोड द्वारा ध्वज।


सेट आप नीचे के रूप में ListView:

<ListView 
    android:id="@+id/my_list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
+0

हाँ, अब, यह ठीक है । लेकिन जब उपयोगकर्ता आइटम क्षेत्र पर क्लिक करता है तो रेडियो बटन कैसे चुना जाए? (और इसे एक ही चयन सूची बनाएं) –

+0

मेरा अद्यतन उत्तर देखें ... वास्तव में आपको अपने ऑनआईटमक्लिक लिस्टनर में कोड द्वारा इसे संभालने की आवश्यकता है जब आइटम पर क्लिक किया जाता है तो रेडियो बटन की जांच करें – waqaslam

+0

हाँ, क्या आप यह भी सुझाव दे सकते हैं कि रेडियो कैसे बनाएं सूची में बटन एक ही चयन होने के लिए? मेरा मतलब है, वर्तमान में, हालांकि मैंने सूची का चयन मोड एकल होने के लिए सेट किया है। लेकिन जब भी मैं एक सूची आइटम पर क्लिक करता हूं, तो रेडियो बटन कोड द्वारा "चेक" पर सेट होता है, जिससे सूची एकाधिक चयन योग्य दिखती है। –

2

अपने RadioButton XML कोड को यह कोड जोड़ें:

android:focusable="false" 
android:focusableInTouchMode="false" 

एक अन्य कीवर्ड इस समस्या को हल करने के लिए TouchDelegate है।

संपादित करें:

https://stackoverflow.com/a/5528945/1285331

+0

यह मेरे लिए बिल्कुल काम करता था। +1 – prolink007