ListView OnItemClickListener不会触发

时间:2014-09-10 15:55:00

标签: android listview android-listview

我很茫然。 我有一个ListView通过片段中的适配器设置。

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,但仍然没有骰子

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

也许应该是onListItemClick

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