1

我已經使用自己的ArrayAdapter版本創建了自定義ListView。我的問題是,它沒有響應任何onItemClickListeners。這裏是我的代碼片段:自定義ListView(帶CheckBox)無法響應點擊

final ArrayAdapter<Agency> aa=new myCustomAdapter(); 
    lv.setAdapter(aa); 

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View myView, int pos, 
       long id) { 
      TextView clickedTV=(TextView)myView.findViewById(R.id.rowtext1); 
      Toast.makeText(getBaseContext(), clickedTV.getText().toString(), Toast.LENGTH_LONG).show(); 


     } 
    }); 

適配器實現:

private class myCustomAdapter extends ArrayAdapter<Agency> { 

    public myCustomAdapter() { 
     super(getBaseContext(), R.layout.layout_row, AgencyList); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View itemView=convertView; 
     if (itemView==null) 
      itemView=getLayoutInflater().inflate(R.layout.layout_row, parent, false); 
     TextView rowText=(TextView)itemView.findViewById(R.id.rowtext1); 
     Agency currentAgency=AgencyList.get(position); 
     rowText.setText(currentAgency.getAgency().toString()); 
     return itemView; 
    } 
} 

我創建的每一行,然後在我的適配器膨脹的自定義XML佈局。定製行佈局具有的TextView和一個CheckBox:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/rowtext1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="23dp" 
     android:layout_marginTop="21dp" 
     android:text="Row Item" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <CheckBox 
     android:id="@+id/rowcheckBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/rowtext1" 
     android:layout_alignBottom="@+id/rowtext1" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="26dp" 
     android:text="Select this" /> 

</RelativeLayout> 

不能我在ListView那裏已經是一個可點擊的控件(如複選框),可點擊行?我已經在許多手機中看到過這種實現(例如索尼的Timescape UI)。 請建議解決!謝謝你的幫助!

回答

1

必須確保每當您使用自定義ListView執行並且onItemClick事件不會被觸發時,您必須爲您的自定義佈局UI元素設置此屬性。因此將其設置爲TextView和複選框

android:focusable="false" 
android:focusableInTouchMode="false" 
+0

我是否需要將這些屬性添加到所有對象?我的意思是列表視圖和複選框? –

+0

是的,你必須設置所有的對象 – Piyush

2

設置此定製佈局的複選框..

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

所以你完整的代碼會是這個樣子

<CheckBox 
    android:id="@+id/rowcheckBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/rowtext1" 
    android:layout_alignBottom="@+id/rowtext1" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="26dp" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:text="Select this" /> 

看起來你的複選框引起此問題。 ..

+0

謝謝你!你是一個拯救生命的人!我在我的複選框xml中添加了這些行。爲我工作!當然,我會盡快接受答案,只要stackoverflow允許我(它說,我可以接受5分鐘後的答案!):D) –

+0

好吧,NP。很高興幫助你。快樂編碼。 – InnocentKiller

相關問題