Skip to content

Commit c3fcadc

Browse files
committedMar 3, 2016
更新dataBinding配置
1 parent f871e39 commit c3fcadc

File tree

7 files changed

+51
-21
lines changed

7 files changed

+51
-21
lines changed
 

‎DataBindingExample/app/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ android {
1818
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1919
}
2020
}
21+
dataBinding {
22+
enabled = true
23+
}
2124
}
2225

2326
dependencies {
2427
compile fileTree(dir: 'libs', include: ['*.jar'])
2528
compile 'com.android.support:appcompat-v7:22.2.0'
29+
compile 'com.github.bumptech.glide:glide:3.7.0'
2630
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.halzhang.android.example.databindingexample" >
2+
<manifest package="com.halzhang.android.example.databindingexample"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
44

55
<application
66
android:allowBackup="true"
77
android:icon="@mipmap/ic_launcher"
88
android:label="@string/app_name"
9-
android:theme="@style/AppTheme" >
9+
android:theme="@style/AppTheme">
1010
<activity
1111
android:name=".MainActivity"
12-
android:label="@string/app_name" >
12+
android:label="@string/app_name">
1313
<intent-filter>
14-
<action android:name="android.intent.action.MAIN" />
14+
<action android:name="android.intent.action.MAIN"/>
1515

16-
<category android:name="android.intent.category.LAUNCHER" />
16+
<category android:name="android.intent.category.LAUNCHER"/>
1717
</intent-filter>
1818
</activity>
1919
</application>
2020

21+
<uses-permission android:name="android.permission.INTERNET"/>
22+
2123
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.halzhang.android.example.databindingexample;
2+
3+
import android.databinding.BindingAdapter;
4+
import android.widget.ImageView;
5+
6+
import com.bumptech.glide.Glide;
7+
8+
/**
9+
* Created by Hal on 2016/3/3.
10+
*/
11+
public class Bindings {
12+
13+
14+
@BindingAdapter({"bind:imageUrl"})
15+
public static void loadImage(ImageView imageView, String url) {
16+
Glide.with(imageView.getContext()).load(url).into(imageView);
17+
}
18+
19+
}

‎DataBindingExample/app/src/main/java/com/halzhang/android/example/databindingexample/MainActivity.java

+5-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class MainActivity extends AppCompatActivity {
2525
protected void onCreate(Bundle savedInstanceState) {
2626
super.onCreate(savedInstanceState);
2727
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
28-
User user = new User("username", "password");
28+
User user = new User("username", "password", "http://images.unsplash.com/photo-1454678904372-2ca94103eca4");
2929
binding.setUser(user);
3030

3131
listView = (ListView) findViewById(R.id.list);
@@ -56,24 +56,17 @@ public long getItemId(int position) {
5656

5757
@Override
5858
public View getView(int position, View convertView, ViewGroup parent) {
59-
ViewHolder viewHolder;
59+
ListItemBinding binding;
6060
if (convertView == null) {
61-
ListItemBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.list_item, parent, false);
61+
binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.list_item, parent, false);
6262
convertView = binding.getRoot();
63-
viewHolder = new ViewHolder();
64-
viewHolder.view = convertView;
65-
convertView.setTag(viewHolder);
63+
convertView.setTag(binding);
6664
} else {
67-
viewHolder = (ViewHolder) convertView.getTag();
65+
binding = (ListItemBinding) convertView.getTag();
6866
}
69-
ListItemBinding binding = DataBindingUtil.bind(viewHolder.view);
7067
binding.setCourse(mCourses.get(position));
7168
return convertView;
7269
}
73-
74-
class ViewHolder {
75-
View view;
76-
}
7770
}
7871

7972
@Override

‎DataBindingExample/app/src/main/java/com/halzhang/android/example/databindingexample/User.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
public class User {
77
public final String username;
88
public final String password;
9+
public final String avatar;
910

10-
public User(String username, String password) {
11+
public User(String username, String password, String avatar) {
1112
this.username = username;
1213
this.password = password;
14+
this.avatar = avatar;
1315
}
1416

1517
public String getUsername() {
@@ -19,4 +21,8 @@ public String getUsername() {
1921
public String getPassword() {
2022
return password;
2123
}
24+
25+
public String getAvatar() {
26+
return avatar;
27+
}
2228
}

‎DataBindingExample/app/src/main/res/layout/activity_main.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<layout xmlns:android="http://schemas.android.com/apk/res/android">
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
34

45
<data>
56

@@ -18,6 +19,11 @@
1819
android:paddingRight="@dimen/activity_horizontal_margin"
1920
android:paddingTop="@dimen/activity_vertical_margin">
2021

22+
<ImageView
23+
android:layout_width="200px"
24+
android:layout_height="200px"
25+
app:imageUrl="@{user.avatar}"/>
26+
2127

2228
<EditText
2329
android:id="@+id/et_username"

‎DataBindingExample/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:1.2.3'
8+
classpath 'com.android.tools.build:gradle:1.5.0'
99
classpath 'com.android.databinding:dataBinder:1.0-rc1'
1010

1111
// NOTE: Do not place your application dependencies here; they belong

0 commit comments

Comments
 (0)