東邪HUX
2018-01-12 07:41
采纳率: 66.7%
浏览 2.2k

如何在include的xml布局中使用观察者模式对ImageView显示的图片进行修改

主布局
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">

<data>
    <import type="android.databinding.ObservableMap"/>
    <import type="com.village.crops.utils.DataBindUtil"/>
    <variable
        name="main"
        type="ObservableMap&lt;String,Integer&gt;"/>

    <variable
        name="click"
        type="com.village.crops.activity.MainActivity"/>

    <variable
        name="imageUtil"
        type="DataBindUtil"/>

    <variable
        name="imageUrl"
        type="Integer"/>
</data>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorWhite"
    android:orientation="vertical" >


    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>

    <include
        android:id="@+id/village_base_bottom"
        layout="@layout/village_bottom"
        bind:main="@{main}"
        bind:click="@{click}"
        bind:imageUtil="@{imageUtil}"
        bind:imageUrl="@{imageUrl}"/>

</LinearLayout>

引用布局
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">

<data>
    <import type="android.databinding.ObservableMap"/>
    <import type="com.village.crops.utils.DataBindUtil"/>
    <variable
        name="main"
        type="ObservableMap&lt;String,Integer&gt;"/>

    <variable
        name="click"
        type="com.village.crops.activity.MainActivity"/>

    <variable
        name="imageUtil"
        type="DataBindUtil"/>

    <variable
        name="imageUrl"
        type="Integer"/>
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorWhite"
    android:gravity="center_vertical">

    <LinearLayout
        android:id="@+id/layout_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:orientation="vertical"
        android:onClick="@{click.onClick}">

        <ImageView
            android:id="@+id/img_home"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src='@{main["imgid"]}'
            bind:imageUrl="@{imageUrl}"/>

        <TextView
            android:id="@+id/text_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/VillageTextTheme"
            android:text="@string/home_home"
            android:textColor='@{main["textColor"]}'/>

    </LinearLayout>

</LinearLayout>

Main代码
activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);

ObservableArrayMap resourceMap = new ObservableArrayMap<>();
resourceMap.put("imgid",R.mipmap.icon_home_car_sel);
resourceMap.put("textColor", Color.RED);

   activityMainBinding.setClick(this);
   activityMainBinding.setMain(resourceMap);
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • devmiao 2018-01-12 11:07
    已采纳
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题