版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第Android开发使用Databinding实现关注功能mvvp目录正文目标ModlePresenter
正文
说到关注功能,可能很多小伙伴要说了。谁不会写
但是没有合理的架构,大家写出来的代码很可能是一大堆的复制粘贴。比如十几个页面,都有这个关注按钮。然后,你是不是也要写十几个地方呢然后修改的时候是不是也要修改十几个地方我们是否考虑过一下几个问题?
可复用性(是否重复代码和逻辑过多?)可扩展性(比如我这里是关注的人,传userId,下个地方又是文章articleId)可读性冗余代码过多,势必要影响到可读性。
然后再看下自己写的代码,是否会面临上面的几个问题呢?是否有一种优雅的方式。帮我们一劳永逸。我这里给出一个解决方案是使用Databinding,如果对databinding使用不熟悉的,建议先去熟悉一下databinding用法
目标
我们要实现的目标是,希望能让关注这快的业务逻辑实现最大程度复用,在所有有关注按钮布局的页面,只需要引入一个同一个vm。实现关注和非关注状态逻辑的切换
Modle
下面以关注人来做为示例
要有两种状态,实体bean要继承自BaseObservable。配合databing实现mvvm效果,属性需要定义为@Bindable,当属性发生变化的时候,调用notifyPropertyChanged(属性ID)
publicclassUserextendsBaseObservableimplementsSerializable{
publicbooleanhasFollow;//是否关注,是和否
@Bindable
publicbooleanisHasFollow(){
returnhasFollow;
publicvoidsetHasFollow(booleanhasFollow){
this.hasFollow=hasFollow;
notifyPropertyChanged(com.mooc.ppjoke.BR._all);
页面布局如下
xmlversion="1.0"encoding="utf-8"
layoutxmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
data
variable
name="feed"
type="com.mooc.ppjoke.model.Feed"/
variable
name="leftMargin"
type="java.lang.Integer"/
variable
name="fullscreen"
type="java.lang.Boolean"/
importtype="com.mooc.ppjoke.utils.TimeUtils"/
importtype="com.mooc.ppjoke.ui.InteractionPresenter"/import
variable
name="owner"
type="androidx.lifecycle.LifecycleOwner"/
/data
androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/author_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:orientation="vertical"
android:paddingLeft="@{leftMargin}"
android:paddingTop="@dimen/dp_3"
android:paddingBottom="@dimen/dp_3"
com.mooc.ppjoke.view.PPImageView
android:id="@+id/author_avatar"
android:layout_width="@dimen/dp_40"
android:layout_height="@dimen/dp_40"
android:layout_marginTop="@dimen/dp_1"
app:image_url="@{feed.author.avatar}"
app:isCircle="@{true}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/icon_splash_text"/com.mooc.ppjoke.view.PPImageView
TextView
android:id="@+id/author_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="@dimen/dp_3"
android:text="@{}"
android:textColor="@{fullscreen@color/color_white:@color/color_000}"
android:textSize="@dimen/sp_14"
android:textapp:layout_constraintLeft_toRightOf="@+id/author_avatar"
app:layout_constraintTop_toTopOf="parent"
tools:text="Title"/TextView
TextView
android:id="@+id/create_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="@dimen/dp_2"
android:text="@{TimeUtils.calculate(feed.createTime)}"
android:textColor="@{fullscreen@color/color_white:@color/color_000}"
android:textSize="@dimen/sp_12"
android:textapp:layout_constraintLeft_toRightOf="@+id/author_avatar"
app:layout_constraintTop_toBottomOf="@+id/author_name"
tools:text="3天前"/TextView
com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/dp_16"
android:backgroundTint="@{fullscreen@color/transparent:@color/color_theme}"
android:gravity="center"
android:android:paddingLeft="@dimen/dp_16"
android:paddingTop="@dimen/dp_5"
android:paddingRight="@dimen/dp_16"
android:paddingBottom="@dimen/dp_5"
android:text="@{feed.author.hasFollow@string/has_follow:@string/unfollow}"
android:textColor="@color/color_white"
android:textSize="@dimen/sp_14"
app:backgroundTint="@color/color_theme"
app:cornerRadius="@dimen/dp_13"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:strokeColor="@{fullscreen@color/color_white:@color/transparent}"
app:strokeWidth="1dp"
tools:text="已关注"/
/androidx.constraintlayout.widget.ConstraintLayout
/layout
显示效果
Presenter
packagecom.mooc.ppjoke.ui;
importandroid.app.Application;
importandroid.content.Context;
importandroid.content.DialogInterface;
importandroid.text.TextUtils;
importandroid.view.View;
importandroid.widget.Toast;
importandroidx.appcompat.app.AlertDialog;
importandroidx.arch.core.executor.ArchTaskExecutor;
importandroidx.lifecycle.LifecycleOwner;
importandroidx.lifecycle.LiveData;
importandroidx.lifecycle.MutableLiveData;
importandroidx.lifecycle.Observer;
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONObject;
importcom.mooc.libcommon.extention.LiveDataBus;
importcom.mooc.libcommon.global.AppGlobals;
importcom.mooc.libnetwork.ApiResponse;
importcom.mooc.libnetwork.ApiService;
importcom.mooc.libnetwork.JsonCallback;
importcom.mooc.ppjoke.model.Comment;
importcom.mooc.ppjoke.model.Feed;
importcom.mooc.ppjoke.model.TagList;
importcom.mooc.ppjoke.model.User;
importcom.mooc.ppjoke.ui.login.UserManager;
importcom.mooc.ppjoke.ui.share.ShareDialog;
importorg.jetbrains.annotations.NotNull;
importjava.util.Date;
publicclassInteractionPresenter{
//关注/取消关注一个用户
privatestaticvoidtoggleFollowUser(LifecycleOwnerowner,Useruser){
ApiService.get("/ugc/toggleUserFollow")
.addParam("followUserId"
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年四川省巴中市从“五方面人员”中选拔乡镇领导班子成员考试强化练习题及答案
- 2025年卫生高级职称面审答辩普通外科副高面审经典试题及答案
- 2025年一级建造师考试(机电工程管理与实务)题库含答案佛山
- 2026年高级育婴师学习考试试题及答案解析
- 宁德市一级建造师考试(机电工程管理与实务)题库含答案(2025年)
- 除颤操作失误纠错模拟应急演练
- 跨河桥梁汛期漂浮物撞击应急预案
- 机动车检测站内审年度计划及实施细则
- Giparmen-生命科学试剂-MCE
- FTC-146-precursor-生命科学试剂-MCE
- 中职机械教学中数字化教学资源的开发与应用课题报告教学研究课题报告
- 宜宾市自然资源和规划局竞争性比选工作人员的考试参考试题及答案解析
- 《道路运输企业主要负责人和安全生产管理人员安全考核机动车维修企业》专业部分题库(附答案)
- 20.2电生磁教案(表格式)2025-2026学年初中物理人教版九年级全一册
- 霍桑红字介绍
- TGXAS-抗肿瘤药物临床试验护理工作规范编制说明
- 美团推广合同范本
- 网络金融部业务知识考试题库
- 税务领导选拔面试题目及答案
- 内分泌危象识别与应急处理
- 机关人员公务出差审批单
评论
0/150
提交评论