Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseSkinActivity#onCreateView中为什么用反射来创建 view? #9

Open
KennethYo opened this issue Feb 17, 2016 · 2 comments
Open

Comments

@KennethYo
Copy link

 try
        {
            //public View createView
            // (View parent, final String name, @NonNull Context context, @NonNull AttributeSet attrs)
            if (sCreateViewMethod == null)
            {
                Method methodOnCreateView = delegate.getClass().getMethod("createView", sCreateViewSignature);
                sCreateViewMethod = methodOnCreateView;
            }
            Object object = sCreateViewMethod.invoke(delegate, parent, name, context, attrs);
            view = (View) object;
        } catch (NoSuchMethodException e)
        {
            e.printStackTrace();
        } catch (InvocationTargetException e)
        {
            e.printStackTrace();
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }

这里的代码为什么用发射创建而不是用 直接调用delegate.createView 呢?如下

 view = delegate.createView(parent, name, context, attrs);
@fishwjy
Copy link

fishwjy commented May 31, 2016

我猜测是作者怕哪个版本的createView方法变成private,就不能调用了,用反射保险一些
不知道猜的对不对,请HongYang兄解惑一下:)

@zhengshiteng
Copy link

搞不懂为什么要实现createView这个部分,应该只需要解析attrs就好了。createView还是由父类完成就好了?
@OverRide
public View onCreateView(View parent, String name, Context context, AttributeSet attrs)
{
View view = super.onCreateView(parent, name, context, attrs);
List skinAttrList = SkinAttrSupport.getSkinAttrs(attrs, context);
if (skinAttrList.isEmpty())
{
return view;
}
if (view == null)
{
view = createViewFromTag(context, name, attrs);
}
injectSkin(view, skinAttrList);
return view;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants