databinding

Posted 沫髯小乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了databinding相关的知识,希望对你有一定的参考价值。

bind,graud 里面添加

dataBinding { enabled = true }
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewModel" type="com.shire.mvvmdemo.viewModel.MainViewModel" /> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activi


作者:希灵丶
链接:http://www.jianshu.com/p/981a2aa956b2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewModel" type="com.shire.mvvmdemo.viewModel.MainViewModel" /> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activi


作者:希灵丶
链接:http://www.jianshu.com/p/981a2aa956b2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android">
  <data>
    <variable
        name="viewModel"
        type="com.shire.mvvmdemo.viewModel.MainViewModel"
        />
  </data>
  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activi


作者:希灵丶
链接:http://www.jianshu.com/p/981a2aa956b2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setViewModel(new MainViewModel(binding));
    }
}


作者:希灵丶
链接:http://www.jianshu.com/p/981a2aa956b2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

public class MainViewModel implements onSearchListener { private ActivityMainBinding binding; private SearchModel searchModel = new SearchModel(); private Handler handler; public MainViewModel(ActivityMainBinding binding) { this.binding = binding; handler = new Handler(Looper.getMainLooper()); } public void search(View view) { binding.pbLoad.setVisibility(View.VISIBLE); searchModel.getIPaddressInfo(binding.etIp.getText().toString().trim(), this); } @Override public void onSuccess(final IPAddress ipAddress) { handler.post(new Runnable() { @Override public void run() { binding.pbLoad.setVisibility(View.GONE); binding.tvMsg.setText(ipAddress.toString()); } }); } @Override public void onError() { handler.post(new Runnable() { @Override public void run() { binding.pbLoad.setVisibility(View.GONE); binding.tvMsg.setText("查询失败"); } }); } }


作者:希灵丶
链接:http://www.jianshu.com/p/981a2aa956b2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
public class MainViewModel implements onSearchListener {

  private ActivityMainBinding binding;
  private SearchModel searchModel = new SearchModel();
  private Handler handler;

  public MainViewModel(ActivityMainBinding binding) {
    this.binding = binding;
    handler = new Handler(Looper.getMainLooper());
  }

  public void search(View view) {
    binding.pbLoad.setVisibility(View.VISIBLE);
    searchModel.getIPaddressInfo(binding.etIp.getText().toString().trim(), this);
  }

  @Override public void onSuccess(final IPAddress ipAddress) {
    handler.post(new Runnable() {
      @Override public void run() {
        binding.pbLoad.setVisibility(View.GONE);
        binding.tvMsg.setText(ipAddress.toString());
      }
    });
  }

  @Override public void onError() {

    handler.post(new Runnable() {
      @Override public void run() {
        binding.pbLoad.setVisibility(View.GONE);
        binding.tvMsg.setText("查询失败");
      }
    });
  }
}


作者:希灵丶
链接:http://www.jianshu.com/p/981a2aa956b2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

public class MainViewModel implements onSearchListener {

  private ActivityMainBinding binding;
  private SearchModel searchModel = new SearchModel();
  private Handler handler;

  public MainViewModel(ActivityMainBinding binding) {
    this.binding = binding;
    handler = new Handler(Looper.getMainLooper());
  }

  public void search(View view) {
    binding.pbLoad.setVisibility(View.VISIBLE);
    searchModel.getIPaddressInfo(binding.etIp.getText().toString().trim(), this);
  }

  @Override public void onSuccess(final IPAddress ipAddress) {
    handler.post(new Runnable() {
      @Override public void run() {
        binding.pbLoad.setVisibility(View.GONE);
        binding.tvMsg.setText(ipAddress.toString());
      }
    });
  }

  @Override public void onError() {

    handler.post(new Runnable() {
      @Override public void run() {
        binding.pbLoad.setVisibility(View.GONE);
        binding.tvMsg.setText("查询失败");
      }
    });
  }
}


作者:希灵丶
链接:http://www.jianshu.com/p/981a2aa956b2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewModel" type="com.shire.mvvmdemo.viewModel.MainViewModel" /> </data> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activi


作者:希灵丶
链接:http://www.jianshu.com/p/981a2aa956b2
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

以上是关于databinding的主要内容,如果未能解决你的问题,请参考以下文章

DataBinding 组件未转换为 Object

无法在对话框片段中的文本视图上设置文本

Android Databinding xml重复属性

Android RecyclerView Adapter DataBinding - 找不到符号 layoutBindingImpl

FasterXML/Jackson-databind远程代码执行漏洞安全通告(CVE-2020-35728)

Android DataBinding笔记