2011-04-05 65 views
205

Activity類代碼:OnItemCLickListener不在列表視圖工作

conversationList = (ListView)findViewById(android.R.id.list); 
ConversationArrayAdapter conversationArrayAdapter=new ConversationArrayAdapter(this, R.layout.conversation_list_item_format_left, conversationDetails); 
conversationList.setAdapter(conversationArrayAdapter); 
conversationList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 
     Log.d("test","clicked"); 
    } 
}); 

AdaptergetView功能:

if (v == null) {         
    LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if(leftSideMessageNumber.equals(m.getTo())) { 
     v = vi.inflate(R.layout.conversation_list_item_format_left, null); 
    } else { 
     v = vi.inflate(R.layout.conversation_list_item_format_right, null); 
    } 
} 

是否有使用兩個個XML而膨脹的問題嗎?

+2

@hiei我的問題是,我在我的佈局單元格中使用imagebuttons。即使添加「android:descendantFocusability」之後,我的點擊偵聽器仍未響應。我所做的是,我將所有的ImaegButtons改爲ImageViews。這解決了我的問題。 – 2015-11-19 07:43:58

回答

84

問題是您的佈局包含可聚焦或可點擊的項目。 如果視圖包含可聚焦或可點擊的項目,則不會調用OnItemCLickListener。

點擊here瞭解更多信息。

如果不是這種情況,請發佈您的佈局xmls之一。

+1

這是我的解決方案,我宣佈我的觀點是可點擊的。謝謝! – kingraam 2012-03-06 13:27:14

+0

我已經堅持了6個小時,我真的需要幫助。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – 2016-01-31 02:56:42

+1

謝謝!我將2個'EditText'改爲'TextView',並且'CheckBox'被允許保留(並且保持可切換),將android:descendantFocusability =「blocksDescendants」添加到我的'LinearLayout'中。 – Azurespot 2016-05-13 03:27:30

672

我剛剛找到的解決方案,從這裏..但深點擊...

如果列表中的任何行項目包含可聚焦或點擊視圖然後OnItemClickListener將無法​​正常工作。

行項目必須有一個像android:descendantFocusability="blocksDescendants"這樣的參數。

