最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開發(fā)使用Databinding實現關注功能mvvp

 更新時間:2022年09月13日 15:21:28   作者:DavidMC  
這篇文章主要為大家介紹了Android開發(fā)使用Databinding實現關注功能mvvp示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

正文

說到關注功能,可能很多小伙伴要說了。誰不會寫

但是沒有合理的架構,大家寫出來的代碼很可能是一大堆的復制粘貼。比如十幾個頁面,都有這個關注按鈕。然后,你是不是也要寫十幾個地方呢 然后修改的時候是不是也要修改十幾個地方 我們是否考慮過一下幾個問題?

  •  可復用性 (是否重復代碼和邏輯過多?)
  •  可擴展性 (比如我這里是關注的人,傳userId,下個地方又是文章 articleId)
  •  可讀性 冗余代碼過多,勢必要影響到可讀性。

然后再看下自己寫的代碼,是否會面臨上面的幾個問題呢?是否有一種優(yōu)雅的方式。幫我們一勞永逸。我這里給出一個解決方案是 使用Databinding ,如果對databinding使用不熟悉的,建議先去熟悉一下databinding用法

目標

我們要實現的目標是,希望能讓關注這快的業(yè)務邏輯實現最大程度復用,在所有有關注按鈕布局的頁面,只需要引入一個同一個vm。實現關注和非關注狀態(tài)邏輯的切換

Modle

下面以關注人來做為示例

要有兩種狀態(tài),實體bean要繼承自BaseObservable。配合databing實現mvvm效果,屬性需要定義為@Bindable,當屬性發(fā)生變化的時候,調用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開發(fā)使用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開發(fā)中的重力傳感器用法實例詳解

    Android開發(fā)中的重力傳感器用法實例詳解

    這篇文章主要介紹了Android開發(fā)中的重力傳感器用法,簡單分析了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六種創(chuàng)建方式案例詳解

    Android AlertDialog六種創(chuàng)建方式案例詳解

    這篇文章主要介紹了Android AlertDialog六種創(chuàng)建方式案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08

最新評論

迁西县| 承德县| 临澧县| 石门县| 永年县| 怀安县| 正镶白旗| 庆城县| 鸡泽县| 台中市| 平江县| 大安市| 灌阳县| 富裕县| 阿拉尔市| 梧州市| 星子县| 涞水县| 马边| 新疆| 岚皋县| 云安县| 定南县| 辽宁省| 南召县| 久治县| 沁阳市| 马边| 泰州市| 凤冈县| 延长县| 五大连池市| 高青县| 托里县| 庄河市| 昆山市| 五莲县| 兖州市| 祥云县| 冕宁县| 荣昌县|