Android开发使用Databinding实现关注功能mvvp

 更新时间:2022年09月13日 15:21:28   作者:DavidMC  
这篇文章主要为大家介绍了Android开发使用Databinding实现关注功能mvvp示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

正文

说到关注功能,可能很多小伙伴要说了。谁不会写

但是没有合理的架构,大家写出来的代码很可能是一大堆的复制粘贴。比如十几个页面,都有这个关注按钮。然后,你是不是也要写十几个地方呢 然后修改的时候是不是也要修改十几个地方 我们是否考虑过一下几个问题?

  •  可复用性 (是否重复代码和逻辑过多?)
  •  可扩展性 (比如我这里是关注的人,传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不需要做任何事情。

以上就是Android开发使用Databinding实现关注功能mvvp的详细内容,更多关于Android Databinding关注功能的资料请关注脚本之家其它相关文章!

相关文章

  • Android实现整理PackageManager获取所有安装程序信息

    Android实现整理PackageManager获取所有安装程序信息

    这篇文章主要介绍了Android实现整理PackageManager获取所有安装程序信息的方法,实例分析了Android使用PackageManager获取安装程序信息的具体步骤与相关技巧,需要的朋友可以参考下
    2016-01-01
  • Android中比较两个图片是否一致的问题

    Android中比较两个图片是否一致的问题

    这篇文章主要介绍了Android中比较两个图片是否一致的问题,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • android图片压缩的3种方法实例

    android图片压缩的3种方法实例

    这篇文章介绍了android图片压缩的3种方法实例,有需要的朋友可以参考一下
    2013-09-09
  • Android开发中的重力传感器用法实例详解

    Android开发中的重力传感器用法实例详解

    这篇文章主要介绍了Android开发中的重力传感器用法,简单分析了Android重力传感器的基本功能、使用方法,并结合实例形式分析了Android基于重力传感器实现横竖屏切换的相关操作技巧,需要的朋友可以参考下
    2017-11-11
  • Android编程使用WebView实现与Javascript交互的方法【相互调用参数、传值】

    Android编程使用WebView实现与Javascript交互的方法【相互调用参数、传值】

    这篇文章主要介绍了Android编程使用WebView实现与Javascript交互的方法,可实现基于WebView与JavaScript相互调用参数、传值的功能,需要的朋友可以参考下
    2017-03-03
  • Android控件CardView实现卡片效果

    Android控件CardView实现卡片效果

    这篇文章主要为大家详细介绍了Android控件CardView实现卡片效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • Android设置重复文字水印背景的方法

    Android设置重复文字水印背景的方法

    这篇文章主要为大家详细介绍了Android设置重复文字水印背景的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Android异步方法以同步方式实现

    Android异步方法以同步方式实现

    这篇文章主要为大家详细介绍了Android异步方法以同步方式进行的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android 更新RecyclerView的好方法

    Android 更新RecyclerView的好方法

    在使用RecyclerView的时候不免要修改RecyclerView的数据,使用notifyDataSetChanged()来刷新界面,但是当数据多,而只是修改了一点的数据,或者刷新比较频繁,这样就会导致界面卡顿,用户交互特别不好,这时可以使用RecyclerView方法解决,具体实现代码大家参考下本文吧
    2017-06-06
  • Android AlertDialog六种创建方式案例详解

    Android AlertDialog六种创建方式案例详解

    这篇文章主要介绍了Android AlertDialog六种创建方式案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08

最新评论