在這裏你可以看到例子,你的列表項應該是什麼樣子。 您的列表項的XML應該是... row_item.xml(your_xml_file.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:baselineAligned="false" 
android:descendantFocusability="blocksDescendants" 
android:gravity="center_vertical" > 

// your other widgets here 

</LinearLayout> 
+0

不適合我。任何人都可以看看[http://stackoverflow.com/questions/21692209/why-my-listview-never-calls-onitemclick] – suitianshi 2014-02-11 03:07:37

+2

你也可以用'listView.setDescendantFocusability(int focus)'編程方式設置它;'where '焦點'是'ViewGroup.FOCUS_BEFORE_DESCENDANTS','ViewGroup.FOCUS_AFTER_DESCENDANTS'或'ViewGroup.FOCUS_BLOCK_DESCENDANTS'之一' – 2015-01-31 13:03:02

+0

我認爲這很有趣,有20個人在評論中感謝你。過去8個小時我一直在遇到這個問題,非常感謝。 – sparticvs 2015-04-28 21:15:37

50

對於我的名單,我行具有可以點擊,如按鈕其他的事情,這樣做毯子blocksDescendants沒有按」工作。相反,我在按鈕的XML添加一行:

android:focusable="false" 

這令按鈕從阻塞行的點擊,但還是讓按鍵取點擊了。

+1

對我無效 – 2013-05-29 05:14:58

+0

@ user1840899這是一個常見的反應,我從中學到了一個,所以我通過了它。這是正確的信息,不值得投降。 – 2013-05-30 17:32:53

+0

這適用於我在ListView項目中的複選框(不包括BlocksDescendants) – thpitsch 2013-07-25 04:19:08

19

使用按鈕標籤內下面的代碼在列表視圖

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

我一直堅持這6個小時了,我真的需要幫助。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – 2016-01-31 02:57:06

+0

偉大的人它真的很有幫助! – 2017-02-14 06:35:46

+0

這工作在這裏! 我在這個視圖中有很多複選框,我嘗試使用'android:descendantFocusability =「blocksDescendants」'並阻止這些複選框的點擊 – 2017-03-05 00:35:56

5

的自定義行佈局我有同樣的問題,並嘗試了所有的解決方案中提到的無濟於事。通過測試,我發現使文本可選是阻止了監聽器被調用。所以通過將其切換爲false或將其刪除,我的聽衆再次被調用。

android:textIsSelectable="false" 

希望這可以幫助像我這樣卡住的人。

+0

我一直堅持6個小時,現在我真的需要幫助。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – 2016-01-31 02:58:05

15

我有同樣的問題,我剛纔看到我不小心設置:

@Override 
public boolean isEnabled(int position) 
{ 
    return false; 
} 

我CustomListViewAdapter類。

通過這一改變到:

return true; 

我已經設法解決這個問題。如果有人犯了同樣的錯誤,以防萬一...

+6

我也有'@Override public void onApplicationStart {ExplodeThisDevice(); }' – 2014-03-25 17:14:30

+0

@LéonPelletier非常感謝!這解決了我的問題! – 2014-06-29 07:28:59

+0

我已經堅持了6個小時,我真的需要幫助。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – 2016-01-31 02:57:20

10

使用安卓descendantFocusability

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="80dip" 
    android:background="@color/light_green" 
    android:descendantFocusability="blocksDescendants" > 

上面添加在根佈局

+1

添加詳細信息可能會使這是一個更好的答案和+1。 – Killer 2017-10-02 05:56:14

10

如果你有textviews,按鈕或STG點擊,或者選擇在你行僅查看

android:descendantFocusability="blocksDescendants" 

是不夠。你必須設置

android:textIsSelectable="false" 

您textviews和

android:focusable="false" 

你的按鈕和其他可聚焦的項目。

+0

我學會了這一點。 android:textIsSelectable =「false」注意這個:) – 2015-08-06 04:16:40

+0

我一直堅持這個6小時,現在我真的需要幫助。 http://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – 2016-01-31 02:57:55

9

即使我有同樣的問題,我有複選框,做了以下以掩蔽itemClickListener工作,

添加以下屬性複選框,

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

和ItemClickListner開始工作。

對於詳細的例子,你可以去通過鏈接,

http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/

希望它可以幫助乾杯!

24

你需要做你的listview_item.xml 2步

  1. 設置根佈局:android:descendantFocusability="blocksDescendants"
  2. 設置任何可獲得焦點或可點擊查看在此項目將用:
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"

下面是一個例子:listview_item。XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10dp" 
    android:layout_marginTop="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:gravity="center_vertical" 
    android:orientation="vertical" 
    android:descendantFocusability="blocksDescendants"> 

    <RadioButton 
     android:id="@+id/script_name_radio_btn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" 
     android:textColor="#000" 
     android:padding="5dp" 
     android:clickable="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     /> 

</LinearLayout> 
+0

在項目視圖中設置'clickable ='true'將使'onItemClick'不被調用。因此,注意在項目根視圖上添加'clickable =「false」',在這種情況下是'LinearLayout' – vovahost 2016-02-15 08:58:16

+0

設置android:descendantFocusability =「blocksDescendants」可以幫助我。謝謝! – LLIAJLbHOu 2016-08-23 20:29:50

+0

12/21/16和之後小時的搜索,這工作,謝謝你! – 2016-12-21 09:16:43

3

兩個真棒解決方案是這一點,如果你從一個片段延伸ListFragment,知道在創建活動之前mListView.setOnItemClickListener不會被調用,這保證了當活動已創建

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    mListView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) { 
      // Do the onItemClick action 

      Log.d("ROWSELECT", "" + rowId); 
     } 
    }); 
} 
它被設置

注視的ListFragment的源代碼,我碰到這個

public class ListFragment extends Fragment { 
    ........................................... 
    ................................................ 

    final private AdapterView.OnItemClickListener mOnClickListener 
       = new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      onListItemClick((ListView)parent, v, position, id); 
     } 
    }; 

    ................................................................ 
    ................................................................ 

    public void onListItemClick(ListView l, View v, int position, long id) { 
    } 
} 

onItemClickListener來到物體附着並呼籲onListItemClick() 這樣其他類似的解決方案,它以完全相同的方式工作是重寫onListItemClick()

