Android Databinding 从入门到转行(三)在xml视图将ViewModel成员注入到View的setXXX方法

转载请声明:http://blog.csdn.net/yoyo_newbie/article/details/51959154


注入规则:

条件:某View中如含方法:setXXX,    参数唯一,类型为T

        注入步骤:在ViewModel中,添加 T  类型成员引用t

       注入方法:在对应的xml,的根元素layout, 添加

xmlns:app="http://schemas.android.com/apk/res-auto

      然后,找到某View , 添加app:xxxx="@{model.t}" ,就可以完成注入。



关说不干,不懂啥意思,让我们来实战。


需求三:

Android Databinding 从入门到转行(二)添加点击事件需求中的Button 改为 SwipeRefreshLayout实现,通过下拉将数据刷新出来。


UI效果图:


  





第一步:实现UI


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

    <data>
        <variable
            name="userModel"
            type="com.yoyonewbie.mvvm.vm.UserModel" />
    </data>

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        
        >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="16dp">


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="姓名:" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@{userModel.name}" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_vertical_margin"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="年龄:" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@{userModel.age}" />
                </LinearLayout>


            </LinearLayout>
        </ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</layout>

那么在databinding中下拉事件回调是怎么实现的呢?

按正常来实现,就是在MainActivity里面,通过findViewById加载出实例,再设置事件回调.如下

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        
    }
});



那么,我们观察setOnRefreshListener这个方法,setXXX(唯一参数),那么满足viewmodel的成员可 在view的xml编写注入变量的需求。


那么我们就可以填空:

条件:SwipeRefreshLayout含方法:setOnRefreshListener,    参数唯一,类型为SwipeRefreshLayout.OnRefreshListener

        注入步骤:在UserModel 中,添加 SwipeRefreshLayout.OnRefreshListener 类型成员引用onRefreshListener 

       注入方法:在对应的xml,的根元素layout, 添加

xmlns:app="http://schemas.android.com/apk/res-auto

      然后,找到SwipeRefreshLayout , 添加app:onRefreshListener="@{userModel.onRefreshListener}" ,就可以完成注入。





实现代码:


package com.yoyonewbie.mvvm.vm;



import android.databinding.Observable;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

public class UserModel {


    public ObservableField<String> name=  new ObservableField<String>();

    public   ObservableField<String>  age=new ObservableField<String>();

    public void init()
    {
        name.set("未加载") ;
        age.set("未加载");
    }


    /**
     * 是刷新用户数据
     */
    public void freshUserInfo()
    {
        name.set("Sam") ;
        age.set("25");
    }

    public SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            freshUserInfo();
        }
    };



}



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

    <data>
        <variable
            name="userModel"
            type="com.yoyonewbie.mvvm.vm.UserModel" />
    </data>

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:onRefreshListener="@{userModel.onRefreshListener}"
        >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="16dp">


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="姓名:" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@{userModel.name}" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_vertical_margin"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="年龄:" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@{userModel.age}" />
                </LinearLayout>


            </LinearLayout>
        </ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</layout>




package com.yoyonewbie.mvvm.view.activity;

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

import com.yoyonewbie.mvvm.vm.UserModel;
import com.yoyonewbie.test.R;
import com.yoyonewbie.test.databinding.MainActivityBinding;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainActivityBinding mainActivityBinding = DataBindingUtil.setContentView(this, R.layout.main_activity);
        UserModel user = new UserModel();
        user.init();
        mainActivityBinding.setUserModel(user);
    }



}



效果:







下拉后:







这时候,我想改变下拉状态怎么办 ?


改变下拉状态,需要调用setRefreshing(boolean)方法。


观察下,这个方法也满足注入的条件是不是?走一下思路!!!!



条件:SwipeRefreshLayout含方法:setRefreshing,    参数唯一,类型为boolean

        注入步骤:在UserModel 中,添加  ObserverBoolean(因为操作超过2次不用boolean)类型成员引用isRefreshing 

       注入方法:在对应的xml,的根元素layout, 添加

xmlns:app="http://schemas.android.com/apk/res-auto

      然后,找到SwipeRefreshLayout , 添加app:refreshing="@{userModel.isRefreshing}" ,就可以完成注入。



补全代码:


package com.yoyonewbie.mvvm.vm;



import android.databinding.Observable;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

public class UserModel {


    public ObservableField<String> name=  new ObservableField<String>();

    public   ObservableField<String>  age=new ObservableField<String>();

    public  ObservableBoolean isRefreshing = new ObservableBoolean();




    public void init()
    {
        name.set("未加载") ;
        age.set("未加载");
    }


    /**
     * 是刷新用户数据
     */
    public void freshUserInfo()
    {
        name.set("Sam") ;
        age.set("25");
    }

