高德天气应用开发之十二:android DataBinding 数据和UI双向绑定 使用方法

版权声明:本文为博主原创文章,未经允许不得转载,如有问题,欢迎指正,谢谢! https://blog.csdn.net/cbk861110/article/details/86669708

【版权说明】

1. 请支持原创,转载请注明出处:https://blog.csdn.net/cbk861110/article/details/86665564

2. 项目源码请移步:https://github.com/caobaokang419/WeatherApp(欢迎Github Fork&Star,框架和技术实现不妥之处,请帮忙指正),谢谢!

-------------------------------------------

基于MVVM框架的高德天气APP:


MVVM框架和组件实现:

  • Data-binding :使用xml声明格式(而不是编程方式)将布局中的UI组件绑定到应用程序中的数据源。

一、 技术背景:

1. DataBinding,Android推荐使用的支持库,使用xml声明方式(而不是编程方式)将布局中的UI组件绑定到应用程序中的数据源上;

2. 此做法移除了大量冗余的findViewById()+类似 TextView.setText()的java语句,使得代码更整洁;

二、 实现方式:

1.  xml布局文件使用DataBinding填充&绑定数据 的实现样式 (以当前天气item_cur_weather.xml为例):

<?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 class="com.gary.weatherdemo.databinding.CurWeatherBinding">

        <variable
            name="viewModel"
            type="com.gary.weatherdemo.viewmodel.CurWeatherViewModel" />
    </data>

    <LinearLayout
        android:id="@+id/cur_weather_view"
        style="@style/WeatherRecycleItem">

        <TextView
            android:id="@+id/cur_temp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.mLiveWeatherBean.temperature+@string/temperature_signal}"
            android:textSize="36dp" />

        <TextView
            android:id="@+id/cur_weather"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{viewModel.mLiveWeatherBean.weather}"
            android:textSize="36dp" />
    </LinearLayout>
</layout>

2. 被xml的控件binded的数据样式(以当前天气CurWeatherViewModel.java为例):

扫描二维码关注公众号,回复: 5377837 查看本文章
public class CurWeatherViewModel {
    public final LiveWeatherBean mLiveWeatherBean;

    public CurWeatherViewModel(LiveWeatherBean result) {
        mLiveWeatherBean = result;
    }
}

3. 最为关键的一步,关联Bind(以当前天气View: CurWeatherViewItem 为例):

public class CurWeatherViewItem implements IViewItem<LiveWeatherBean> {
    private CurWeatherBinding mDataBinding;

    public static CurWeatherViewItem getViewItem() {
        return new CurWeatherViewItem();
    }

    public View createView(ViewGroup parent) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_cur_weather, parent, false);
        //获取CurWeatherViewItem 的CurWeatherBinding对象
        mDataBinding = CurWeatherBinding.bind(itemView);
        return itemView;
    }

    public void bindView(@NonNull LiveWeatherBean data) {
        //关联CurWeatherBinding对象 & 数据 CurWeatherViewModel(data)
        mDataBinding.setViewModel(new CurWeatherViewModel(data));
    }
}
public class CityWeatherRecyclerAdapter extends RecyclerView.Adapter<BaseItemViewHolder> {

    @Override
    public void onBindViewHolder(BaseItemViewHolder holder, int position) {
        holder.getIViewItem().bindView(mItemDatas.get(position));
    }
}

4. 双向绑定之反向绑定:TBD

-------------------------------------------

文章目录(未完,待续):

一:android 应用子功能及移动框架总述 https://blog.csdn.net/cbk861110/article/details/86665564

二:android 高德天气API说明及城市天气查询实现 https://blog.csdn.net/cbk861110/article/details/86665655

三:android 自定义控件实现(ActionBar + PageIndicatorView) https://blog.csdn.net/cbk861110/article/details/86665790

四:android ViewPager实现左右页面滑动切换 https://blog.csdn.net/cbk861110/article/details/86665964

五:android应用权限动态申请 https://blog.csdn.net/cbk861110/article/details/86666321

六:android RecyclerView 封装及使用 https://blog.csdn.net/cbk861110/article/details/86666392

七:android Xutils3文件下载实现(高德天气城市配置) https://blog.csdn.net/cbk861110/article/details/86666573

八:android DiskLruCache 磁盘缓存 封装和使用 https://blog.csdn.net/cbk861110/article/details/86666664

九:android ThreadPoolExecutor线程池 封装及使用  https://blog.csdn.net/cbk861110/article/details/86667101

十:android 天气网络请求框架(retrofit2&okhttp3&Gson) 封装及使用  https://blog.csdn.net/cbk861110/article/details/86667375

十一:android RxAndroid(响应式编程) 异步网络请求实现 https://blog.csdn.net/cbk861110/article/details/86669178

十二:android DataBinding 数据和UI双向绑定实现 https://blog.csdn.net/cbk861110/article/details/86669708

十三:android room数据库 天气数据读写实现 https://blog.csdn.net/cbk861110/article/details/86670354

十四:android LiveData 使用方法(实现城市天气自动刷新) https://blog.csdn.net/cbk861110/article/details/86670531

十五:android ViewModel 使用方法 https://blog.csdn.net/cbk861110/article/details/86670703

十六:android 集成友盟消息推送机制(U-Push) https://blog.csdn.net/cbk861110/article/details/86683849

-------------------------------------------

【版权说明】

1. 请支持原创,转载请注明出处:https://blog.csdn.net/cbk861110/article/details/86665564

2. 项目源码请移步:https://github.com/caobaokang419/WeatherApp(欢迎Github Fork&Star,框架和技术实现不妥之处,请帮忙指正),谢谢!

猜你喜欢

转载自blog.csdn.net/cbk861110/article/details/86669708