2014-09-10 81 views
0

我很茫然。 我有一個ListView通過適配器在片段中設置。ListView OnItemClickListener不會觸發

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_knowledgebase, container, false); 

    // Set the adapter 
    mListView = (AbsListView) view.findViewById(R.id.searchResultList); 


    // Set OnItemClickListener so we can be notified on item clicks 
    mListView.setOnItemClickListener(this); 
    mListView.setAdapter(mAdapter); 

    return view; 
} 


public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    if (null != mCallback) { 
     //call back to activity 
     mCallback.onFragmentInteraction(position); 
    } 
} 

列表的佈局很簡單。只有1個文本視圖,沒有按鈕或複選框。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ListView 
    android:id="@+id/searchResultList" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:footerDividersEnabled="true" 
    android:clickable="true" 
    /> 

<TextView 
    android:id="@+id/name" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:gravity="start" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false" 
    android:textIsSelectable="false"/> 

我已經設置可聚焦的屬性設置爲false,仍然沒有骰子

+1

'如果(空!= mCallback){}'什麼'mCallBack'在這一行? – 2014-09-10 16:00:01

+0

mCallback =(OnFragmentInteractionListener)活動;這是一個鏈接回活動主機片段 – aobrazcova 2014-09-10 16:20:20

+0

原來我誤解了ListView佈局如何工作。我認爲,爲了使自定義列表我需要修改這個默認列表。原來我需要爲列表項目創建自定義佈局,並在自定義適配器中膨脹... – aobrazcova 2014-09-17 17:09:10

回答

0

主要的問題是你的變量mCallback爲空。所以低於這個代碼不觸發...

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    if (null != mCallback) { 
    //call back to activity 
    mCallback.onFragmentInteraction(position); 
    } 
} 

這幾乎是這個

if (null != null) { 

來檢查我的回答,試試這個:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Log.e("ListView OnItemClick","Clicked"); 
    if (null != mCallback) { 
    //call back to activity 
    mCallback.onFragmentInteraction(position); 
    } 
} 

這將記錄ListView OnItemClick Clicked

+0

不是這種情況。 Ididnt先前複製了所有我的代碼,但是mCallback被設置爲public void onAttach(活動活動){super35}。 嘗試mCallback =(OnFragmentInteractionListener)活動; } }我繼續嘗試你的解決方案,它仍然沒有觸發。 – aobrazcova 2014-09-10 16:18:03

+0

'' 這是您的列表視圖行項目嗎? – 2014-09-10 16:22:35

+0

是的。它很簡單。 – aobrazcova 2014-09-10 16:25:21

0

也許應該onListItemClick

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