一、电商项目笔记【架构设计与模块分解+单Activity界面架构设计及验证】

  • 模块化开发

在这里插入图片描述

  • List item

在这里插入图片描述

在这里插入图片描述

  • 代码生成器model
    从注解获取信息,通过annotationProcessor或apt生成代码
    家里项目使用annotationProcesso
  • 核心model

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • 分别建立各模块
    atte-annotations
    latte-compiler 编译模块
    latte-core 核心库
    latte-ec 电商模块

  • 添加依赖
    latte-core依赖于latte-annotations
    latte-ec 依赖于latte-core
    app依赖于latte-ec,latte-compiler

  • 一:开始编写项目 :项目架构初始化
    1、latte-core
    笔记:
    WeakHashMap:里面的键值对在不使用的时候会自动回收,而且非常及时,避免内存爆满

代码:

  • Configurator.class
/**
 * Created  by  liusihui  on  2019/11/05
 * 模块功能:配置文件的存储以及获取
 **/

package com.lsh.latte_core.app;

import java.util.WeakHashMap;

public class Configurator {
    private static final WeakHashMap<String,Object> LATTE_CONFIGS=new WeakHashMap<>();
    private Configurator(){
        LATTE_CONFIGS.put(ConfigType.CONFIG_READY.name(),false); //配置开始了,但是没有完成
    }
    //线程安全的懒汉模式
    public static  Configurator getInstance(){
        return Holder.INSTANCE;
    }

    //直接返回
    final WeakHashMap<String,Object> getLatteConfigs(){
        return LATTE_CONFIGS;
    }

    //静态内部类单例模式的初始化
    private static class Holder{
        private static final Configurator INSTANCE=new Configurator();
    }
    public final void configure(){
        LATTE_CONFIGS.put(ConfigType.CONFIG_READY.name(),true);//配置开始了,并且已经完成
    }

    //开始实例化API—HOST
    public final Configurator withApiHost(String host){
        LATTE_CONFIGS.put(ConfigType.API_HOST.name(),host);
        return this;
    }

/*
*
* 检查配置是否正确
* 设置为不可更改
*
*
* 在应用程序中获取调用
* */
    private void checkConfiguration(){
        final boolean isReady=(boolean)LATTE_CONFIGS.get(ConfigType.CONFIG_READY.name());
        //如果配置并没有完成,抛出一个运行时异常
        if (!isReady){
            throw new RuntimeException("Configuration id not ready,call configure");
        }
   }

   /*
   * @SuppressWarnings("unchecked") 没有检测过的
   * */
@SuppressWarnings("unchecked")
   final <T> T getConfiguration(Enum<ConfigType> key){
        checkConfiguration();
        return (T) LATTE_CONFIGS.get(key);

   }

}
  • ConfigType
package com.lsh.latte_core.app;
/*
* 枚举类:在整个应用程序里是唯一的单例,也只能初始化一次
*
* 要在进行多线程操作的时候,完全可以用枚举类惰性单例的初始化(线程安全的懒汉模式哈哈哈哈)
* */
public enum ConfigType {
    API_HOST,    //网络请求域名
    APPLICATION_CONTEXT,   //全局的上下文
    CONFIG_READY,   //控制初始化或配置是否完成
    ICON     //存储自己的初始化项目

}
  • Latte
    /**
  • Created by liusihui on 2019/11/05
  • 模块功能:
    **/
package com.lsh.latte_core.app;

import android.content.Context;

import java.util.WeakHashMap;

public final  class Latte {

    public static Configurator init(Context context){
        getConfigurations().put(ConfigType.APPLICATION_CONTEXT.name(),context.getApplicationContext());
        return Configurator.getInstance();
    }

    private static WeakHashMap<String,Object> getConfigurations(){
        return Configurator.getInstance().getLatteConfigs();
    }
}

在这里插入图片描述

2、APP

  • ExampleApp
/**
 * Created  by  liusihui  on  2019/11/05
 * 模块功能:
 **/

package com.lsh.fastec;

import com.lsh.latte_core.app.Latte;
import android.app.Application;

public class ExampleApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Latte.init(this)
                .withApiHost("http://localhost:8080")
                .configure();
    }
}
  • 二:集成第三方字体图标库iconify
    由于项目中会用到很多图片,图片过多或者过大会导致项目较大,哈哈哈哈但是!iconify可以解决这个问题!那么甩链接
    Github地址:添加链接描述

