美文网首页
第二单元

第二单元

作者: 贤兵 | 来源:发表于2020-12-07 02:08 被阅读0次

    一、模块以及依赖

    1、模块分类

    应用:apply plugin: 'com.android.application'

    库:apply plugin: 'com.android.library

    区别是,application是能直接运行的

    2、依赖分类:

    implementation:添加到编译路径,并将依赖打包到apk。依赖不传递

    api:和之前的compile一样,添加到编译依赖,并将依赖打包到apk。依赖传递

    compileOnly:只参加编译不打包到apk

    runtimeOnly:不参加依赖,但是打包到依赖

    annotationProcessor:检测源代码,找出Annotation,根据注解自动生成代码

    二、ARouter

    1、什么是ARouter

    A framework for assisting in the renovation of Android componentization (帮助 Android App 进行组件化改造的路由框架)

    https://github.com/alibaba/ARouter

    2、ARouter的使用

    1、新建工程ARouter Demo

    2、新建module: library1

    3、在library1中新建Activity: Library1Activity

    4、在library1中新建layout:activity_library1

    5、在library1的manifest中配置Library1Activity

    6、在library1的gradle的android的defaultConfig中配置:

    javaCompileOptions {

        annotationProcessorOptions {

            arguments = [ AROUTER_MODULE_NAME : project.getName() ]

        }

    }

    7、在library1的gradle的android的dependencies中配置:

    implementation 'com.alibaba:arouter-api:1.5.1'

    annotationProcessor 'com.alibaba:arouter-compiler:1.5.1'

    8、Library1Activity配置:

    @Route(path = "/xianbing/Library1Activity")

    9、新建Application:App

    10、App的onCreate中添加:

    ARouter.openLog();

    ARouter.openDebug();

    ARouter.init(this);

    在manifest中配置好

    11、回到主app,主app需要依赖library1

    12、在MainActivity中添加点击事件

    13、在gradle中同样配置

    javaCompileOptions {

        annotationProcessorOptions {

            arguments = [ AROUTER_MODULE_NAME : project.getName() ]

        }

    }

    以及:

    implementation 'com.alibaba:arouter-api:1.5.1'

    annotationProcessor 'com.alibaba:arouter-compiler:1.5.1'

    14、在MainActivity中添加点击事件

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){

        @Override

        public void onClick(View v) {

            ARouter.getInstance().build("/xianbing/Library1Activity").navigation();

        }

    });

    15、新建SecondActivity,manifest中配置

    16、给SecondActivity添加@Route(path="/xianbing/SecondActivity")

    17、在主activiyt中添加新的btn以及事件

    findViewById(R.id.btn_with_param).setOnClickListener(new OnClickListener(){

        @Override

        public void onClick(View v) {

            ARouter.getInstance().build("/xianbing/SecondActivity").withString("name", "xianbing").navigation();

        }

    });

    18、转场动画
    fade_in.xml

    <?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator"    android:fromAlpha="0.0" android:toAlpha="1.0"    android:duration="3000" />

    fade_out.xml

    <?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator"    android:fromAlpha="1.0" android:toAlpha="0.0"    android:duration="3000" />

    ARouter.getInstance().build("/xianbing/SecondActivity").withTransition(R.anim.fade_in, R.anim.fade_out).withString("name", "xianbing").navigation(MainActivity.this);

    19、Fragment

    @Route(path="/xianbing/DemoFragment")

    public class DemoFragment extends Fragment { 

    @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_demo, container, false);

    return view;

    }

    }

    findViewById(R.id.find_fragment).setOnClickListener(new View.OnClickListener(){

    @Override public void onClick(View v) {

    Fragment fragment = (Fragment) ARouter.getInstance().build("/xianbing/DemoFragment").navigation();

    FragmentManager fragmentManager = getFragmentManager();

    FragmentTransaction transaction = fragmentManager.beginTransaction();

    transaction.add(R.id.fragment_parent, fragment);

    transaction.commit();

    }

    }

    );}

    20、autowired需要

    ARouter.getInstance().inject(this);

    21、模块依赖

    implementation project(':library')

    22、app初始化以及在manifest中配置

    23、拦截器

    @Interceptor( priority =1)

    public class LoginInterceptor implements IInterceptor {

    @Override

        public void process(Postcard postcard,InterceptorCallback callback) {

    Log.i("Simon","LoginInterceptor process");

    if (isLogined()) {

    callback.onContinue(postcard);

    }

    else {

    if (postcard.getPath().equalsIgnoreCase("/xianbing/DemoActivity")) {

    callback.onInterrupt(new RuntimeException(""));

    }

    else {

    callback.onContinue(postcard);

    }

    }

    }

    @Override

        public void init(Context context) {

    }

    public boolean isLogined() {

    return true;

    }

    }

    相关文章

      网友评论

          本文标题:第二单元

          本文链接:https://www.haomeiwen.com/subject/gvqzwktx.html