Android开发必看:一文教你完全理解DataBinding框架,Android性能优化推荐书

Android ViewModel详解 视图与数据模型之间的桥梁ViewModel ViewModel的生命周期会比创建它的Activity、Fragment的生命周期都要长。即ViewModel中的数据会一直存活在Activity/Fragment中。众所周知,由于Android平台的特殊性,若应用程序发送屏幕旋转的时候会经历Activity的销毁与重建,这里就涉及到数据保存的问题。虽然Activity可以通过onSaveInstanceState()机制保存与恢复数据,但是onSaveInstanceState()方法只能存储少量的数据进行恢 阅读详情

class MainActivity : DataBindingBaseActivity<ActivityMainBinding, MainViewModel>(
R.layout.activity_main, BR.viewModel
) {
// 除了主 vm,还可以有其他的 vm,来自 fragment-ktx 的 viewModels 扩展,可快速一行代码创建实例
private val mTestViewModel by viewModels()

override fun initData() {
super.initData()
// 关联
mBinding.setVariable(BR.testViewModel, mTestViewModel)
// 或者是这样:
// mBinding.testViewModel = mTestViewModel
}
}

xml 如下:

<layout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”


…

上面已经将 V 和 VM 结合起来了,为啥叫 MVVM 框架呢?因为 M 是 Model 数据层,V 是 View 视图层,VM 是 ViewModel 业务逻辑层。

2、没有 DataBinding 前的世界

在没有 DataBinding 前,也许你的代码是这样的,我们举个例子:执行一个耗时操作后将结果显示在界面上。

<?xml version="1.0" encoding="utf-8"?>
Android_Jetpack:ViewModel原理 这次回来重新写Android,发现现在都用MVVM模式了,想想也是,以前用MVC,随着业务扩展,Activity代码量越来越大,改着改着就牵一发而动全身了。当然,最麻烦的还是随着屏幕的旋转的界面重新初始化处理。所以这也是ViewModel深得我心的一个原因。 ViewModel独立于配置变化,所以屏幕旋转所导致的Activity重建并不会影响ViewModel的生命周期,大概就是下面这个图的样子。 通过查看ViewModel源码可以看到,ViewModel是一个抽象类,里面有一个onCleared()方法 阅读详情

相关推荐

Android 安卓DataBinding(十)·include中使用

在include中使用首先写出layout布局:include_layout.xml

2401_84009626的博客 1093

RecyclerView 与 DataBinding 结合的 Android 列表开发最佳实践

RecyclerView 是 Android 官方推荐的列表容器,其核心价值在于可扩展的 LayoutManager 和生命周期友好的架构设计;DataBinding 则通过声明式语法实现 UI 与数据的自动同步,避免 findViewById 和手动刷新等易错操作。二者结合构成现代 Android 列表开发的底层基建,显著提升代码可维护性、UI 响应一致性与团队协作效率。该方案天然支持 ViewModel 生命周期感知、DiffUtil 智能刷新及 BindingAdapter 自定义逻辑扩展,广泛应用于

weixin_33755649的博客 401

Android MVVM架构与DataBinding实战指南

MVVM(Model-View-ViewModel)是一种广泛应用于现代移动开发的架构模式,通过数据绑定实现视图与业务逻辑的解耦。其核心原理是ViewModel作为数据中介层,配合观察者模式自动同步UI与数据状态。DataBinding作为Android官方支持库,通过编译时生成绑定类的方式,将布局表达式转换为高效代码,显著减少findViewById等样板代码。在工程实践中,这种技术组合能提升开发效率、增强代码可维护性,特别适合电商、社交等需要频繁更新UI的场景。通过双向绑定和自定义适配器等高级特性,开发

weixin_30514745的博客 420

Android DataBinding与MVVM架构实战指南

数据绑定(DataBinding)是Android开发中实现UI与数据自动同步的核心技术,其本质是通过编译时生成的绑定类建立视图与数据模型的连接。结合MVVM架构模式,开发者可以彻底摆脱手动更新UI的繁琐操作,实现真正的数据驱动界面。在工程实践中,DataBinding通过@BindingAdapter支持自定义属性绑定,配合LiveData可实现响应式编程,大幅提升代码可维护性。典型应用场景包括表单处理、列表展示和状态管理,特别是在电商类App的商品详情、购物车等模块能显著减少样板代码。随着Android

weixin_30512043的博客 367

