0

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"
    />

Roykovic
  • 3
  • 4
  • Possible duplicate of https://stackoverflow.com/questions/5551042/onitemclicklistener-not-working-in-listview – beeb Apr 05 '18 at 11:25
  • The topcomment on that question proposes to add a line of code. As you can see in my snippets, i already added that line – Roykovic Apr 05 '18 at 11:57
  • You should add this line in your `row_item.xml` file, not in your fragments layout file... As I don't know your `row_item.xml` I only can assume that it's a possible duplicate. The problem looks like the same. – beeb Apr 05 '18 at 12:22
  • I added the code of the list item – Roykovic Apr 05 '18 at 14:54
  • Can you try to add `android:clickable="false"` and `android:focusable="false"` to your TextView inside your `row_item.xml`? – beeb Apr 06 '18 at 08:27

1 Answers1

0

You should add items and adapter for the on click listener to work. Remove android:descendantFocusability="blocksDescendants", blocksDescendants removes focus for all the children in the ViewGroup.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Subhag
  • 1
  • 1
  • I do add these, in the mainactivity. When i open the app, a list with the added items shows. However nothing happens when i click these items. – Roykovic Apr 05 '18 at 12:44
  • Have you removed android:descendantFocusability="blocksDescendants" from the parentview of listitem – Subhag Apr 05 '18 at 13:04
  • should i remove it all together, or put it in the list item as stated in the comment above? – Roykovic Apr 05 '18 at 14:35
  • remove it altogether , that is irrelevant for your onclick. For reference , i am adding the simplest way to set adapter, items to the ListView. – Subhag Apr 06 '18 at 06:53
  • String[] items = {"Apple","Banana","Mango","Grapes","Berries"}; ArrayAdapter adapter = new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,android.R.id.text1,items); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Log.i("this", "listener in overviewFragment"); } }); – Subhag Apr 06 '18 at 06:58