Last active
November 29, 2024 12:05
-
-
Save hector6872/d197c5c9b37fd3528072d192b007928e to your computer and use it in GitHub Desktop.
DRMVPFrameLayoutView example for DaRealMVP library - https://github.com/hector6872/DaRealMVP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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