代码:
1、添加依赖

//字体图标
    implementation 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.2' // (v4.5)
    implementation 'com.joanzapata.iconify:android-iconify-entypo:2.2.2' // (v3,2015)
    implementation 'com.joanzapata.iconify:android-iconify-typicons:2.2.2' // (v2.0.7)
    implementation 'com.joanzapata.iconify:android-iconify-material:2.2.2' // (v2.0.0)
    implementation 'com.joanzapata.iconify:android-iconify-material-community:2.2.2' // (v1.4.57)
    implementation 'com.joanzapata.iconify:android-iconify-meteocons:2.2.2' // (latest)
    implementation 'com.joanzapata.iconify:android-iconify-weathericons:2.2.2' // (v2.0)
    implementation 'com.joanzapata.iconify:android-iconify-simplelineicons:2.2.2' // (v1.0.0)
    implementation 'com.joanzapata.iconify:android-iconify-ionicons:2.2.2' // (v2.0.1)

2、在package com.lsh.latte_core.app.Configurator初始化

 //封装下字体图标iconify
    private static final ArrayList<IconFontDescriptor> ICONS=new ArrayList<>();
     //初始化字体图标
    private void initIcons(){
        if (ICONS.size()>0){
            final Iconify.IconifyInitializer initializer=Iconify.with(ICONS.get(0));

            for (int i = 0; i <ICONS.size() ; i++) {
                initializer.with(ICONS.get(i));

            }
        }
    }


  /*
   * 加入自己的字体图标
   * */
   public final Configurator withIcon(IconFontDescriptor descriptor){
       ICONS.add(descriptor);
       return this;
   }

3、在APP中调用

 public void onCreate() {
        super.onCreate();
        Latte.init(this)
                .withIcon(new FontAwesomeModule())
                .withApiHost("http://10.10.110.38/")
                .configure();
    }

4、定义自己的图标字体:如阿里巴巴矢量图标
操作步骤:
然后在latte-ec中创建assets,将iconfont.ttf放入

代码:
1、package com.lsh.latte.ec.icon.FontRcMoudle
/**

  • Created by liusihui on 2019/11/05
  • 模块功能:
    **/
package com.lsh.latte.ec.icon;

import com.joanzapata.iconify.Icon;
import com.joanzapata.iconify.IconFontDescriptor;

public class FontRcMoudle implements IconFontDescriptor {
    @Override
    public String ttfFileName() {
        return "iconfont.ttf";
    }

    @Override
    public Icon[] characters() {
        return EcIcons.values();
    }
}

2、创建枚举类EcIcons

/**
 * Created  by  liusihui  on  2019/11/05
 * 模块功能:
 **/

package com.lsh.latte.ec.icon;

import com.joanzapata.iconify.Icon;

public enum EcIcons implements Icon {
    //扫描二维码和支付的字体
    icon_scan('\ue606'),
    icon_ali_pay('\ue606');


    private char character;

    EcIcons(char character){
        this.character=character;
    }

    @Override
    public String key() {
        return name().replace('_','-');
    }

    @Override
    public char character() {
        return character;
    }
}

3、Activity初始化

/**
 * Created  by  liusihui  on  2019/11/05
 * 模块功能:
 **/

package com.lsh.fastec;

import com.joanzapata.iconify.fonts.FontAwesomeModule;
import com.lsh.latte.ec.icon.FontRcMoudle;
import com.lsh.latte_core.app.Latte;
import android.app.Application;

public class ExampleApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Latte.init(this)
                .withIcon(new FontAwesomeModule())
                .withIcon(new FontRcMoudle())
                .withApiHost("http://10.10.110.38/")
                .configure();
    }
}
  • 集成fragment依赖、butterkniffer黄油刀

Fragmentation
butterknife

 //fragment依赖
    implementation 'me.yokeyword:fragmentation:0.10.1'
    implementation 'me.yokeyword:fragmentation-swipeback:0.7.9'

代码实现:
1、package com.lsh.latte_core.delegates;BaseDelegate

/**

  • Created by liusihui on 2019/11/05
  • 模块功能:基础的Delegate
  • 不希望以后的使用者创造实例,所以abstract整一个抽象类
    **/
package com.lsh.latte_core.delegates;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

public abstract class BaseDelegate extends SwipeBackFragment {

/*
* Unbinder 是黄油刀的一个类型
* */
   
