Seven click modes of databinding onclick

Time:2021-1-14

Recently, I have encountered a lot of pitfalls in learning how to use databinding. Let’s record the following to help my friends who will learn databinding in the future.

There is a ViewModel class and an XML. XML binds ViewModel

1. @{click}


//xml:
<Button
   android:layout_width="match_parent"
   android:layout_height="48dp"
   android:onClick="@{click}"
/>

//ViewModel:
public void click(View view){

}

2. Without parameters: @ {() – > viewModel.click ()}


//xml:
<Button
   android:layout_width="match_parent"
   android:layout_height="48dp"
   android:onClick="@{() -> viewModel.click()}"
/>

//ViewModel:
public void click(){

}

3. @{viewModel::click}


//xml:
<Button
   android:layout_width="match_parent"
   android:layout_height="48dp"
   android:onClick="@{viewModel::click}"
/>

//ViewModel:
public void click(View view){

}

Tip: if you set the click method in other classes, such as EventHandlers.java In fact, it is consistent with the above

//xml:
<variable
  name="handler"
  type="com.xx.xxx.EventHandlers" />

<Button
   android:layout_width="match_parent"
   android:layout_height="48dp"
   android:onClick="@{handler::click}" />

//UI class: bind handler, just like ViewModel
EventHandlers handler = new EventHandlers();
binding.setHandler(handler);

//Eventhandlers executes the click event
public void click(View view){
  //do
}

4. With parameters: @ {() – > viewModel.click ( obj.id }


//xml:
<variable
  name="viewModel"
  type="com.xx.xxx.ViewModel" />

<variable
  name="obj"
  type="com.xx.xxx.User" />
<Button
   android:layout_width="match_parent"
   android:layout_height="48dp"
   android:onClick="@{() -> viewModel.click(obj.id)}"
/>

//ViewModel:
public void click(long id){
   //do  
}

5. ObservableField<OnClickListener>

//xml:
 <variable
    name="iconView"
    type="com.xxxxx.IconView" />

 <RelativeLayout
    android:id="@+id/rl_icon_view"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:onClick="@{iconView.clickListener}"
    >  

//IconView:
 public final ObservableField<OnClickListener> clickListener=new ObservableField<>();

//Activity class of XML:
 binding.iconView.clickListener.set(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
       Toast.makeText(getApplicationContext(),"iconView",Toast.LENGTH_SHORT).show();
     }
 });

6. With parameters


 android:onClick="@{()->loadingModel.chooseLang(2)}"
 public void chooseLang(int language) {
    LogUtil.i(TAG, "language=" + language);

 }

7. Parameters with view


//xml:
<variable
    name="listener"
    type="test.carrie.todomvvmtest.ui.OnTaskItemListener"/>

android:onClick="@{(view)->listener.onCheckBoxClick(obj,view)}"


//adapter:
 public void onCheckBoxClick(ToDo entity, View v) {

    }

summary

The above is the whole content of this article, I hope the content of this article has a certain reference learning value for your study or work, thank you for your support to developer.

Recommended Today

Practice of query operation of database table (Experiment 3)

Following the previous two experiments, this experiment is to master the use of select statements for various query operations: single table query, multi table connection and query, nested query, set query, to consolidate the database query operation.Now follow Xiaobian to practice together!Based on the data table (student, course, SC, teacher, TC) created and inserted in […]