我在面对这样的问题之前实现了数据绑定。但是,这次创造了很多麻烦我无法在我的项目中导入BR。看看我的gradle和代码。
我尝试了stackoverflow answer的所有可能解决方案。
package com.hcl.developer.telematics.Adapter;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.hcl.developer.telematics.Model.CarResponse;
import com.hcl.developer.telematics.R;
import com.hcl.developer.telematics.Utilities.CustomViewHolder;
import java.util.ArrayList;
import java.util.List;
/**
* Created by nirmal.ku on 9/7/2017.
*/
public class CarDetailsAdapter extends RecyclerView.Adapter<CustomViewHolder>
{
private List<CarResponse> recentCarRequestList = new ArrayList<>();
public CarDetailsAdapter(List<CarResponse> recentCarRequestList) {
this.recentCarRequestList = recentCarRequestList;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.history_list_row, viewGroup, false);
return new CustomViewHolder(binding);
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
ViewDataBinding viewDataBinding = holder.getViewDataBinding();
viewDataBinding.setVariable(BR.car, recentCarRequestList.get(position));
}
@Override
public int getItemCount() {
return (null != recentCarRequestList ? recentCarRequestList.size() : 0);
}
}
MyGradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.hcl.developer.telematics"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
dataBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',
{
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:11.0.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.android.support:multidex:1.0.0'
}
我的XML:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="car"
type="com.hcl.developer.telematics.Model.CarResponse"
/>
</data>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="2dp"
android:background="#000000"
android:orientation="vertical">
<LinearLayout
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:padding="2dp">
<LinearLayout
android:id="@+id/firstLayout"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/txt_RegisterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={car.carRegistrationNumber}"
android:textColor="@android:color/white"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/secondLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6"
android:orientation="vertical">
<LinearLayout
android:id="@+id/firstSecondLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="7dp">
<TextView
android:id="@+id/userName"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@={car.username}"
android:textColor="@android:color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/txt_last_service"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="@={car.lastServiceDate}"
android:textColor="@android:color/white"
android:textSize="8sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/secondSecondLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp">
<TextView
android:id="@+id/car_status"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@={car.carStatus}"
android:textColor="@android:color/white"
android:textSize="10sp" />
<TextView
android:id="@+id/car_type"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="@={car.carType}"
android:textColor="@android:color/white"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/thirdSecondLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginLeft="7dp">
<TextView
android:id="@+id/txt_mileage"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@={car.mileagePerLiter}"
android:textColor="@android:color/white"
android:textSize="10sp" />
<TextView
android:id="@+id/total_trip"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:text="@={car.totalTrip}"
android:textColor="@android:color/white"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
然后是我的模特课:
package com.hcl.developer.telematics.Model;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import com.google.gson.annotations.SerializedName;
public class CarResponse extends BaseObservable {
@SerializedName("username")
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
notifyPropertyChanged(BR.username);
}
}