自学内容网 自学内容网

Android中使用Viewbind的activity基类

package mu.basic.app;



/**
 * 使用ViewBinding的Activity基类
 * @param <T> ViewBinding泛型
 */
public abstract class Activity<T extends ViewBinding> extends AppCompatActivity {

    protected T mBinding;

    public final T initViewBinding() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        if(type != null) {
            Class<?> cls = (Class<?>) type.getActualTypeArguments()[0];
            try {
                Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class);
                mBinding = (T) inflate.invoke(null, getLayoutInflater());
            } catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return mBinding;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewBinding();
        if(mBinding !=  null) setContentView(mBinding.getRoot());
    }

    public boolean useImmersed() {
        return true;
    }

    public boolean isDarkFontForStatusBar() {
        return false;
    }

    public int getStatusBarColor() {
        return android.R.color.transparent;
    }

    public boolean isFitsSystemWindows() {
        return true;
    }

    public View getStatusView() {
        int id = getResources().getIdentifier("status_bar", "id", getPackageName());
        return findViewById(id);
    }

    public BarHide getBarHide() {
        return BarHide.FLAG_SHOW_BAR;
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);

        if(useImmersed()) {
            // TODO 启用沉浸式状态栏
            ImmersionBar.with(this)
                    .transparentStatusBar()
                    .transparentNavigationBar()
                    .statusBarDarkFont(isDarkFontForStatusBar())
                    .titleBar(getStatusView())
                    .statusBarColor(getStatusBarColor())
                    .fitsSystemWindows(isFitsSystemWindows())
                    .fullScreen(false)
                    .hideBar(getBarHide())
                    .init();
        }

        onLoad();
    }

    protected abstract void onLoad();

    public void onBack(View view) {
        AlphaAnimation animation = new AlphaAnimation(0.8f, 1.0f);
        animation.setDuration(200);
        view.startAnimation(animation);

//        back();
    }

    public final <V extends ViewBinding> void showFragment(@IdRes int id, Fragment<V> fragment) {
        showFragment(id, fragment, null);
    }

    public final <V extends ViewBinding> void showFragment(@IdRes int id, Fragment<V> fragment, String tag) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment<V> currentFragment = findCurrentShowFragmentById(id);
        if(fragment == currentFragment) return;

        if(currentFragment != null)
            transaction.hide(currentFragment);

        if(fragment.getId() == 0) {
            if(tag == null) {
                transaction.add(id, fragment, fragment.getClass().getName());
            } else {
                transaction.add(id, fragment, tag);
            }
        } else {
            transaction.show(fragment);
        }
        transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();
    }

    public final <V extends ViewBinding> void replaceFragment(int id, Fragment<V> fragment) {
        replaceFragment(id, fragment, null);
    }

    public final <V extends ViewBinding> void replaceFragment(int id, Fragment<V> fragment, String tag) {
        Fragment<V> currentFragment = findCurrentShowFragmentById(id);
        if(fragment == currentFragment) return;

        FragmentManager manager = getSupportFragmentManager();
        manager.popBackStack();
        FragmentTransaction transaction = manager.beginTransaction();
        if(tag == null) {
            transaction.replace(id, fragment);
        } else {
            transaction.replace(id, fragment, tag);
        }
        transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();
    }

    public final <V extends ViewBinding> Fragment<V> findCurrentShowFragmentById(@IdRes int id) {
        List<androidx.fragment.app.Fragment> fragments = getSupportFragmentManager().getFragments();
        for (int i=0; i < fragments.size(); i++) {
            androidx.fragment.app.Fragment fragment = fragments.get(i);
            if(!fragment.isHidden() && fragment.getId() == id) {
                return (Fragment<V>) fragment;
            }
        }
        return null;
    }

    public final <V extends ViewBinding> Fragment<V> findFragmentById(@IdRes int id) {
        return (Fragment<V>) getSupportFragmentManager().findFragmentById(id);
    }

    public final <V extends ViewBinding> Fragment<V> findFragmentByTag(String tag) {
        return (Fragment<V>) getSupportFragmentManager().findFragmentByTag(tag);
    }

    public void receiveAction(int id, Object... params) {
        // 处理来自Fragment的动作
    }
}


原文地址:https://blog.csdn.net/weixin_65399034/article/details/140184202

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!