0

I have the following scenario: a fragment that hosts a listview. In this listview the items have several buttons. Now i need to have the clicklisteners inside my fragment. How can i achieve this?

By default using android:onClick="myListener" is searches for myListener() in the parent activity not the fragment, which is in my opinion useless if i want to encapsulate functionality for reusability in a fragment. So i need to keep the activity out of this.

As it appears to me that onclick bindings in a list view are applied to contexts and a fragment has none on its own, i'm stuck.

patman
  • 2,780
  • 4
  • 30
  • 54
  • Did you try to add the click listener dynamically in your CustomListAdapter's getView method? – Emil Sep 04 '15 at 10:20
  • Actually i'd rather circumvent this solution. Would be possible i guess, but by the default the adapter has no reference to the fragment and moreover i'd like to keep such declarations in my view. But i probably need to do or try that if there's no other solution. – patman Sep 04 '15 at 10:25
  • You could use an interface. Then you can also listen for the click event in your fragment too.. – Emil Sep 04 '15 at 10:26
  • I don't think that you have an alternate solution, but to implement it in the `getView()` method. – Emil Sep 04 '15 at 10:29
  • Probably there isn't another solution. As i pointed out in a comment to an answer i don't see the point of interfaces for this usecase as interfaces are ment for communication in between components which in my case explicitly should be the case. – patman Sep 04 '15 at 10:38
  • FYI i ended up using an interface, if you want you can post it as an answer. – patman Sep 09 '15 at 11:51

1 Answers1

1

Your list view is inside your fragment, have a look at the snippet below:

yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //do ur stuff here........
        }
    });
Kaveesh Kanwal
  • 1,753
  • 17
  • 16
  • This does not handle different buttons inside my list view items. Just clicks on the items themselves – patman Sep 04 '15 at 10:23
  • ok, so add click listener to your items where you are inflating your list items. Most probably you can do it in the getView() method of you adapter. – Kaveesh Kanwal Sep 04 '15 at 10:25
  • Thanks, see my above comment. I find such a solution "hackish" and is effectively not what i want – patman Sep 04 '15 at 10:27
  • alright, then why don't you try and use interfaces for proper communication. [Here](http://stackoverflow.com/questions/32343958/how-to-send-data-from-one-fragment-to-another-fragment-in-android/32344658#32344658) is one post that might help you. – Kaveesh Kanwal Sep 04 '15 at 10:29
  • Well, i know about interfaces. But in my understanding thats for communication in between classes. But in my case that explicitly shouldn't be the case. It is about functionality that should only reside inside the fragment and no other component should have anything to do with it. I'm really starting to doubt the concept of fragments with this limitations, at least my understanding of fragments. – patman Sep 04 '15 at 10:35