Android JectPack笔记三-1:ViewModel/LiveData基本使用

官方地址

https://developer.android.google.cn/topic/libraries/architecture/viewmodel#java

创建viewmodel

import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.ViewModel;

import com.zqq.h5shell.activity.jectpack_navigation.bean.User;

import java.util.ArrayList;
import java.util.List;

public class MyViewModle extends ViewModel {
    private MediatorLiveData> users;

    public MediatorLiveData> getUser() {
        if(users == null){
            users=new MediatorLiveData<>();
            loadUsers();
        }
        return users;
    }

    private void loadUsers() {
        List list=new ArrayList<>();
        for(int i=0;i<10;i++){
            User user=new User("aoe"+i,"iuu"+i,i);
           list.add(user);
        }
        users.setValue(list);
    }

    public void setUser(MediatorLiveData> user) {
        this.users = user;
    }

    public void changeUser() {
        if(users != null){
            List list=new ArrayList<>();
            for(int i=0;i<10;i++){
                User user=new User("bpmf"+i,"geh"+i,100+i);
                list.add(user);
            }
            users.postValue(list);
        }
    }
}

import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.zqq.h5shell.ARouterContent;
import com.zqq.h5shell.R;
import com.zqq.h5shell.activity.jectpack_navigation.bean.User;
import com.zqq.h5shell.activity.jectpack_navigation.model.MyViewModle;
import com.zqq.h5shell.databinding.ActivityViewmodelBinding;
import com.zqq.h5shell.databinding.ItemDatabindAdapterBinding;

import org.jetbrains.annotations.NotNull;

import java.util.List;

@Route(path = ARouterContent.ViewModelActivity)
public class ViewModelActivity extends AppCompatActivity {

    private ActivityViewmodelBinding binding;
    private MyBinddingAdapter adapter;
    private MyViewModle myViewModle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_viewmodel);

        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        //adapter 使用databinding
        adapter = new MyBinddingAdapter(R.layout.item_databind_adapter);
        binding.recyclerview.setAdapter(adapter);

        myViewModle = new ViewModelProvider(this).get(MyViewModle.class);
        myViewModle.getUser().observe(this, new Observer>() {
            @Override
            public void onChanged(List users) {
                adapter.addData(users);
            }
        });

        binding.btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myViewModle.changeUser();  //更新
            }
        });
    }

    public class MyBinddingAdapter extends BaseQuickAdapter {

        public MyBinddingAdapter(int layoutResId, @org.jetbrains.annotations.Nullable List data) {
            super(layoutResId, data);
        }

        public MyBinddingAdapter(int layoutResId) {
            this(layoutResId,null);
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }

        @Override
        protected void convert(@NotNull BaseDataBindingHolder baseDataBindingHolder, User user) {
            if(user == null){
                return;
            }

            ItemDatabindAdapterBinding dataBinding = (ItemDatabindAdapterBinding) baseDataBindingHolder.getDataBinding();
            dataBinding.setUser(user);
            dataBinding.executePendingBindings();
        }
    }
}

adapter 条目布局



    
        
        

    

    
    

    


结果:
Android JectPack笔记三-1:ViewModel/LiveData基本使用_第1张图片

你可能感兴趣的:(android,android,gradle,java)