Search code examples
javaandroidandroid-layoutlistviewonitemclicklistener

setOnItemClickListener() not working on listView of Fragments


I have some issues with handle click's on my ListView element witch contains customized ArrayAdapter of Fragments. There is no checkbox anywhere, only Button in Fragment is ImageButton and it already has android:focusable="false". In Layout of ListView I have some FrameLayout's, some Buttons and EditText XML code Below. Any solutions like android:focusable="false" ,android:focusableInTouchMode = "false" , list.setItemsCanFocus(false). I searched a lot but nothing helps.

Here is main container for listView with Gmap and listView above:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/map"
     android:name="com.google.android.gms.maps.SupportMapFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="com.example.medhigh.mapfragmet.MapsActivity" />

 <LinearLayout
     android:orientation="horizontal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginLeft="24dp"
     android:layout_marginRight="8dp"
     android:id="@+id/searchLine">

    <FrameLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:padding="5dp">

         <EditText
             android:layout_marginLeft="16dp"
             android:layout_marginRight="42dp"
             android:singleLine="true"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:id="@+id/editText"
             android:layout_weight="1"
             android:textColor="@color/black"
             android:textSize="18sp"
             android:hint="  Start Typing Here"
             android:textColorHighlight="@color/colorPrimaryTest"
             android:textColorHint="#959595" />

         <Button
             android:id="@+id/clearEditText"
             android:layout_width="25dp"
             android:layout_height="25dp"
             android:layout_marginRight="50dp"
             android:layout_gravity="right|center_vertical"
             android:background="@drawable/close"
             android:focusable="false"/>

     </FrameLayout>
 </LinearLayout>

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:name="com.google.android.gms.maps.SupportMapFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context="com.example.medhigh.mapfragmet.MapsActivity">

     <Button
         style="?android:attr/buttonStyleSmall"
         android:layout_width="48dp"
         android:layout_height="48dp"
         android:id="@+id/buttonSwitchToList"
         android:layout_alignParentBottom="true"
         android:layout_alignParentEnd="true"
         android:layout_marginBottom="98dp"
         android:layout_marginRight="7dp"
         android:background="@drawable/format_list"
         android:focusable="false"/>

     <SeekBar
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/seekBar"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:layout_marginRight="42dp"
         android:layout_marginLeft="42dp"
         android:layout_marginBottom="12dp"
         android:max="300" />

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/seekBarProgress"
         android:textColor="@color/black"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true" />
 </RelativeLayout>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="0dp"
     tools:context=".search.NameFragment"
     android:background="@color/white"
     android:id="@+id/layoutList">

     <LinearLayout
         android:orientation="horizontal"
         android:layout_width="match_parent"
         android:layout_height="wrap_content">

         <FrameLayout
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:padding="5dp">

             <EditText
                 android:layout_marginLeft="16dp"
                 android:layout_marginRight="42dp"
                 android:singleLine="true"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:id="@+id/editTextList"
                 android:layout_weight="1"
                 android:textColor="@color/black"
                 android:textSize="18sp"
                 android:hint="  Start Typing Here"
                 android:textColorHighlight="@color/colorPrimaryTest"
                 android:textColorHint="#959595" />

             <Button
                 android:id="@+id/clearEditTextList"
                 android:layout_width="25dp"
                 android:layout_height="25dp"
                 android:layout_marginRight="10dp"
                 android:background="@drawable/close"
                 android:layout_gravity="end|center_vertical"
                 android:focusable="false"/>

         </FrameLayout>
     </LinearLayout>

     <FrameLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content">

         <ListView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/listView"
             android:clickable="true" />

         <Button
             android:id="@+id/buttonBackToMap"
             android:layout_width="48dp"
             android:layout_height="48dp"
             android:layout_marginRight="12dp"
             android:layout_marginBottom="24dp"
             android:background="@drawable/map"
             android:layout_gravity="end|bottom"
             android:focusable="false"/>
     </FrameLayout>
 </LinearLayout> </FrameLayout>

Here is Fragment of List View:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="112dp">

     <ImageView
         android:layout_width="80dp"
         android:layout_height="80dp"
         android:id="@+id/imageView2"
         android:layout_alignParentTop="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentStart="true"
         android:layout_marginLeft="16dp"
         android:layout_marginStart="16dp"
         android:layout_marginTop="16dp"
         android:src="@drawable/news_demo"
         android:scaleType="fitXY" />

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dp"
         android:textAppearance="?android:attr/textAppearanceMedium"
         android:text="Location Name"
         android:id="@+id/textViewNewsHeader"
         android:textColor="@color/colorPrimaryTest"
         android:layout_alignTop="@+id/imageView2"
         android:layout_toRightOf="@+id/imageView2"
         android:layout_toEndOf="@+id/imageView2" />

     <TextView
         android:layout_width="wrap_content"
         android:lines="4"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dp"
         android:layout_marginTop="5dp"
         android:layout_marginRight="60dp"
         android:textAppearance="?android:attr/textAppearanceSmall"
         android:text="Location description"
         android:id="@+id/textViewShortStory"
         android:textColor="@color/black"
         android:layout_below="@+id/textViewNewsHeader"
         android:layout_toRightOf="@+id/imageView2"
         android:layout_toEndOf="@+id/imageView2" />

     <TextView
         android:layout_width="wrap_content"
         android:lines="4"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dp"
         android:layout_marginRight="60dp"
         android:textAppearance="?android:attr/textAppearanceLarge"
         android:text="5"
         android:id="@+id/clinicNumber"
         android:textColor="@color/colorPrimaryTest"
         android:layout_alignParentBottom="true"
         android:layout_alignEnd="@+id/imageButton"
         android:layout_marginEnd="13dp" />

     <ImageButton
         android:layout_width="42dp"
         android:layout_height="42dp"
         android:id="@+id/imageButton"
         android:src="@drawable/arrow_right_black"
         android:background="@color/white"
         android:scaleType="fitXY"
         android:layout_marginRight="20dp"
         android:layout_centerVertical="true"
         android:layout_alignParentRight="true"
         android:layout_alignParentEnd="true"
         android:focusable="false"
         />
 </RelativeLayout>

Here is some code from view Container class

 adapter = new LocationHospitalListFragmentAdapter(getContext(), locationList, R.layout.fragment_search_location_hospital_list);
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //show list view layout
        Toast.makeText(getActivity().getApplicationContext(),"onItemClick",Toast.LENGTH_SHORT).show();
        Log.d("333__","on click");
    }
});

Solution

  • add this line to parent layout of ListItem android:descendantFocusability="blocksDescendants"