@Override 
public void onListItemClick(ListView l, View v, int position, long rowId) { 
    super.onListItemClick(l, v, position, id); 
    // Do the onItemClick action 

    Log.d("ROWSELECT", "" + rowId); 
} 
1

我已經嘗試了所有以上並沒有什麼工作。

我解決了這個問題,如下所示:

首先我定義調用自定義按鈕ListButton

public class ListButton extends android.widget.Button 
{ 

private ButtonClickedListener clickListener; 

public ListButton(Context context) 
{ 
    this(context, null); 
} 

public ListButton(Context context, AttributeSet attrs) 
{ 
    this(context, attrs, 0); 
} 

public ListButton(Context context, AttributeSet attrs, int defStyle) 
{ 
    super(context, attrs, defStyle); 
} 

public void setClickListener(ButtonClickedListener listener) { 
    this.clickListener = listener; 
} 

@Override 
public boolean isInTouchMode() { 
    return true; 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 

    return false; 
} 

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 

    switch (event.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_UP: 

       eventClicked(); 

       break; 
      case MotionEvent.ACTION_CANCEL: 
       break; 
      case MotionEvent.ACTION_MOVE: 
       break; 
      default : 

     } 
    return true; 
} 

private void eventClicked() { 
    if (this.clickListener!=null) { 
     this.clickListener.ButtonClicked(); 
    } 
} 

} 

的XML看起來像:

<dk.example.views.ListButton 
android:id="@+id/cancel_button" 
android:layout_width="125dp" 
android:layout_height="80dp" 
android:text="Cancel" 
android:textSize="20sp" 
android:layout_margin="10dp" 
android:padding="2dp" 
android:background="#000000" 
android:textColor="#ffffff" 
android:textStyle="bold" 
/> 

然後我定義自己的ButtonClicked Listener接口:

public interface ButtonClickedListener { 
    public void ButtonClicked(); 
} 

然後我用我自己的聽衆,就好像它是正常OnClickListener

final ListButton cancelButton = (ListButton) viewLayout.findViewById(R.id.cancel_button); 

    cancelButton.setClickListener(new ButtonClickedListener() { 

     @Override 
     public void ButtonClicked() { 
      //Do your own stuff here... 
     } 

    }); 
0

如果你想同時使用簡單的點擊,長點擊列表視圖中的項目更好的方式來實現,這將是使用上下文菜單進行長按。避免使用setItemLongClickListener,特別是如果您的列表視圖有多個行佈局。

+0

我一直堅持這個6小時了,我真的需要幫助。 ://stackoverflow.com/questions/35108940/why-cant-i-remove-an-item/35109304#35109304 – 2016-01-31 02:59:00

0

面對同樣的問題,試了幾個小時。如果您嘗試了上述所有操作,請嘗試將listview的layout_width更改爲wrap_content的match_parent。

10

this answer

1.增加的幫助下解決了它的線性佈局以下list_items.xml在list_items.xml的LinearLayout的 android:descendantFocusability="blocksDescendants"

2.Child查看

android:focusable="false" 
+1

謝謝你對我的工作 – 2017-02-25 04:06:55

+0

您的鏈接已損壞 – 2017-02-27 08:59:26

+1

@JasonC感謝您的信息。鏈接h TTP://stackoverflow.com/a/14915758/5740760 – 2017-02-27 09:26:40

0

以上所有都失敗了。但是,我能夠解決這個問題(如果你正在傾聽,谷歌,如果你正在聽的話,經過好幾個小時後,請考慮修復下面遇到的編譯器錯誤的問題)

你真的有要小心你在這裏添加到你的xml佈局的android屬性(在這個原始問題中,它被稱爲list_items.xml)。對我來說,導致這個問題的原因是我從EditText視圖切換到了TextView,並且從變化(在我的情況下爲inputType)中剩下了屬性cruft。編譯器沒有捕獲它,當我去運行應用程序時,可點擊性失敗。仔細檢查你的佈局xml節點中的所有屬性。

1
  1. 在主要佈局

    android:descendantFocusability="blocksDescendants" 
    
  2. 寫的代碼添加到每個按鈕,TextView的,ImageView的等,這有 的onClick

    android:focusable="false" 
    
    android:clickable="false" 
    

希望它會工作這一點。