亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

一文教你如何使用Databinding寫(xiě)一個(gè)關(guān)注功能

 更新時(shí)間:2022年09月13日 11:34:42   作者:DavidMC  
這篇文章主要介紹了一文教你如何使用Databinding寫(xiě)一個(gè)關(guān)注功能,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

前言

但是沒(méi)有合理的架構(gòu),大家寫(xiě)出來(lái)的代碼很可能是一大堆的復(fù)制粘貼。比如十幾個(gè)頁(yè)面,都有這個(gè)關(guān)注按鈕。然后,你是不是也要寫(xiě)十幾個(gè)地方呢 然后修改的時(shí)候是不是也要修改十幾個(gè)地方 我們是否考慮過(guò)一下幾個(gè)問(wèn)題?

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

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

目標(biāo)

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

Modle

下面以關(guān)注人來(lái)做為示例:

要有兩種狀態(tài),實(shí)體bean要繼承自BaseObservable。配合databing實(shí)現(xiàn)mvvm效果,屬性需要定義為@Bindable,當(dāng)屬性發(fā)生變化的時(shí)候,調(diào)用notifyPropertyChanged(屬性ID)

public class User extends BaseObservable implements Serializable {
    public boolean hasFollow;//是否關(guān)注,是和否
    @Bindable
    public boolean isHasFollow() {
        return hasFollow;
    }
    public void setHasFollow(boolean hasFollow) {
        this.hasFollow = hasFollow;
        notifyPropertyChanged(com.mooc.ppjoke.BR._all);
    }
}

頁(yè)面布局如下:

<?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="已關(guān)注" />
    </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 {
    //關(guān)注/取消關(guān)注一個(gè)用戶
 
    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);
                    }
                });
    }
 
}

綜上已經(jīng)實(shí)現(xiàn)了簡(jiǎn)單的用戶關(guān)注功能。activity不需要做任何事情。 

到此這篇關(guān)于一文教你如何使用Databinding寫(xiě)一個(gè)關(guān)注功能的文章就介紹到這了,更多相關(guān)Databinding寫(xiě)功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android?MaterialButton使用實(shí)例詳解(告別shape、selector)

    Android?MaterialButton使用實(shí)例詳解(告別shape、selector)

    我們平時(shí)寫(xiě)布局,當(dāng)遇到按鈕需要圓角、或者描邊等,通常的方法是新建一個(gè)xml文件,在shape標(biāo)簽下寫(xiě),然后通過(guò)android:background或setBackground(drawable)設(shè)置,這篇文章主要給大家介紹了關(guān)于Android?MaterialButton使用詳解的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • Android自定義View實(shí)現(xiàn)鐘擺效果進(jìn)度條PendulumView

    Android自定義View實(shí)現(xiàn)鐘擺效果進(jìn)度條PendulumView

    這篇文章主要介紹了Android自定義View實(shí)現(xiàn)鐘擺效果進(jìn)度條PendulumView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android開(kāi)發(fā)手冊(cè)TextView控件及陰影效果實(shí)現(xiàn)

    Android開(kāi)發(fā)手冊(cè)TextView控件及陰影效果實(shí)現(xiàn)

    這篇文章主要為大家介紹了Android開(kāi)發(fā)手冊(cè)TextView控件及陰影效果的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android之有效防止按鈕多次重復(fù)點(diǎn)擊的方法(必看篇)

    Android之有效防止按鈕多次重復(fù)點(diǎn)擊的方法(必看篇)

    下面小編就為大家?guī)?lái)一篇Android之有效防止按鈕多次重復(fù)點(diǎn)擊的方法(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • 教你快速實(shí)現(xiàn)Android動(dòng)態(tài)模糊效果

    教你快速實(shí)現(xiàn)Android動(dòng)態(tài)模糊效果

    相信大家都發(fā)現(xiàn)了越來(lái)越多的App里面使用了模糊效果,比如雅虎天氣的界面,雖然我并不知道雅虎天氣是怎么做出這種效果的,但是簡(jiǎn)單的模仿一下的話,還是能做到的。下面一起來(lái)學(xué)習(xí)學(xué)習(xí)。
    2016-08-08
  • Android 出現(xiàn)“Can''t bind to local 8602 for debugger”錯(cuò)誤的解決方法

    Android 出現(xiàn)“Can''t bind to local 8602 for debugger”錯(cuò)誤的解決方法

    這篇文章主要介紹了Android 出現(xiàn)“Can't bind to local 8602 for debugger”錯(cuò)誤的解決方法的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android?利用ImageView屬性實(shí)現(xiàn)選中和未選中效果

    Android?利用ImageView屬性實(shí)現(xiàn)選中和未選中效果

    這篇文章主要介紹了Android巧用ImageView屬性實(shí)現(xiàn)選中和未選中效果,實(shí)現(xiàn)思路通常我們會(huì)選擇在布局里加個(gè)ImageView,然后通過(guò)代碼層面加個(gè)判斷去讓ImageView加載不同狀態(tài)的圖片,需要的朋友可以參考下
    2023-06-06
  • Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄

    Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)搜索功能,并實(shí)現(xiàn)本地保存搜索歷史記錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Android 編程下的計(jì)時(shí)器代碼

    Android 編程下的計(jì)時(shí)器代碼

    在安卓 APP 的手機(jī)號(hào)注冊(cè)邏輯中,經(jīng)常會(huì)有將激活碼發(fā)送到手機(jī)的環(huán)節(jié),這個(gè)環(huán)節(jié)中絕大多數(shù)的應(yīng)用考慮到網(wǎng)絡(luò)延遲或服務(wù)器壓力以及短信服務(wù)商的延遲等原因,會(huì)給用戶提供一個(gè)重新獲取激活碼的按鈕
    2013-08-08
  • 2013年 移動(dòng)App設(shè)計(jì)13項(xiàng)注意細(xì)節(jié)總結(jié)

    2013年 移動(dòng)App設(shè)計(jì)13項(xiàng)注意細(xì)節(jié)總結(jié)

    在過(guò)去的一年里,移動(dòng)成主流也讓眾多的移動(dòng)應(yīng)用如雨后春筍般層出不窮,在眾多開(kāi)發(fā)者從中獲利的同時(shí)競(jìng)爭(zhēng)也愈演愈烈,如何才能保證自己立于不敗之地接下來(lái)介紹移動(dòng)App設(shè)計(jì)的13大精髓感興趣的朋友可以了解下啊
    2013-01-01

最新評(píng)論