網站首頁 編程語言 正文
前言
但是沒有合理的架構,大家寫出來的代碼很可能是一大堆的復制粘貼。比如十幾個頁面,都有這個關注按鈕。然后,你是不是也要寫十幾個地方呢 然后修改的時候是不是也要修改十幾個地方 我們是否考慮過一下幾個問題?
- ?可復用性 (是否重復代碼和邏輯過多?)
- ?可擴展性 (比如我這里是關注的人,傳userId,下個地方又是文章 articleId)
- ?可讀性 冗余代碼過多,勢必要影響到可讀性。
然后再看下自己寫的代碼,是否會面臨上面的幾個問題呢?是否有一種優雅的方式。幫我們一勞永逸。我這里給出一個解決方案是 使用Databinding ,如果對databinding使用不熟悉的,建議先去熟悉一下databinding用法
目標
我們要實現的目標是,希望能讓關注這快的業務邏輯實現最大程度復用,在所有有關注按鈕布局的頁面,只需要引入一個同一個vm。實現關注和非關注狀態邏輯的切換
Modle
下面以關注人來做為示例:
要有兩種狀態,實體bean要繼承自BaseObservable。配合databing實現mvvm效果,屬性需要定義為@Bindable,當屬性發生變化的時候,調用notifyPropertyChanged(屬性ID)
public class User extends BaseObservable implements Serializable {
public boolean hasFollow;//是否關注,是和否
@Bindable
public boolean isHasFollow() {
return hasFollow;
}
public void setHasFollow(boolean hasFollow) {
this.hasFollow = hasFollow;
notifyPropertyChanged(com.mooc.ppjoke.BR._all);
}
}
頁面布局如下:
<?xml version="1.0" encoding="utf-8"?> <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"> <data> <variable name="feed" type="com.mooc.ppjoke.model.Feed" /> <variable name="leftMargin" type="java.lang.Integer" /> <variable name="fullscreen" type="java.lang.Boolean" /> <import type="com.mooc.ppjoke.utils.TimeUtils" /> <import type="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="@{feed.author.name}" android:textColor="@{fullscreen?@color/color_white:@color/color_000}" android:textSize="@dimen/sp_14" android:textStyle="bold" app: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:textStyle="normal" app: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:onClick="@{()->InteractionPresenter.toggleFollowUser(owner,feed)}" 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
package com.mooc.ppjoke.ui;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mooc.libcommon.extention.LiveDataBus;
import com.mooc.libcommon.global.AppGlobals;
import com.mooc.libnetwork.ApiResponse;
import com.mooc.libnetwork.ApiService;
import com.mooc.libnetwork.JsonCallback;
import com.mooc.ppjoke.model.Comment;
import com.mooc.ppjoke.model.Feed;
import com.mooc.ppjoke.model.TagList;
import com.mooc.ppjoke.model.User;
import com.mooc.ppjoke.ui.login.UserManager;
import com.mooc.ppjoke.ui.share.ShareDialog;
import org.jetbrains.annotations.NotNull;
import java.util.Date;
public class InteractionPresenter {
//關注/取消關注一個用戶
private static void toggleFollowUser(LifecycleOwner owner,User user) {
ApiService.get("/ugc/toggleUserFollow")
.addParam("followUserId", UserManager.get().getUserId())
.addParam("userId", feed.author.userId)
.execute(new JsonCallback<JSONObject>() {
@Override
public void onSuccess(ApiResponse<JSONObject> response) {
if (response.body != null) {
boolean hasFollow = response.body.getBooleanValue("hasLiked");
user.setHasFollow(hasFollow);
LiveDataBus.get().with(DATA_FROM_INTERACTION)
.postValue(feed);
}
}
@Override
public void onError(ApiResponse<JSONObject> response) {
showToast(response.message);
}
});
}
}
綜上已經實現了簡單的用戶關注功能。activity不需要做任何事情。?
原文鏈接:https://juejin.cn/post/7142678611810582558
相關推薦
- 2022-09-28 C語言關于二叉樹中堆的創建和使用整理_C 語言
- 2022-12-21 Redis數據庫原理深入刨析_Redis
- 2022-09-16 python?playwright之元素定位示例詳解_python
- 2022-07-22 python:實現打印從 0 到 n 的卡特蘭數算法(附完整源碼)
- 2022-08-30 詳解Oracle控制文件及日志文件的管理問題_oracle
- 2022-05-27 python繪制棉棒圖的方法詳解_python
- 2023-07-24 uniapp開發小程序端原生導航欄
- 2023-06-02 Hadoop部署的基礎設施操作詳解_服務器其它
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支