Search code examples
androidandroid-fragmentsandroid-listfragment

onListItemClick not working in a ListFragment placed in an activity


I have a standard activity which has a ListFragment "embedded" into the layout through XML. The ListFragment is defined by a custom adapter.

Though, the list gets populated, I'm unable to click on the list items; the click is not getting registered at all. The list item layout has only two TextViews in it.

Here is the code for the ListFragment code:

public class alarmList extends ListFragment {

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };

        // use your custom layout
        alarmListCustomAdapter adapter = new alarmListCustomAdapter(getActivity(), values, "fonts/Lato-Light.ttf");
        setListAdapter(adapter);

    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("checking: ", "WORKING");
        String item = (String) getListAdapter().getItem(position);
        Toast.makeText(getActivity(), item + " selected", Toast.LENGTH_LONG).show();
    }

}

Here is the layout of the list element:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:scrollbars="none"
    android:background="#dfdfdf"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:focusable="false">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="#fbfbfb"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:paddingBottom="14dp"
            android:paddingTop="14dp"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin" >

            <TextView
                android:id="@+id/alarm_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="@string/wall_clock_hint"
                android:textSize="25sp"
                android:layout_alignParentLeft="true"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

            <TextView
                android:id="@+id/alarm_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/alarm_time"
                android:layout_marginTop="8dp"
                android:textSize="15sp"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:hint="LABEL"/>

        </RelativeLayout>

        <View
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_width="fill_parent"
            android:layout_height="5dip"
            android:background="@drawable/drop_shadow">
        </View>

     </LinearLayout>


</ScrollView>

What could be wrong here?

I have gone through similar questions on SO, but none of the solutions worked for me.


Solution

  • Don't embed scrollable widgets like ScrollView(from the row layout) in another scrollable widget like a ListView.