Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnItemClickListener on a ListView inside a Fragment not working

EDIT: SOLVED. If there's anything focusable in the XML of the items, it will break the touch of the list, in other words, android:focusable=false to all the checkboxes, switches or anything like that of ur list. And done =)

Ok so, here's my problem.

I wrote a app that uses tabs and fragments, and it all goes the way I want except for the thing that when I try to capture a onItemClick on a listView it does not even mark the row as touched/pressed/selected.

I've been reading a little bit about and many people have the same issue, but I did not found any responses that helped me at all.

I don't want to implement a ListFragment, in fact I don't even know how/why I should, and since all my code is already working, I don't know if implementing one will give me much more work to do, so, here it is:

Is it possible to implement a listener for a click on a listView, inside a fragment? and if it is, HOW?

PD: minSDK=14, tatgetSDK=15

like image 552
Dunnow Avatar asked Feb 15 '13 17:02

Dunnow


2 Answers

Just put

android:focusable="false"
android:clickable="false"

in layout. For all textviews,buttons etc.

like image 165
Ashwin S Ashok Avatar answered Oct 10 '22 21:10

Ashwin S Ashok


Here's a code snippet that'll do what you want.

ListView lv;

//code to get the listView instance using findViewByID etc

lv.setOnItemClickListener(new OnItemClickListener()
{
    @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        Toast.makeText(EnclosingActivity.this, "Stop Clicking me", Toast.LENGTH_SHORT).show();
    }
});

People usually trip on this, see if you have got this covered:

All clicks and call backs (eg: the menu/actionbar callbacks) are sent to the activity the fragment is bound to, so they must be in the activity class and not the fragment class.

like image 25
Dheeraj Bhaskar Avatar answered Oct 10 '22 21:10

Dheeraj Bhaskar