Skip to content

Instantly share code, notes, and snippets.

@hector6872
Last active November 29, 2024 12:05
Show Gist options
  • Select an option

  • Save hector6872/d197c5c9b37fd3528072d192b007928e to your computer and use it in GitHub Desktop.

Select an option

Save hector6872/d197c5c9b37fd3528072d192b007928e to your computer and use it in GitHub Desktop.
DRMVPFrameLayoutView example for DaRealMVP library - https://github.com/hector6872/DaRealMVP
public abstract class DRMVPFrameLayoutView<P extends DRMVPPresenter<V>, V extends DRMVPView> extends FrameLayout {
private P presenter;
public DRMVPFrameLayoutView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DRMVPFrameLayoutView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@SuppressWarnings("unchecked") private void init() {
presenter = DRMVPUtils.getDeclaredPresenter(getClass());
presenter.bind((V) this);
inflate(getContext(), getLayoutResId(), this);
}
@CallSuper @Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
presenter.unbind();
}
protected @NonNull P getPresenter() {
return presenter;
}
protected abstract @LayoutRes int getLayoutResId();
}
public class DRMVPUtils {
@SuppressWarnings("unchecked") public static <P> P getDeclaredPresenter(@NonNull Class clazz) {
Type genericSuperclass;
for (; ; ) {
genericSuperclass = clazz.getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType) {
break;
}
clazz = clazz.getSuperclass();
}
Type presenterClass = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
try {
return (P) Class.forName(presenterClass.toString().split(" ")[1]).newInstance();
} catch (java.lang.InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new IllegalArgumentException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment