All Projects → tianzhijiexian → Shatter

tianzhijiexian / Shatter

代替fragment的轻量级解耦类,拥有和activity完全一致的生命周期

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Shatter

StackOverFlowApi
working with Stack OverFlow Api
Stars: ✭ 24 (-94.68%)
Mutual labels:  fragments
Android Jetpack Demo
🔥 快速入门Android Jetpack以及相关Kotlin、RxJava、MVVM等主流技术,独立构架App的基础技能
Stars: ✭ 335 (-25.72%)
Mutual labels:  lifecycle
Puzzle Js
⚡ Micro frontend framework for scalable and blazing fast websites.
Stars: ✭ 398 (-11.75%)
Mutual labels:  fragments
Alligator
Alligator is a modern Android navigation library that will help to organize your navigation code in clean and testable way.
Stars: ✭ 287 (-36.36%)
Mutual labels:  fragments
Kompass
Kotlin Multiplatform Router for Android and iOS
Stars: ✭ 328 (-27.27%)
Mutual labels:  fragments
Net
Android上强大的网络请求
Stars: ✭ 344 (-23.73%)
Mutual labels:  lifecycle
LifecycleCells
An Android library that provides a Lifecycle to any ViewHolder through the implementation of the LifecycleOwner interface, allowing it to interact with a Lifecycle-Aware Component.
Stars: ✭ 19 (-95.79%)
Mutual labels:  lifecycle
Mvvmarms
Android MVVM Architecture Components based on MVPArms and Android Architecture Components.
Stars: ✭ 425 (-5.76%)
Mutual labels:  lifecycle
Android Architecture Components
The template project that uses Android Architecture Components with Repository pattern. The simple app that uses awesome Fuel library instead of Retrofit for perfoming HTTP request. The app also persists data using the Room library and display data in RecyclerView.
Stars: ✭ 329 (-27.05%)
Mutual labels:  lifecycle
Lazyfragment
类似微信,网易新闻 延迟加载Fragment基类 , 支持与ViewPager组合刷新全部Fragment
Stars: ✭ 391 (-13.3%)
Mutual labels:  fragments
Autodispose
Automatic binding+disposal of RxJava streams.
Stars: ✭ 3,209 (+611.53%)
Mutual labels:  lifecycle
Android Inline Youtube View
Utility library around using YouTube inside your android app.
Stars: ✭ 313 (-30.6%)
Mutual labels:  fragments
Android Extensions
An Android library with modules to quickly bootstrap an Android application.
Stars: ✭ 356 (-21.06%)
Mutual labels:  fragments
Rxjava Rxlife
一行代码解决RxJava 内存泄漏,一款轻量级别的RxJava生命周期管理库
Stars: ✭ 269 (-40.35%)
Mutual labels:  lifecycle
Ng Dynamic Component
Dynamic components with full life-cycle support for inputs and outputs for Angular
Stars: ✭ 396 (-12.2%)
Mutual labels:  lifecycle
rxlife
一行代码解决RxJava 内存泄漏,一款轻量级别的RxJava生命周期管理库
Stars: ✭ 322 (-28.6%)
Mutual labels:  lifecycle
Decompose
Kotlin Multiplatform lifecycle-aware business logic components (aka BLoCs) with routing functionality and pluggable UI (Jetpack Compose, SwiftUI, JS React, etc.)
Stars: ✭ 339 (-24.83%)
Mutual labels:  lifecycle
Mvp Android Arch Component
Android MVP sample adapts with new Android architecture components (Lifecycle, Room).
Stars: ✭ 446 (-1.11%)
Mutual labels:  lifecycle
Beehive
🐝 BeeHive is a solution for iOS Application module programs, it absorbed the Spring Framework API service concept to avoid coupling between modules.
Stars: ✭ 4,117 (+812.86%)
Mutual labels:  lifecycle
Miox
Modern infrastructure of complex SPA
Stars: ✭ 374 (-17.07%)
Mutual labels:  lifecycle

Shatter

Shatter是一个代替fragment来划分ui模块的库。它主要完成的工作是管理ui区块,并且能和activity保持完全相同的生命周期,没有任何学习成本。

Shatter对于单页面多ui模块的结构有着很好的支持,非常适合用来降低复杂activity的复杂度。但因为设计的关系,它的生命周期仅仅被activity触发的,所以不会有完整的生命周期的概念。

所有的监听工作都是通过shatterManager来实现的,这个类将会把activity的方法对应给shatter:

(上图的方法均是一一对应的关系)

引入方式

1.添加JitPack仓库

repositories {
    maven {
        url "https://jitpack.io"
    }
}

2.添加依赖

implementation 'com.github.tianzhijiexian:Shatter:Latest release(<-click)'

配置方式

配置的方式有两种可选,第一种比较复杂,第二种较为简单。

1. 让shatter有监听activity全部生命周期的能力

在app的build.gradle中配置aspectj:

apply plugin: 'com.android.application'

apply plugin: 'me.leolin.gradle-android-aspectj'

接着在baseActivity实现IShatterActivity,并复写你需要被shatter感知的生命周期(无需做任何处理,只需复写即可),如:

private ShatterManager mShatterManager;

public ShatterManager getShatterManager() {
     if (mShatterManager == null) {
    	mShatterManager = new ShatterManager(this);
     }
     return mShatterManager;
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

// ...

2. 仅仅需要监听部分生命周期

在baseActivity中的onCreate()中写上如下语句:

private ShatterManager mShatterManager;

public ShatterManager getShatterManager() {
     if (mShatterManager == null) {
    	mShatterManager = new ShatterManager(this);
     }
     return mShatterManager;
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EventDispatchFragment.injectIfNeededIn(this);
}

这种方式下的shatter可拥有如下生命周期:

  • onRestoreInstanceState
  • onStart
  • onResume
  • onPause
  • onStop
  • onDestroy
  • onActivityResult
  • onSaveInstanceState

使用

定义一个shatter:

public static class MyShatter extends Shatter {

    private TextView mTopTv;
    
    @Override
    protected int getLayoutResId() {
        return android.R.layout.my_shatter;
    }

    @Override
    public void bindViews(View rootView) {
        mTopTv = findViewById(R.id.top_tv);
    }

    @Override
    public void setViews() {
        View root = getRootView();
        root.setBackgroundResource(R.drawable.shatter_green_bg);
        
        TextView textView = mTopTv;
        textView.setGravity(Gravity.CENTER);
        textView.setText(R.string.test_text);
    }

}

方式一:在activity中添加这个shatter:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    getShatterManager().add(R.id.root_container, new MyShatter());
}

方式二:在shatter中添加一个shatter(支持多重嵌套):

public class MiddleShatter extends Shatter {

    @Override
    protected int getLayoutResId() {
        return R.layout.middle_shatter;
    }

    @Override
    public void bindViews(View rootView) {
    
        getShatterManager().add(R.id.inner_fl, new InnerShatter());
    }

}

效果

多个shatter组合完成一个界面

额外说明

  1. Shatter自身会产生事件,如果要和activity进行交互,那么可以通过activity给shatter设置listener的方式来做。

  2. ShatterManager提供了findShatterByTag()findShatterByContainViewId(),可以通过二者来查找shatter,方便解耦。

  3. 如果你需要在viewPager中使用shatter,那么可以“选用”shatterPagerAdapter来做。

开发者

Jack Tony: [email protected]

License

Copyright 2016-2019 Jack Tony

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].