0

I have fragment with RecycleView and Floating Action Button to add new object.

POJO:

public class MockCar extends BaseObservable {
private String name;
private String plateNumber;

public MockCar(String name, String plateNumber) {
    this.name = name;
    this.plateNumber = plateNumber;
}

@Bindable
public String getName() {
    return name;
}

@Bindable
public void setName(String name) {
    this.name = name;
    notifyPropertyChanged(BR.car);
}

@Bindable
public String getPlateNumber() {
    return plateNumber;
}

@Bindable
public void setPlateNumber(String plateNumber) {
    this.plateNumber = plateNumber;
    notifyPropertyChanged(BR.car);
}
}

item.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>

    <variable
        name="car"
        type="com.igor.touchless.model.MockCar"/>
    <variable
        name="click"
        type="com.igor.touchless.adapter.interfaces.CarClickHandler"/>
</data>

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:layout_gravity="center"
    android:padding="4dp"
    android:background="#FFF"
    card_view:cardCornerRadius="6dp">

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

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@{car.name}"
            android:textColor="@color/primary_text"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/plate_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            android:text="@{car.plateNumber}"
            android:textColor="@color/secondary_text"
            android:textSize="13sp" />

        <ImageView
            android:id="@+id/button_edit"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_alignParentRight="true"
            android:background="@color/primary"
            android:src="@drawable/ic_create_black_24dp"
            app:civ_border_color="#00EEEEEE"
            app:civ_border_width="0dp"
            app:civ_shadow="false"
            app:civ_shadow_color="#008BC34A"
            app:civ_shadow_radius="0" />


        <ImageView
            android:id="@+id/button_remove"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="#F44336"
            android:src="@drawable/ic_clear_black_24dp" />

    </RelativeLayout>

</android.support.v7.widget.CardView>
</layout>

After pressing floating action button I add new item to collection

 public void add(MockCar car) {
    carList.add(car);
    notifyDataSetChanged();
}

The problem is next - when I try to add elements more then can fit in the screen (8th and all next) I can see that recycler view twitches, doesn't matter I am at the beginning of the list or at the end. As far as I understand it updated all views if I update ArrayList or try to change any property of the object. I assume I made something wrong with it notifyPropertyChanged(BR.car), but have no idea how to deal with it.

If I use standard approach everything works as expected.

I just started learning android data binding. Sure somebody has already faced with this problem. Thanks in advance.

Peter Parker
  • 560
  • 1
  • 5
  • 17
  • Try using `notifyItemAdded()` instead of `notifyDataSetChanged()`. I think the RecyclerView is rebuilding the entire list on you. – George Mount Mar 03 '17 at 23:58
  • @GeorgeMount , you're right! It helped. But now it is interesting for me, why `notifyDataSetChanged()` worked for standard approach, and didn't work as expected with `data binding`? – Peter Parker Mar 04 '17 at 08:01
  • Possible duplicate of [notifyItemInserted() in RecyclerView and animation](http://stackoverflow.com/questions/32399115/notifyiteminserted-in-recyclerview-and-animation) – Sergei Bubenshchikov Mar 04 '17 at 14:23
  • I'm guessing that you didn't run `executePendingBindings()` after you set the values in your `onBindViewHolder()` call. Check out: https://medium.com/google-developers/android-data-binding-recyclerview-db7c40d9f0e4#.auq377t6m – George Mount Mar 05 '17 at 00:26

0 Answers0