Android mvvm使用实战

本编文章简单使用databinding,给刚入门的同学学习mvvm借鉴
进入正题

//在gradle android下加入
dataBinding {
        enabled = true
}

.xml中包含常见控件还有recycleview



    
        
            type="com.renli.mvvmdemo.been.UserInfo" />
        

        
        
        
    

    

        

        

        

activity中

public class MvvmMainActivity extends Activity {

    private ActivityMvvmMainBinding binding;
    UserInfo userInfo = new UserInfo();

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

        userInfo.setName("阿萌");
        userInfo.setAge(20);
        userInfo.setSex("男");
        userInfo.setDetail("深圳");
        userInfo.setIconUrl("https://网上图片连接.jpg");
        userInfo.setPlaceUrl(R.drawable.bm_list);

        binding.setUserInfo(userInfo);
        binding.setClick(this);

        ArrayList mList = new ArrayList<>();
        mList.add(userInfo);
        mList.add(userInfo);

        UserAdapter adapter = new UserAdapter(this, mList);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvUser.setLayoutManager(manager);
        binding.rvUser.setAdapter(adapter);
    }

    /**
     * 点击事件
     * @param view
     */
    public void btClick(View view){
        //数据源改变更新UI
        userInfo.setName("阿金金");
        userInfo.setAge(30);
    }

    @BindingAdapter({"imagesUrl", "placeUrl"})//必须静态方法
    public static void image(ImageView view, String iconUrl, Drawable placeUrl){
        Glide.with(view.getContext()).load(iconUrl).placeholder(placeUrl).error(R.mipmap.agent_icon).into(view);
    }

    public void onCheckedChanged(View view, boolean isChecked){
        binding.setIsShow(isChecked);
    }
}

定义ViewHolder

public class BindingViewHolder extends RecyclerView.ViewHolder {

    private T mBinding;
    public BindingViewHolder(@NonNull T binding) {
        super(binding.getRoot());
        mBinding = binding;
    }

    public T getBinding(){
        return mBinding;
    }
}

adapter中

public class UserAdapter extends RecyclerView.Adapter {

    private LayoutInflater inflater;
    private ArrayList mList = new ArrayList<>();

    public UserAdapter(Activity activity, ArrayList list){
        mList = list;
        inflater = LayoutInflater.from(activity);
    }

    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, R.layout.activity_mvvm_main_item, parent, false);
        return new BindingViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull BindingViewHolder holder, int position) {
        ViewDataBinding binding = holder.getBinding();
        binding.setVariable(BR.userItem, mList.get(position));
        binding.executePendingBindings();//让数据立即生效
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
}

adapter中的xml


    
        
    

    

        

        


    

UserInfo实体类

public class UserInfo extends BaseObservable {
    @Bindable
    private String name;
    @Bindable
    private int age;
    @Bindable
    private String sex;
    @Bindable
    private String detail;
    @Bindable
    private String iconUrl;
    @Bindable
    private int placeUrl;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
        notifyPropertyChanged(BR.age);
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
        notifyPropertyChanged(BR.sex);
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
        notifyPropertyChanged(BR.detail);
    }

    public String getIconUrl() {
        return iconUrl;
    }

    public void setIconUrl(String iconUrl) {
        this.iconUrl = iconUrl;
        notifyPropertyChanged(BR.iconUrl);
    }

    public int getPlaceUrl() {
        return placeUrl;
    }

    public void setPlaceUrl(int placeUrl) {
        this.placeUrl = placeUrl;
        notifyPropertyChanged(BR.placeUrl);
    }

}

你可能感兴趣的:(Android,mvvm,android,android,studio)