    public SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            isRefreshing.set(true);
            freshUserInfo();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    isRefreshing.set(false);
                }
            }, 1000);
        }
    };



}



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

    <data>
        <variable
            name="userModel"
            type="com.yoyonewbie.mvvm.vm.UserModel" />
    </data>

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:onRefreshListener="@{userModel.onRefreshListener}"
        app:refreshing ="@{userModel.isRefreshing}"
        >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="16dp">


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="姓名:" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@{userModel.name}" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/activity_vertical_margin"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="年龄:" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@{userModel.age}" />
                </LinearLayout>


            </LinearLayout>
        </ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</layout>

















  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《Jetpack架构组件从入门到精通》.pdf 是一本介绍Android Jetpack架构组件的书籍。Jetpack是Google官方提供的一套组件库,旨在帮助开发者更轻松地构建高质量的Android应用程序。 这本书从入门到精通地介绍了Jetpack架构组件的各个方面。首先,它详细解释了Jetpack的基本概念和使用方法。读者可以学习到Jetpack的核心组件,如ViewModel、LiveData、Room和Navigation等,以及它们在构建Android应用时的作用。 此外,这本书还介绍了Jetpack架构组件的一些高级技术和最佳实践。读者可以学习到如何使用WorkManager实现后台任务,如何使用DataBinding进行数据绑定,以及如何使用Paging构建分页列表等。 除了介绍Jetpack架构组件的基本用法和高级技术,这本书还提供了一些实际应用示例和案例分析。读者可以通过参考这些示例来更好地理解和应用Jetpack架构组件。 总之,《Jetpack架构组件从入门到精通》.pdf 是一本全面而深入地介绍Jetpack架构组件的书籍。无论是初学者还是有一定经验的开发者,都可以通过阅读这本书来提升自己在Android应用开发中使用Jetpack的能力。 ### 回答2: 《Jetpack架构组件从入门到精通》.pdf 是一本关于Jetpack架构组件的学习指南。Jetpack是Android开发中一套强大而且灵活的组件集合,旨在帮助开发者更轻松地构建高质量的Android应用程序。 该PDF文件从入门到精通地介绍了Jetpack架构组件的各个方面。首先,它向读者介绍了Jetpack的概念和使用场景,以帮助读者了解为什么应该学习和使用这些组件。 接下来,该指南逐一介绍了Jetpack架构组件的不同模块,包括ViewModel、LiveData、Room、Navigation等。它详细解释了每个组件的功能和用法,并通过实际示例演示了如何在项目中使用它们。 此外,该指南还提供了一些常见的最佳实践和实用技巧,以帮助读者更好地理解和运用Jetpack架构组件。它还包含了一些常见问题和解决方案,帮助读者避免在实践中遇到的常见问题。 最后,该指南还提供了一些参考资料和进一步学习资源,以帮助读者深入学习和掌握Jetpack架构组件。 总的来说,该《Jetpack架构组件从入门到精通》.pdf提供了一个全面而详尽的学习指南,帮助读者了解和应用Jetpack架构组件,使他们能够更高效地构建高质量的Android应用程序。无论是初学者还是有经验的开发者,该指南都是一个值得阅读和参考的资源。 ### 回答3: 《Jetpack架构组件从入门到精通》.pdf 是一本介绍Android Jetpack架构组件的电子书,它有助于开发者学习和理解如何使用这些组件来构建高质量、稳定的Android应用程序。 Jetpack架构组件是由谷歌开发的一组库,旨在帮助开发者简化Android应用的开发过程。它提供了一系列的工具和组件,涵盖了各个方面,包括界面设计、数据库、网络通信、数据绑定、后台处理等。 这本电子书从入门到精通地介绍了各个组件的使用方法和最佳实践。它首先详细介绍了Jetpack架构组件的核心概念和优势,让读者了解为什么要使用这些组件。 然后,电子书逐一介绍了常用的Jetpack组件,如Lifecycle、ViewModel、Room、LiveData、Navigation等。每个组件都被详细地讲解,包括其作用、使用方法和示例代码。读者可以通过跟随书中的案例来实际操作和理解这些组件的使用。 除了介绍各个组件,电子书还分享了一些进阶的使用技巧和开发经验。这些技巧包括如何优化应用性能、处理异步任务、实现数据缓存等。通过这些实用的技巧,开发者可以进一步提高应用的质量和用户体验。 总的来说,《Jetpack架构组件从入门到精通》.pdf 是一本很有价值的学习资料,对于想要深入学习和掌握Jetpack架构组件的开发者来说是必不可少的参考书籍。无论是初学者还是有经验的开发者,都可以从中获得知识和技能的提升。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值