MVC,MVP,MVVM

DataBinding的原理

  1. 在Build中intermediates文件加下根据layout布局生成了两个xml文件,一个文件给每个View标记了Tag,
    另一个文件中记录了Tag对应的属性
  2. 在apt目录下生成了对应的BR类ActivityMainBindingImpl和DataBinderMapperImpl
  3. 当数据改变时,调用了User类中的notifyPropertyChanged(BR.password);
    此时ViewDataBinding的静态块代码中的监听被触发,这个监听会新开一个线程,获取到两个xml中的tag信息,最后调用到ActivityMainBindingImpl类中的excuteBinfings完成User数据的赋值和UI的更新

MVVM的开发模式方案:

  1. build.gradle中添加
 dataBinding{
        enabled true
    }
  1. 在layout布局文件中添加
    
        
        
    

        
        
        
    

  1. 数据类中添加
 * 这个类就相当于一个ViewModel
 */
public class User extends BaseObservable {
    private String name;
    private String password;

    private String header;

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public User(String name, String password,String header) {
        this.name = name;
        this.password = password;
        this.header=header;
    }

    @Bindable
    public String getName() {
        return name;
    }

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

    }
    @Bindable
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        notifyPropertyChanged(BR.password);
    }
    //自定义属性:提供一个静态方法来加载image
    @BindingAdapter("bind:header")
    public static void getImage(ImageView view,String url){
        Picasso.with(view.getContext()).load(url).into(view);
    }

    public void click(View view){
        Toast.makeText(view.getContext(),getName() , Toast.LENGTH_SHORT).show();
    }
}
  1. Activity中使用
 ActivityMainBinding binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
        //这些数据是model层来的
        user=new User("jett","123","http://e.hiphotos.baidu.com/image/pic/item/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg");
        binding.setUser(user);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                user.setName("alan");
                user.setPassword("*******");
            }
        },5000);

5.ListView中使用,点击事件的添加


public class CommAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private int layoutId;
    private int variableId;//会自动生成

    private List list;

    public CommAdapter(Context context, LayoutInflater inflater, int layoutId, int variableId, List list) {
        this.context = context;
        this.inflater = inflater;
        this.layoutId = layoutId;
        this.variableId = variableId;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewDataBinding dataBinding;
        if(convertView==null){
            dataBinding= DataBindingUtil.inflate(inflater,layoutId,parent,false);
        }else{
            //就重就之前的
            dataBinding=DataBindingUtil.getBinding(convertView);
        }
        dataBinding.setVariable(variableId,list.get(position));
        return dataBinding.getRoot().getRootView();
    }
}
public class MainActivity extends AppCompatActivity {

    ListView list;
    List data=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=(ListView)findViewById(R.id.listView);
        data.add(new User("http://e.hiphotos.baidu.com/image/pic/item/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg","1","1"));
        data.add(new User("http://e.hiphotos.baidu.com/image/pic/item/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg","2","2"));
        data.add(new User("http://e.hiphotos.baidu.com/image/pic/item/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg","3","3"));
        data.add(new User("http://e.hiphotos.baidu.com/image/pic/item/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg","4","4"));
        list.setAdapter(new CommAdapter(this,getLayoutInflater(),R.layout.item,BR.user,data));
    }
}



item.xml



    
        
    
    
        
        
        

    

    

你可能感兴趣的:(MVC,MVP,MVVM)