4

OK I've searched and seen similar issues, tried them and no avail. I have a listView with some elements and I want to click on one element and display a detail somewhere else.

This is my listView

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pedidos);

    ListView listView = (ListView) findViewById(R.id.actualStoresList);

    Model.initialize();
    Vector<String> values = Model.stores;

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position,
                    long id) {
                Object o = adapter.getItemAtPosition(position);
                String str_text = o.toString();
                Log.i("", "I have selected this: " + str_text);
            }

        });

    CustomStringAdapter adapter = new CustomStringAdapter(this, R.layout.my_list_layout, R.id.list_content, values);
    listView.setAdapter(adapter);
}

This is the my_list_layout.xml

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

<TextView
    android:id="@+id/list_content"
    android:textColor="#000000"
    android:gravity="center"
    android:layout_margin="4dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"/>

</LinearLayout>

The list gets displayed correctly. When I click on an element of the list (A textView) it "steals" the click event so nothing happens (the onItemClickListener is attached to the listView, not the TextView).

The textView has an small margin where, if careful, I can click just behind it, in fact, touching the listView. In this case, the event gets fired ok and I see the log.

I've tried to set the TextView android:focusable="false" but still, the TextView is "above" of the listView and always gets the click events.

How can I either make the TextView "transparent" so it actually clicks on the listView, or add a onclickListener to the TextView so I can handle its events?

Thanks!

Alejandro

meh
  • 22,090
  • 8
  • 48
  • 58
KingCrimson
  • 97
  • 3
  • 9
  • Have you tried putting `android:descendantFocusability="blocksDescendants"` into your LinearLayout? – Aaron He Jan 30 '13 at 22:01
  • Check answers here: http://stackoverflow.com/questions/6374592/passing-touch-events-to-the-parent-view Seems like you can pass `return false;` on the TextView to have the event not consumed and passed through. – Grambot Jan 30 '13 at 22:02
  • I tried blockDescendants but it did not work =/. Where should I override the onInterceptTouchEvent? Should I extend the textView class and use mine instead with that new method? – KingCrimson Jan 30 '13 at 22:11

3 Answers3

3

Setting clickable property of TextView to false should solve this problem. Try this:

<TextView
    android:id="@+id/list_content"
    android:textColor="#000000"
    android:gravity="center"
    android:layout_margin="4dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:clickable="false" />
Damian Jeżewski
  • 1,246
  • 2
  • 14
  • 21
  • Thanks. It did the trick, and also, on my CustomStringAdapter, in order to leave the item selected, I had overriden the getView(). Inside, I somehow wrote a setFocusListener() for the textView so it would always "win". Thanks a lot. – KingCrimson Jan 30 '13 at 22:22
0

To make items not focusable, do this:

listView.setItemsCanFocus(false);

source

I think this will solve your issue.

ben75
  • 29,217
  • 10
  • 88
  • 134
0

Also make sure the

android:textIsSelectable 

property is not set to true.

mattgarkusha
  • 13
  • 1
  • 5