databinding传递事件和绑定数据

布局文件

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">

    <data>
        <!--<import type="com.victor.databinding.User"/>-->
        <import type="android.view.View"/>
        <variable
            name="user"
            type="com.victor.databindingdemo.User"/>
        <variable
            name="handler"
            type="com.victor.databindingdemo.MyHandler"/>
        <variable
            name="task"
            type="com.victor.databindingdemo.MyTask"/>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.victor.databindingdemo.MainActivity">

        <!--数据的绑定-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.firstName}"/>

        <!-- 三元表达式 -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.phone}"
            android:visibility="@{user.isShowPhone ? View.VISIBLE : View.GONE}"/>

        <!--类方法的绑定-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.getFullName(user.firstName, user.lastName)}"/>

        <!--事件绑定-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ShowToast"
            android:onClick="@{handler::showTost}"/>

        <!-- 监听绑定 使用lambda表达式 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="runTask"
            android:onClick="@{() -> handler.onEventListenerExecute(task)}"/>
    </LinearLayout>
</layout>
package com.victor.databindingdemo;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.victor.databindingdemo.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 要用setContentView(Activity activity, int layoutId)代替该方法
        //setContentView(R.layout.activity_main);

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        // 初始化数据
        User user = new User();
        user.firstName = "Victor";
        user.lastName = "Fang";
        user.phone = "13333333333";
        user.isShowPhone = true;
        // 绑定数据
        binding.setUser(user);

        // 绑定方法和监听
        MyHandler handler = new MyHandler();
        MyTask task = new MyTask(this);
        binding.setHandler(handler);
        binding.setTask(task);
    }
}
package com.victor.databindingdemo;

import android.view.View;
import android.widget.Toast;

/** 事件处理类
 * Created by Victor on 2017/1/21.
 */
public class MyHandler {

    public void showTost(View view) {
        Toast.makeText(view.getContext(), "展示吐司", Toast.LENGTH_SHORT).show();
    }

    public void onEventListenerExecute(MyTask task) {
        task.run();
    }

}

 

package com.victor.databindingdemo;

import android.content.Context;
import android.widget.Toast;

/** 任务类
 * Created by Victor on 2017/1/21.
 */
public class MyTask implements Runnable {

    private Context mContext;

    public MyTask(Context context) {
        mContext = context;
    }

    @Override
    public void run() {
        Toast.makeText(mContext, "执行任务", Toast.LENGTH_SHORT).show();
    }
}
package com.victor.databindingdemo;

import android.text.TextUtils;

/** JavaBean
 * Created by Victor on 2017/1/20.
 */

public class User {

    public String firstName;
    public String lastName;
    public String phone; // 电话
    public boolean isShowPhone;

    /**
     * 获取全名
     * @param firstName
     * @param lastName
     * @return
     */
    public String getFullName(String firstName, String lastName) {
        StringBuilder sb = new StringBuilder();
        sb.append("全名:");
        if (!TextUtils.isEmpty(firstName)) {
            sb.append(firstName);
        }
        if (!TextUtils.isEmpty(lastName)) {
            sb.append("." + lastName);
        }
        return sb.toString();
    }
}

 

发布了6 篇原创文章 · 获赞 1 · 访问量 2205
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览