安卓基础(LiveData DataBinding)

别说谁变了你拦得住时间么 提交于 2020-02-11 20:10:15

昨天因为有点事情,没有及时发表博客,昨天学习了LiveData和DataBinding,LiveData属于jetpack中的框架里面的,DataBinding可以进行数据绑定。

我分别利用这两部分知识做了一个实例,可以进行对比一下。

LiveData:

java/MainAcitivity:

package com.example.livedatetest;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowInsets;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
 ViewModelWithLiveDate viewModelWithLiveDate;
 ImageButton like,dislike;
 TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        like =findViewById(R.id.imageButton);
        dislike = findViewById(R.id.imageButton2);
        textView = findViewById(R.id.textView);
       viewModelWithLiveDate = ViewModelProviders.of(this).get(ViewModelWithLiveDate.class);
       viewModelWithLiveDate.getLikenumber().observe(this, new Observer<Integer>() {
           @Override
           public void onChanged(@Nullable Integer integer) {
               textView.setText(String.valueOf(integer));
           }
       });
   like.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           viewModelWithLiveDate.addnumber(1);
       }
   });
   dislike.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
       viewModelWithLiveDate.addnumber(-1);
       }
   });
    }
}

java/ViewModelWithLiveData

package com.example.livedatetest;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

public class ViewModelWithLiveDate extends ViewModel {
    private MutableLiveData<Integer> likenumber;

    public MutableLiveData<Integer> getLikenumber() {
        if(likenumber == null){
            likenumber = new MutableLiveData<>();
            likenumber.setValue(0);
        }
        return likenumber;
    }
    public void addnumber(int n){
        likenumber.setValue(likenumber.getValue() + n);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:id="@+id/dislike"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textview"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.471"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.149" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/Like"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.201"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.48"
        app:srcCompat="@drawable/ic_thumb_up_black_24dp" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/dislike"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.845"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.481"
        app:srcCompat="@drawable/ic_thumb_down_black_24dp" />
</android.support.constraint.ConstraintLayout>

再运用上Databinding:

java/MainAcitivity:

package com.example.datebinding;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingComponent;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.datebinding.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {
MyViewModel myViewModel;
ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

     binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
     myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
     binding.setData(myViewModel);
     binding.setLifecycleOwner(this);

    }
}

java/MyviewModel

package com.example.livedatetest;

        import android.arch.lifecycle.MutableLiveData;
        import android.arch.lifecycle.ViewModel;

public class ViewModelWithLiveDate extends ViewModel {
    private MutableLiveData<Integer> likenumber;

    public MutableLiveData<Integer> getLikenumber() {
        if(likenumber == null){
            likenumber = new MutableLiveData<>();
            likenumber.setValue(0);
        }
        return likenumber;
    }
    public void addnumber(int n){
        likenumber.setValue(likenumber.getValue() + n);
    }
}

acitivity_main.xml:

<?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="data"
         type="com.example.datebinding.MyViewModel" />
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbarSize="50dp"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/textview"
            android:text="@{String.valueOf(data.like)}"
            android:textSize="36sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.161" />

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/like"
            android:onClick="@{()->data.addnumber()}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.204"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.421"
            app:srcCompat="@drawable/ic_thumb_up_black_24dp"
            />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/dislike"
            android:onClick="@{()->data.cutnumber()}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.872"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.421"
            app:srcCompat="@drawable/ic_thumb_down_black_24dp" />

    </android.support.constraint.ConstraintLayout>
</layout>

界面截图:点点赞按钮图片就加一,点另一个加一。

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!