Felipe Caldas
Felipe Caldas

Reputation: 2503

When adding onClick listener to view inside an Adapter, the ListView's listSelector style doesn't work

I am facing this issue and it is driving me nuts. I have the following adapter code:

private class ListAdaptor extends ArrayAdapter<String> {
        private ArrayList<String> listVO;

        public ListAdaptor(Context context, int textViewResourceId, ArrayList<String> items) {
            super(context, textViewResourceId, items);
            this.listVO = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                v = ViewGroup.inflate(getContext(), R.layout.list_friends_inflate, null);
            }
            String o = listVO.get(position);
            TextView fullName = (TextView) v.findViewById(R.id.textViewName);
            fullName.setText(o);

            v.setOnClickListener(new OnClickListener() {                
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT);
                }
            });
            /*v.setOnTouchListener(new OnTouchListener() {              
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Toast.makeText(getApplicationContext(), "test2", Toast.LENGTH_SHORT);
                    return false;
                }
            });*/           
            return v;
        }
    }

This Adapter class is inside a ListActivity. When I tap in the listView's item, the listSelector style behaviour does not happen. If I comment the onClickListener and tap it, it works. If I uncomment the onTouchListener and tap on the item, it also works, but I don't want a onTouch event, but a onClick.

The xml of the view that I am inflating in the Adapter is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp" android:clickable="false" android:focusableInTouchMode="false">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" android:clickable="false" android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/textViewName"
        style="@style/TextViewStyleCommon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_marginBottom="18dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/imageView1"
        android:textAppearance="?android:attr/textAppearanceMedium" android:clickable="false" android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/textViewPoints"
        style="@style/TextViewStyleCommon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textViewName"
        android:layout_alignBottom="@+id/textViewName"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:clickable="false" android:focusableInTouchMode="false"/>

</RelativeLayout>

I know that there's a problem with having focusable elements in the xml that you inflate, making the onClick event not work. The problem is that my event does work, but the style behaviour doesn't... this is crazy.

On my styles.xml I have:

<style name="MyTheme" parent="android:Theme.Light">
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@drawable/ndp_background</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:listViewStyle">@style/MyListView</item>
    </style>

    <style name="MyListView" parent="@android:style/Widget.ListView.White">
        <item name="android:listSelector">@drawable/list_subject_selector</item>
        <item name="android:cacheColorHint">@android:color/transparent</item>
    </style>

I define MyTheme as the application's theme in the Android manifest...

Has anyone ever seen such behaviour? Many thanks, Felipe

Upvotes: 3

Views: 2760

Answers (1)

Sarim Sidd
Sarim Sidd

Reputation: 2176

If you want to do some stuff on Listview Item click,so why not use on setOnItemClickListener.

list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
     Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT);
    });

Upvotes: 1

Related Questions