避坑指南:锐浪报表PictureBox打印图像常见的5大问题及解决方案

本文深入剖析了锐浪报表(Grid++Report)中PictureBox控件打印图像时常见的五大难题,包括图像源加载机制、拉伸失真、动态加载失败、跨平台兼容性及性能优化。文章提供了从原理理解到实战解决方案的完整指南,帮助开发者有效规避图像打印中的常见陷阱,确保报表输出的稳定与高质量。

water的专栏 1148

MacOS Intel芯片实战:Docker Compose一键部署Dify全流程解析

本文详细解析了在搭载Intel芯片的MacOS系统上,使用Docker Compose一键部署开源LLM应用开发平台Dify的全流程。内容涵盖环境准备、Docker配置与镜像加速、代码拉取与配置初始化、服务启动与问题排查,以及访问验证与初步探索,旨在帮助开发者快速在本地搭建私有化AI应用开发环境。

ik678901234的博客 618

高通camx ThreadManager

m_pCore->AcceptNewJob是将一个Job或者任务插入一个Queue,有一个工作线程循环从Queue中取Job任务执行,执行任务就是回调任务中的callback函数,AcceptNewJob实现。调用AddToPriorityQueue将任务添加到queue任务队列,那么什么时候从这个queue中取任务执行呢?有个线程循环执行DoWork,最终调用checkAndDequeue取任务执行。

GetnextWindow的专栏 435

MATLAB打开.m文件乱码解决办法

Matlab打开.m文件出现中文乱码问题,是因为Matlab存在两种编码格式:GBK和UTF-8,而不同版本的Matlab编码格式可能不统一,因此在不同版本的Matlab打开文件,由于编码格式的改变,会导致注释乱码。 1.查看你的Matlab的文件编码方式,在Matlab的命令行窗口输入feature('locale') ,如下图,可以看到默认编码为GBK,那问题就变成了将Matlab的文件编码方式GBK修改成UTF-8; 2.找到你的Matlab安装目录bin文件夹下的lcdata_utf.

zhugby的博客 4万+

信息奥赛课课通(C++)p154-1近似排序

试题描述 读入一对正整数,将这两个数之间(包括这两个数本身)的所有数按下述特别规则排序后输出,该特别规则是按两数倒过来的值进行比较决定其大小,如30倒过来为3,29倒过来为92,则29大于30. 输入要求 1行,包含两个用空格隔开的自然数,这两个自然数给出了待排序数的范围,其中1≤第一个数≤第二个数≤999999999,两个数之差不超过100. 输出要求 输出排序后的数据,每行输出...

mengdicfm的博客 7279

56-FMC连接器电路设计

FMC连接器电路设计

weixin_66510961的博客 4721

2026 AI编程环境安装指南:从下载、部署到流式验证

AI编程环境已不再是传统软件安装,而是融合硬件兼容性、模型运行时与IDE插件协同的系统工程。其核心原理在于:本地GPU驱动需匹配量化运行时(如Ollama v0.3+对CUDA 12.1+的强制要求),协议层依赖gRPC流式响应,生态层则要求插件签名与模型源可信校验。这一技术组合显著提升AI代码生成的实时性与生产就绪度,支撑微服务开发、CI/CD智能补全、本地LLM推理等关键场景。本文聚焦AI编程软件下载与安装过程中的可复现部署实践,覆盖macOS/WSL2双平台、NVMe模型加速、离线校验及原子化功能验证

weixin_30480075的博客 581

Python遥感图像处理应用篇(二十三):Python+GDAL 批量拼接图像

遥感影像拼接分多种不同的情况,比如比较常用的是两幅影像拼接或者多个图像合并为一个图像。GDAL中实现影像拼接的方式也有多种,比如常用可以通过numpy读取影像数组,并计算各个图像的numpy数组范围,合并多个numpy数组为一个最终的结果,并将其重新输出为一幅新的图像,即可实现图像的拼接。另外比较方便的一种方法便是使用GDAL.warp()函数方法。这个函数的功能很多,可以实现多种不同的目的,比如用于坐标系转换、投影变换、图像合并与镶嵌、地理范围裁剪、更改分辨率、矢量裁剪等多种功能。这些功能可以通过设置Op

soderayer的博客 4642

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: Android开发实战宝典,附答案,移动端跨平台开发
下一篇: Android开发简记:Java和Android程序员都应该掌握的虚拟机知识
flutter架构师
博客等级 码龄5年 47粉丝 133原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值