I am working on a school project, in which i have a main_activity with two fragments. One of these fragments contains a listview. The listview gets filled through an adapter, and this all seems to work fine. However i try to add an OnItemClickListener to this listview, and i really can't get it to fire.
This is the fragment with the 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" />
This where I add the listener in the 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;
}
Here is the list item i use:
<?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"
/>