FragmentKey一款解决使用newInstance创建fragment定义key传值问题的apt框架

FragmentKey一款解决使用newInstance创建fragment定义key传值问题的apt框架

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S6qnaKW5-1579241449782)(https://jitpack.io/v/TanZhiL/FragmentKey.svg)]

更新日志:

v1.0.0 2020.1.17
  • 第一次发布
使用前:
   public TFragment newInstance(String username, String password, int age) {
        TFragment tFragment = new TFragment();
//传值
        Bundle bundle = new Bundle();
        bundle.putString("username", username);
        bundle.putString("password", password);
        bundle.putInt("age", age);
        tFragment.setArguments(bundle);
        return tFragment;
    }
	
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
		//使用
        Bundle arguments = getArguments();
        mUsername = arguments.getString("username");
        mPassword = arguments.getString("password");
        age = arguments.getInt("age");
    }
使用后:
	//定义
    @Inject
    public String mUsername;
    @Inject(name = "password1")
    public String mPassword;
    @Inject
    public int age;
	//传值
     TFragment2 tFragment = new TFragment2Key().send("姓名", "密码", 10);
	  
	@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		//使用
        Log.d(TAG, mUsername);
        Log.d(TAG, mPassword);
        Log.d(TAG, String.valueOf(age));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

可以看出此框架简化了传值过程,避免了使用key来传递数据带来的麻烦.

Installation:

1.project.gradle

    buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

2.app.gradle 添加

dependencies {
   implementation 'com.github.TanZhiL.FragmentKey:fragmentkey:1.0.2'
    annotationProcessor 'com.github.TanZhiL.FragmentKey:fragmentkey-compiler:1.0.2'
}

Usage:

1.在需要外部传递的字段上加上Inject注解,然后build

   @Inject
    public String mUsername;
	//name为自定义key值,默认为字段名
    @Inject(name = "password1")
    public String mPassword;
    @Inject
    public int age;

2.创建fragment实例时使用xxxKey.send(…)方法;xxxKey类由apt自动生成.

      TFragment2 tFragment = new TFragment2Key().send("姓名", "密码", 10);

##注意:
目前以支持bundle能传递的常见类型字段

   @Inject
    protected String s;
    @Inject
    protected Integer i;
    @Inject
    protected boolean b;
    @Inject
    protected float f;
    @Inject
    protected double d;
    @Inject
    protected long l;
    @Inject
    protected ArrayList<String> ls;
    @Inject
    protected ArrayList<Integer> li;
    @Inject
    protected ArrayList<Parcelable> lp;
    @Inject
    protected Serializable se;
    @Inject
    protected Parcelable p;

致谢

  • 感谢所有开源库的大佬
  • 借鉴大佬 https://github.com/JakeWharton/butterknife

问题反馈

欢迎加星,打call https://github.com/TanZhiL/FragmentKey

  • email:1071931588@qq.com

关于作者

谭志龙

开源项目

License

Copyright (C)  tanzhilong FragmentKey Open Source Project

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值