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

關於作者

譚志龍

開源項目

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.
發佈了6 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章