让你在开发中爱不释手的 GT 包。关注GSLS官网,查看更多源码 ヾ(✿゚▽゚)ノ工具包。
所有文章 小编尽量让读者可以 直接 读懂 与 完全 复制粘贴,其中复杂或较多 的源码 会有 源码 并 贴上 github 网址。
GT 类 里面的源码完全开源,较多的中文注释,让更多的人直接读懂。
点个关注点个赞呗(〃'▽'〃),关注博主最新发布库: https://github.com/1079374315/GSLS_Tool
美帝 框架,让创造变得如此简单!
当你依赖GT库后就可以进行以下操作了
使用 GT 注解来开发一个 多个 Fragment 之间切换的小实例
效果图: 项目目录
第一步:添加4个 fragment 的 xml 布局 与 activity_main 布局
fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F44336"
>
</LinearLayout>
fragment_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#03A9F4"
>
</LinearLayout>
fragment_3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4CAF50"
>
</LinearLayout>
fragment_4.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF9800"
tools:context=".MainActivity">
<TextView
android:id="@+id/ioc_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/ioc_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单击测试"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.603" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/ioc_tv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/ioc_btn01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:id="@+id/ioc_btn02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" />
<Button
android:id="@+id/ioc_btn03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
/>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
>
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="F1"
/>
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="F2"
/>
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="F3"
/>
<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="F4"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
第二步:添加 4 个 Fragment 类 与 编写 MainActivity 代码:
Fragment_1
public class Fragment_1 extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_1,container,false);
}
}
Fragment_2
public class Fragment_1 extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_1,container,false);
}
}
Fragment_3
public class Fragment_3 extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_3,container,false);
}
}
Fragment_4
public class Fragment_4 extends Fragment {
@GT.Annotations.GT_View(R.id.ioc_tv)
private TextView tv;
@GT.Annotations.GT_View(R.id.ioc_btn)
private Button btn;
@GT.Annotations.GT_Object(valueString = "1079", valueInt = 21, types = {GT.Annotations.GT_Object.TYPE.STRING, GT.Annotations.GT_Object.TYPE.INT}, functions = {"setUsername", "setAge"})
private LoginBean loginBean;
@GT.Annotations.GT_Res.GT_String(R.string.StringTest)
private String data;
@GT.Annotations.GT_Res.GT_Color(R.color.colorTest)
private int MyColor;
@GT.Annotations.GT_Res.GT_Drawable(R.drawable.ic_launcher_background)
private Drawable btnBG;
@GT.Annotations.GT_Res.GT_Dimen(R.dimen.tv_size)
private float TextSize;
@GT.Annotations.GT_Res.GT_Animation(R.anim.alpha)
private Animation animation;
@GT.Annotations.GT_Res.GT_StringArray(R.array.ctype)
private String[] strArray;
@GT.Annotations.GT_Res.GT_IntArray(R.array.textInt)
private int[] intArray;
@GT.Annotations.GT_Res.GT_Layout(R.layout.activity_main)
private View layout;
@GT.Annotations.GT_Collection.GT_List(valueString = {"111", "222", "333"})
private List<String> stringList;
@GT.Annotations.GT_Collection.GT_Map(valueKey = {"username","password"},valueInt = {4444,5555})
private Map<String,Integer> userMap;
@GT.Annotations.GT_Collection.GT_Set(valueInt = {11,22,33,44})
private Set<Integer> booleanSet;
private static final String TAG = "GT_";
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_4, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
GT.getGT().build(this, view);//绑定 Fragment
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "loginBean: " + loginBean);
Log.i(TAG, "data: " + data);
Log.i(TAG, "color: " + MyColor);
Log.i(TAG, "Drawable: " + btnBG);
Log.i(TAG, "TextSize: " + TextSize);
Log.i(TAG, "animation: " + animation);
Log.i(TAG, "strArray: " + strArray);
Log.i(TAG, "intArray: " + intArray);
Log.i(TAG, "layout: " + layout);
Log.i(TAG, "stringList: " + stringList);
Log.i(TAG, "userMap: " + userMap);
Log.i(TAG, "booleanSet: " + booleanSet);
tv.setText("实现成功!");
btn.setTextColor(MyColor);
btn.setTextSize(TextSize);
btn.setBackgroundDrawable(btnBG);
btn.startAnimation(animation);
}
});
}
@GT.Annotations.GT_Click({R.id.ioc_btn01, R.id.ioc_btn02, R.id.ioc_btn03})
public void testBtnOnCLick(View view) {
switch (view.getId()) {
case R.id.ioc_btn01:
Log.i(TAG, "单击 1 号");
break;
case R.id.ioc_btn02:
Log.i(TAG, "单击 2 号");
break;
case R.id.ioc_btn03:
Log.i(TAG, "单击 3 号");
break;
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
//实例化 4 个 Fragment 并注入到 List 中
@GT.Annotations.GT_Collection.GT_List({Fragment_1.class, Fragment_2.class, Fragment_3.class, Fragment_4.class})
private List<Fragment> fragmentList;
//定义 Fragment 管理器
private GT.GT_Fragment gt_fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GT.getGT().build(this);//绑定 Activity
//实例化 Fragment 管理器 并初始化要管理的 Fragment
gt_fragment = GT.GT_Fragment.getGT_fragment()
.initFragment(savedInstanceState, this, getSupportFragmentManager())
.loadFragment(R.id.frameLayout, fragmentList, 0);
}
@GT.Annotations.GT_Click({R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4})
public void onBtnClick(View view){
switch (view.getId()){
case R.id.btn1:
gt_fragment.startFragment(Fragment_1.class);//跳转到 Fragment_1 页面
break;
case R.id.btn2:
gt_fragment.startFragment(Fragment_2.class);//跳转到 Fragment_2 页面
break;
case R.id.btn3:
gt_fragment.startFragment(Fragment_3.class);//跳转到 Fragment_3 页面
break;
case R.id.btn4:
gt_fragment.startFragment(Fragment_4.class);//跳转到 Fragment_4 页面
break;
}
}
}
效果数据:
09-16 11:16:34.657 5823-5823/? I/GT_i: ------- key:username
09-16 11:16:34.657 5823-5823/? I/GT_i: ------- key:password
09-16 11:16:40.378 5823-5823/? I/GT_: 单击 1 号
09-16 11:16:41.042 5823-5823/? I/GT_: 单击 2 号
09-16 11:16:41.619 5823-5823/? I/GT_: 单击 3 号
09-16 11:16:43.061 5823-5823/? I/GT_: loginBean: LoginBean{username='1079', password='null', age=21}
09-16 11:16:43.061 5823-5823/? I/GT_: data: 测试数据
09-16 11:16:43.061 5823-5823/? I/GT_: color: -6400
09-16 11:16:43.061 5823-5823/? I/GT_: Drawable: android.graphics.drawable.VectorDrawable@2747f5a3
09-16 11:16:43.061 5823-5823/? I/GT_: TextSize: 10.0
09-16 11:16:43.061 5823-5823/? I/GT_: animation: android.view.animation.AnimationSet@58d70a0
09-16 11:16:43.061 5823-5823/? I/GT_: strArray: [Ljava.lang.String;@3149e459
09-16 11:16:43.061 5823-5823/? I/GT_: intArray: [I@35df881e
09-16 11:16:43.061 5823-5823/? I/GT_: layout: androidx.constraintlayout.widget.ConstraintLayout{30da48ff V.E..... ......I. 0,0-0,0}
09-16 11:16:43.061 5823-5823/? I/GT_: stringList: [111, 222, 333]
09-16 11:16:43.061 5823-5823/? I/GT_: userMap: {username=4444, password=5555}
09-16 11:16:43.061 5823-5823/? I/GT_: booleanSet: [22, 44, 11, 33]
总结:只用注解只要添加 绑定操作 就可以了,如:绑定 Activity 或 绑定 Fragment 即可在 Activity 与 Fragment 内使用注解,如果 GT注解 配合 GT 注解基类 那你的代码将会得到不一样的变化。
项目源码:https://github.com/1079374315/GT_Annotation