2014-09-10 77 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) {