Android单Activity页面架构使用

这里我就不废话了,只是说一下,这里对于这个单Activity框架的使用只是简单的说明一下,相关比如参数传递和其他一些功能的实现,大家可以上github查询一下。

  1. 引入框架
  • 引入的框架包括Fragmentation和ButterKnife
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'me.yokeyword:fragmentation:1.3.6'
implementation 'me.yokeyword:fragmentation-swipeback:1.3.6'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
  • 项目的build.gradle中
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
  1. 创建ProxyActivity,代码如下
/**
 * Activity基类继承
 */
public abstract class ProxyActivity extends SupportActivity {

    public abstract LatteDelegate setRootDelegate();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContainer(savedInstanceState);
    }

    private void initContainer(Bundle savedInstanceState) {
        @SuppressLint("RestrictedApi") final ContentFrameLayout container = new ContentFrameLayout(this);
        container.setId(R.id.delegate_container);
        setContentView(container);
        //将该ContentFrameLayout作为视图布局
        if (savedInstanceState == null) {
            loadRootFragment(R.id.delegate_container, setRootDelegate());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.gc();
        System.runFinalization();
    }

    /**
     * 切换页面
     * @param myDelegate
     */
    public void cutFragment(LatteDelegate myDelegate){
        replaceFragment(myDelegate,true);
    }
}
  1. 其中使用了value -> ids,资源文件如下

    

  1. 创建页面布局,代码如下


    

  1. Fragment第一层基类封装
public abstract class BaseDelegate extends SwipeBackFragment{

    @SuppressWarnings("SpellCheckingInspection")
    private Unbinder mUnbinder = null;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View rootView = null;
        if (setLayout() instanceof Integer){
            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;
    }

    //设置布局,既可以返回layoutId,也可以返回一个View
    public abstract Object setLayout();

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnbinder!=null){
            mUnbinder.unbind();
        }
    }
}
  1. Fragment第二层封装
/**
 * Fragment第二层基类封装,进行相关权限的处理
 */
public abstract class PermissionCheckerDelegate extends BaseDelegate{

}
  1. Fragment第三层封装
/**
 * Fragment第三层基类封装,该类为正式使用的基类
 */
public abstract class LatteDelegate extends PermissionCheckerDelegate{

}

8.使用举例

public class ExampleActivity extends ProxyActivity{

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

public class ExampleDelegate extends LatteDelegate{

    private ExampleActivity exampleActivity;

    @BindView(R.id.tv)
    TextView tv;

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

    @Override
    public void onBindView(Bundle savedInstanceState, View rootView) {
        exampleActivity = (ExampleActivity) getActivity();
    }

    @OnClick(R.id.tv)
    void click(){
        if (exampleActivity!=null){
            exampleActivity.cutFragment(new TwoDelegate());
        }
    }
}
public class TwoDelegate extends LatteDelegate{

    @BindView(R.id.tv)
    TextView tv;

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

    @Override
    public void onBindView(Bundle savedInstanceState, View rootView) {
        tv.setText("这是第二页");
    }
}

你可能感兴趣的:(Android架构)