Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView with horizontalScrollView OnItemClick not working

So really quickly i have a listView with a custom adapter and it inflates a view that contains a horizontalScrollView as well as a textview etc. The problem I am having is that when i try to attach a listener to this listView it is not receiving any callbacks.

I believe the problem has to do with the fact that my list item contains a scroll view which is intercepting the click events (although i thought it should only intercept other gestures).

code... (my list item xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/grey" >

        <TextView
            android:id="@+id/headerName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:padding="4dp"
            android:textColor="@color/text_header"
            android:textStyle="bold"
            android:text="TextView" />

    </RelativeLayout>
    <View
        android:background="@color/border"
        android:layout_width="fill_parent"
        android:layout_height="1px" />
    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="fill_parent"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearImages"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" 
            android:padding="4dp">
        </LinearLayout>
    </HorizontalScrollView>
    <View
        android:background="@color/border"
        android:layout_width="fill_parent"
        android:layout_height="1px" />
</LinearLayout>

and then in my onCreate...

lv.setAdapter(myobj.adapter);
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Log.w("dsdsds", "sdsdsds");
        }});

Any help or suggestions would be greatly appreciated

like image 615
Maurycy Avatar asked Jan 28 '12 22:01

Maurycy


2 Answers

Add this to the topmost Layout:

android:descendantFocusability="blocksDescendants"

It did the trick for me, now onItemClickListener is being fired.

like image 133
Nicholas Ng Avatar answered Oct 07 '22 18:10

Nicholas Ng


You can call setOnClickListener for linearMain in the getView() method of your adapter.

like image 30
akarimova Avatar answered Oct 07 '22 18:10

akarimova