OnItemClickListener未在listview中触发

时间:2018-04-05 11:02:19

标签: android listview onitemclicklistener

我正在开展一个学校项目,其中我有一个带有两个片段的main_activity。其中一个片段包含listview。 listview通过适配器填充,这一切似乎工作正常。但是我尝试将OnItemClickListener添加到此列表视图中,我真的无法解决它。

这是listview(overview_fragment.xml)的片段:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
   //android:descendantFocusability="blocksDescendants"
>

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />

这是我在overviewFragment.java中添加监听器的地方:

public class OverviewFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.overview_fragment, container, false);
    final ListView listView = view.findViewById(R.id.list_view);
    AdapterView.OnItemClickListener mMessageClickedHandler = new
            AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position, long
                        id) {
                    Log.d(OverviewFragment.class.getSimpleName(), "listener in overviewFragment");
                }
            };
    listView.setOnItemClickListener(mMessageClickedHandler);
    return view;
}

这是我使用的列表项目:

<?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"
android:descendantFocusability="blocksDescendants">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_below="@id/name"
    />

1 个答案:

答案 0 :(得分:0)

您应该为点击监听器添加项目和适配器。移除var SomeView = View.extend({ parentMethod: function() { //some parent code } }) var MyView = SomeView.extend({ parentMethod: function() { //keep parents statements and extend } }) android:descendantFocusability="blocksDescendants"会移除blockDescendants

中所有孩子的焦点