ViewPump, 查看通货膨胀你可以拦截

分享于 

4分钟阅读

GitHub

  繁體 雙語
View Inflation you can intercept.
  • 源代码名称:ViewPump
  • 源代码网址:http://www.github.com/InflationX/ViewPump
  • ViewPump源代码文档
  • ViewPump源代码下载
  • Git URL:
    git://www.github.com/InflationX/ViewPump.git
    Git Clone代码到本地:
    git clone http://www.github.com/InflationX/ViewPump
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/InflationX/ViewPump
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    ViewPump

    查看通货膨胀你可以拦截。

    ViewPump通过ContextThemeWrapper安装自定义 LayoutInflater,并提供预/后通货膨胀拦截器的API。

    启动

    依赖项

    包含依赖项下载(. aar ) 插件:

    dependencies {
     compile 'io.github.inflationx:viewpump:1.0.0'}

    用法

    定义你的拦截器。下面是一个有点任意的例子,这些例子是一个前缀。

    publicclassTextUpdatingInterceptorimplementsInterceptor {
     @OverridepublicInflateResultintercept(Chainchain) {
     InflateResult result = chain.proceed(chain.request());
     if (result.view() instanceofTextView) {
     // Do something to result.view()// You have access to result.context() and result.attrs()TextView textView = (TextView) result.view();
     textView.setText("[Prefix] "+ textView.getText());
     }
     return result;
     }
    }

    下面是一个例子,在布局的XML中定义了时返回一个 CustomTextView。

    publicclassCustomTextViewInterceptorimplementsInterceptor {
     @OverridepublicInflateResultintercept(Chainchain) {
     InflateRequest request = chain.request();
     View view = inflateView(request.name(), request.context(), request.attrs());
     if (view !=null) {
     returnInflateResult.builder()
    . view(view)
    . name(view.getClass().getName())
    . context(request.context())
    . attrs(request.attrs())
    . build();
     } else {
     return chain.proceed(request);
     }
     }
    }

    安装

    将拦截器添加到 ViewPump.builder() 中,在 #onCreate() 方法中的Application 类中,并在 init 中添加 ViewPump。 拦截器的顺序非常重要,因为它们形成了请求和结果的拦截器链。

    拦截器可以选择返回以编程方式实例化的视图而不是允许缺省的膨胀。 为这里,最好在前面的货币截止截止之前添加你的邮件截止截止器。

    @Overridepublicvoid onCreate() {
     super.onCreate();
     ViewPump.init(ViewPump.builder()
    . addInterceptor(newTextUpdatingInterceptor())
    . addInterceptor(newCustomTextViewInterceptor())
    . build());
     //....}

    注入到上下文中

    包装 Activity 上下文:

    @Overrideprotectedvoid attachBaseContext(Context newBase) {
     super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
    }

    ,你 are !

    要查看关于潜在用例的更多想法,请查看页面 wiki。

    协作者许可证
    
    Copyright 2017 InflationX
    
    
    
    Licensed under the Apache License, Version 2.0 (the"License");
    
    
    you may not use this file except in compliance with the License.
    
    
    You may obtain a copy of the License at
    
    
    
     http://www.apache.org/licenses/LICENSE-2.0
    
    
    
    Unless required by applicable law or agreed to in writing, software
    
    
    distributed under the License is distributed on an"AS IS" BASIS,
    
    
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    
    
    See the License for the specific language governing permissions and
    
    
    limitations under the License.
    
    
    
    

    INT  VIEW  INF  Intercept  
    相关文章