I have a “Header” at the top of 3 lists, which contains simple Create Read Update & Delete buttons for editing the list.
How should I go about subclassing the Header so I can overwrite the listener in TaskHeader, ProjectHeader, ClientsHeader (Subclasses)
seeing as the layout, button creation, assignment etc.. can all be done in the same implementaion and just assigning the listener can be delegated to the specific sub-class.
Updated with suggested solution
Interface
import android.view.View.OnClickListener;
public interface AttachClickListeners {
void attachCreateListener(OnClickListener l);
void attachReadListener(OnClickListener l);
void attachUpdateListener(OnClickListener l);
void attachDeleteListener(OnClickListener l);
}
Header
public class Header extends LinearLayout implements AttachClickListeners {
/* removed for shortness */
public void attachCreateListener(OnClickListener listener) {
insertBtn.setOnClickListener(listener);
}
public void attachReadListener(OnClickListener listener) {
selectBtn.setOnClickListener(listener);
}
public void attachUpdateListener(OnClickListener listener) {
updateBtn.setOnClickListener(listener);
}
public void attachDeleteListener(OnClickListener listener) {
deleteBtn.setOnClickListener(listener);
}
then each time I create a header I just pass a different listener object as suggested by @elijah
if the UI and behavior can be shared, you shouldn’t have to subclass the header.
Instead, create interface definitions for listener classes for the events you want to respond to. For instance, if you want to have different responses to the Create button, define a onCreateClickedListener interface, and then create particular instances of that interface for every view the shared header object exists in. You have to register the listener; something like:
This tutorial covers the topic in depth.