Android Open Source - Paginize View Wrapper






From Project

Back to project page Paginize.

License

The source code is released under:

Copyright (c) 2014 neevek <i at neevek.net> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal ...

If you think the Android project Paginize listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.neevek.android.lib.paginize;
//from  ww w. j a va 2  s  . co  m
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import net.neevek.android.lib.paginize.annotation.InheritPageLayout;
import net.neevek.android.lib.paginize.annotation.PageLayout;
import net.neevek.android.lib.paginize.exception.InjectFailedException;
import net.neevek.android.lib.paginize.util.AnnotationUtils;
import net.neevek.android.lib.paginize.util.ViewFinder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by neevek on 6/15/14.
 */
public abstract class ViewWrapper {
    private View mView;
    // protected variables to be accessed in subclasses.
    protected PageActivity mContext;

    public ViewWrapper(PageActivity pageActivity) {
        mContext = pageActivity;
        init();
    }

    private void init() {
        Class clazz = getClass();

        try {
            List<Class> list = new ArrayList<Class>(4);

            do {
                list.add(clazz);

                if (mView == null && clazz.isAnnotationPresent(PageLayout.class)) {
                    mView = mContext.getLayoutInflater().inflate(((PageLayout)clazz.getAnnotation(PageLayout.class)).value(), null);
                }
            } while ((clazz = clazz.getSuperclass()) != ViewWrapper.class);

            if (mView == null) {
                throw new IllegalArgumentException("Must specify a layout resource with the @PageLayout annotation on " + clazz.getName());
            }

            if (list.size() > 1) {
                // -2 because a Page with @PageLayout should not have @InheritPageLayout, which will be silently ignored.
                for (int i = list.size() - 2; i >= 0; --i) {
                    clazz = list.get(i);
                    if (clazz.isAnnotationPresent(InheritPageLayout.class)) {
                        InheritPageLayout inheritPageLayoutAnno = (InheritPageLayout)clazz.getAnnotation(InheritPageLayout.class);
                        if (inheritPageLayoutAnno.root() != -1) {
                            ViewGroup root = (ViewGroup)mView.findViewById(inheritPageLayoutAnno.root());
                            if (root == null) {
                                throw new IllegalArgumentException("The root specified in @InheritPageLayout is not found.");
                            }
                            mContext.getLayoutInflater().inflate(inheritPageLayoutAnno.value(), root, true);
                        } else {
                            mContext.getLayoutInflater().inflate(inheritPageLayoutAnno.value(), (ViewGroup)mView, true);
                        }
                    }
                }
            }

            ViewFinder viewFinder = new ViewFinder() {
                public View findViewById(int id) { return ViewWrapper.this.findViewById(id); }
            };

            for (int i = list.size() - 1; i >= 0; --i) {
                AnnotationUtils.initAnnotatedFields(list.get(i), this, viewFinder);
                AnnotationUtils.handleAnnotatedPageConstructors(list.get(i), this, viewFinder);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new InjectFailedException(e);
        }
    }

    public View getView() {
        return mView;
    }

    public View findViewById(int id) {
        return mView.findViewById(id);
    }

    protected void hideTopPage() {
        mContext.hideTopPage();
    }

    public void showPage(Class <? extends Page > pageClass, boolean animated) {
        showPage(pageClass, animated, null);
    }

    public void showPage(Class <? extends Page > pageClass, boolean animated, Object arg) {
        try {
            Page page = pageClass.getConstructor(PageActivity.class).newInstance(mContext);
            page.show(arg, animated);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isAttached() {
        return mView.getParent() != null;
    }

    /** called when added to the view hierarchy of the host activity **/
    public void onAttach() {}

    /** called when removed from the view hierarchy of the host activity **/
    public void onDetach() {}

    /** onBackPressed mirrors Activity.onBackPressed, only the current page(page on the top of the stack) receives this call **/
    public boolean onBackPressed() { return false; }

    /** onActivityResult mirrors Activity.onActivityResult, only the current page(page on the top of the stack) receives this call **/
    public void onActivityResult(int requestCode, int resultCode, Intent data) {  }

    /** onPause mirrors Activity.onPause, only the current page(page on the top of the stack) receives this call **/
    public void onPause() { }

    /** onResume mirrors Activity.onResume, only the current page(page on the top of the stack) receives this call **/
    public void onResume() { }

    /** onShown is called after the page is pushed on the page stack **/
    public void onShown(Object arg) { }

    /** onHidden is called after the page is popped out of the page stack **/
    public void onHidden() { }

    /** onCovered is called for the current page when a new page is pushed on the page stack **/
    public void onCovered() { }

    /** onUncovered is called for the previous page when the current page is popped out of the page stack **/
    public void onUncovered(Object arg) { }
}




Java Source Code List

net.neevek.android.demo.paginize.activities.MainActivity.java
net.neevek.android.demo.paginize.pages.general.FrameInnerPage.java
net.neevek.android.demo.paginize.pages.general.FramePage.java
net.neevek.android.demo.paginize.pages.main.MainPage.java
net.neevek.android.demo.paginize.pages.main.TabPage1.java
net.neevek.android.demo.paginize.pages.main.TabPage2.java
net.neevek.android.demo.paginize.pages.other.AlertPage.java
net.neevek.android.demo.paginize.pages.other.ListItemPage.java
net.neevek.android.demo.paginize.pages.other.ListPage.java
net.neevek.android.demo.paginize.pages.other.TestPage.java
net.neevek.android.demo.paginize.pages.viewpager.MyViewPagerPage.java
net.neevek.android.demo.paginize.pages.viewpager.ViewPageSubPage1.java
net.neevek.android.demo.paginize.pages.viewpager.ViewPageSubPage2.java
net.neevek.android.lib.paginize.InnerPageContainer.java
net.neevek.android.lib.paginize.InnerPageManager.java
net.neevek.android.lib.paginize.InnerPage.java
net.neevek.android.lib.paginize.PageActivity.java
net.neevek.android.lib.paginize.PageDataCallback.java
net.neevek.android.lib.paginize.PageManager.java
net.neevek.android.lib.paginize.PagePagerAdapter.java
net.neevek.android.lib.paginize.Page.java
net.neevek.android.lib.paginize.ViewPagerInnerPage.java
net.neevek.android.lib.paginize.ViewPagerPage.java
net.neevek.android.lib.paginize.ViewWrapper.java
net.neevek.android.lib.paginize.anim.PageAnimator.java
net.neevek.android.lib.paginize.anim.SlidePageAnimator.java
net.neevek.android.lib.paginize.anim.ZoomPageAnimator.java
net.neevek.android.lib.paginize.annotation.DecoratePageConstructor.java
net.neevek.android.lib.paginize.annotation.InheritPageLayout.java
net.neevek.android.lib.paginize.annotation.InjectPageAnimator.java
net.neevek.android.lib.paginize.annotation.InjectView.java
net.neevek.android.lib.paginize.annotation.InnerPageContainerLayoutResId.java
net.neevek.android.lib.paginize.annotation.PageLayout.java
net.neevek.android.lib.paginize.annotation.SetListeners.java
net.neevek.android.lib.paginize.annotation.ViewPagerResId.java
net.neevek.android.lib.paginize.exception.InjectFailedException.java
net.neevek.android.lib.paginize.exception.NotImplementedInterfaceException.java
net.neevek.android.lib.paginize.util.AnnotationUtils.java
net.neevek.android.lib.paginize.util.ViewFinder.java