What is the right way to communicate from a custom

2019-01-18 04:48发布

问题:

I have a custom View class that extends Spinner. I'm trying to figure out what the correct way to talk to the Activity that it's embedded in is, when the user makes a selection. I see that the OnItemSelected listener gets a reference to the Adapter, but I'm not clear on whether or not I should be using this adapter and walking up its parent chain somehow, or if I should just talk directly to the context (for some reason that doesn't feel safe, even though I can't think of a way in which it might fail, offhand).

回答1:

the right way to do that, is to "listen" to your custom view by exposing an interface which your view holding a reference to instance of him, and you hosting activity should implement. exactly like the OnItemSelected interface and any events which android views are exposing is been implemented. this is the observer design pattern.

for example:

public class MyCustomSpinner extends Spinner {
public MyCustomSpinner(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public interface IMyEventListener {
    public void onEventOccurred();
}

private IMyEventListener mEventListener;

public void setEventListener(IMyEventListener mEventListener) {
    this.mEventListener = mEventListener;
}

protected void someMethodWhichDoingSomthingAndShouldRaiseAlsoTheEvent() {

    /*
     * Some Code which the function doing //more code...
     */

    if (mEventListener != null) {
        mEventListener.onEventOccurred();
    }
}
}

this is how you will use it from your activity:

            mMyCustomSpinner.setEventListener(new IMyEventListener() {

                @Override
                public void onEventOccurred() {
                    // TODO Auto-generated method stub

                }
            });


回答2:

I'm trying to figure out what the correct way to talk to the Activity that it's embedded in is, when the user makes a selection.

You don't want to "talk to the Activity that it's embedded in". You want to talk to the controller responsible for the View. Today, that might be an Activity. Tomorrow, that might be a Fragment.

I see that the OnItemSelected listener gets a reference to the Adapter, but I'm not clear on whether or not I should be using this adapter and walking up its parent chain somehow

That implies that the View knows the specific type of Adapter, since the Adapter interface does not have any sort of getContext() method. Moreover, it ties you to talking to the Activity, which is not a good plan at this point, as mentioned above.

Personally, I'm a bit dubious about having a custom Spinner subclass in the first place. But, assuming there's a good reason for it, you should follow Tal Kanel's advice (posted while I was writing this) and design a custom listener interface for this custom event that is being declared by your custom View. Have the controller (Activity or Fragment) supply an implementation of that interface -- this could be directly implemented on the controller, or implemented as an anonymous inner class (as in Tal Kanel's answer), etc. Have your custom View call method(s) on the listener interface as needed.



回答3:

A simple solution -

((ParentClass) context).functionToRun();

Where ParentClass is the class name of the activity.



回答4:

The correct way is using a listener of some sort. I think you can make direct reference, your code would just not be reusable for another project then...