    private Unbinder mUnbinder=null;

    public abstract void onBindView(@Nullable Bundle savedInstanceState,View rootView);

    /*
    * 下面是写框架的技巧
    * */
    public abstract Object setLayout();

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

        View rootView=null;
       //那么 现在就要开始我们的框架了!!!
        if (setLayout() instanceof Integer){    //如果返回的是setLayout的id
            rootView=inflater.inflate((Integer) setLayout(),container,false);
        }else if (setLayout() instanceof View){
            rootView=(View) setLayout();
        }
        if (rootView!=null){
            mUnbinder= ButterKnife.bind(this,rootView);
            onBindView(savedInstanceState,rootView);
        }


        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder!=null){
            mUnbinder.unbind();  //解除绑定
        }
    }
}

2、package com.lsh.fastec.ExanpleDelegate

/**
 * Created  by  liusihui  on  2019/11/05
 * 模块功能:
 **/

package com.lsh.fastec;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.lsh.latte_core.delegates.LatteDelegate;

public class ExanpleDelegate extends LatteDelegate {
   
    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_example;
    }
}

3、ExampleActivity

package com.lsh.fastec;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.lsh.latte_core.activities.ProxyActivity;
import com.lsh.latte_core.delegates.LatteDelegate;

public class ExampleActivity extends ProxyActivity {

    @Override
    public LatteDelegate setRootDelegate() {
        return new ExanpleDelegate();
    }



}

4、delegate_example.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">

    <TextView
        android:text="example delegate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
A powerful library that manage Fragment for Android!为"Activity + 多Fragment","多模块Activity 多Fragment"架构而生,简化开发,轻松解决动画、嵌套、事务相关等问题。为了更好的使用和了解该库,推荐阅读下面的文章:Fragment全解析系列(一):那些年踩过的坑Fragment全解析系列(二):正确的使用姿势Demo演示:均为Activity 多Fragment,第一个为简流式demo,第二个为仿微信交互的demo(全页面支持滑动退出),第三个为仿知乎交互的复杂嵌套demo下载APK   特性1、可以快速开发出各种嵌套设计的Fragment App2、悬浮球/摇一摇实时查看Fragment的栈视图Dialog,降低开发难度3、增加启动模式、startForResult等类似Activity方法4、类似Android事件分发机制的Fragment回退方法:onBackPressedSupport(),轻松为每个Fragment实现Back按键事件5、提供onSupportVisible()等生命周期方法,简化嵌套Fragment的开发过程; 提供统一的onLazyInitView()懒加载方法6、提供 Fragment转场动画 系列解决方案,动态更换动画7、提供Activity作用域的EventBus辅助类,Fragment通信更简、独立(需要使用EventBusActivityScope库)8、支持SwipeBack滑动边缘退出(需要使用Fragmentation_SwipeBack库)      如何使用1. 项目下app的build.gradle中依赖:// appcompat-v7包是必须的,v1.1.9兼容v4-27.0.0 compile 'me.yokeyword:fragmentation:1.1.9' // 如果不想继承SupportActivity/Fragment,自己定制Support,可仅依赖: // compile 'me.yokeyword:fragmentation-core:1.1.9' // 如果想使用SwipeBack 滑动边缘退出Fragment/Activity功能,完整的添加规则如下: compile 'me.yokeyword:fragmentation:1.1.9' // swipeback基于fragmentation, 如果是自定制SupportActivity/Fragment,则参照SwipeBackActivity/Fragment实现即可 compile 'me.yokeyword:fragmentation-swipeback:1.1.9' // Activity作用域的EventBus,更安全,可有效避免after onSavenInstanceState()异常 compile 'me.yokeyword:eventbus-activity-scope:1.1.0' // Your EventBus's version compile 'org.greenrobot:eventbus:{version}'2. Activity继承SupportActivity:// v1.0.0开始,不强制继承SupportActivity,可使用接口+委托形式来实现自己的SupportActivity public class MainActivity extends SupportActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(...);         // 建议在Application里初始化         Fragmentation.builder()              // 显示悬浮球 ; 其他Mode:SHAKE: 摇一摇唤出   NONE:隐藏              .stackViewMode(Fragmentation.BUBBLE)              .debug(BuildConfig.DEBUG)              ... // 更多查看wiki或demo              .install();         if (findFragment(HomeFragment.class) == null)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值