Android数据绑定无法在回收视图上正常运行

时间:2016-08-30 09:11:34

标签: android android-recyclerview android-databinding

我有RecycleView包含我希望使用android数据绑定来填充我的 Recycleview 的每一行的文本和时间的消息列表但我有一个奇怪的问题,即绑定消息后每行TextView的文字和时间都会填充空白文字,就像您没有设置任何文字一样!

我的RecyceView适配器:

 @Override
public BaseRVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    layoutInflater=LayoutInflater.from(parent.getContext());
    viewGroup=parent;
    View view= layoutInflater.inflate(layout[viewType], null);
    try {
        return holder.getConstructor(View.class,LayoutInflater.class,ViewGroup.class,Context.class).newInstance(view,layoutInflater,viewGroup,context);
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}

public Object getItem(int position){
    if(position<items.size())
        return items.get(position);
    return null;
}

@Override
public void onBindViewHolder(BaseRVHolder holder, int position) {
    if(holder!=null)
        holder.fill(context,layoutInflater,viewGroup,items.get(position),position,getItemViewType(position),getObjects());

}
 @Override
        public int getItemViewType(int position) {
            if(messages.get(position).getFrom() == homeId){
                return 1;
            }
            return 0;
        }
我的ViewHolder消息的

和fill()方法:

@Override
    public void fill(final Context context, LayoutInflater layoutInflater, ViewGroup viewGroup, final Message message, int pos, int viewType, Object... objects) {
        if(viewType==0) {
            AdapterMessageSendActivitytBinding binding = AdapterMessageSendActivitytBinding.inflate(this.inflater);
            binding.setMessage(message);
            binding.notifyChange();
            binding.invalidateAll();
        }else {
            AdapterMessageReceiveActivityBinding binding =AdapterMessageReceiveActivityBinding.inflate(this.inflater);
            binding.setMessage(message);
        }
    }

Message.java:

public class Message extends BaseModel implements Serializable{

@PrimaryKey(autoincrement = true)
private int id;
@Column
private int from;
@Column
private String username;
@Column
private String text;
@Column
transient DateTime dateTime;

private List<ToMessage> recipients;

//getter and setters are here...

和适配器的布局:

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="message"
            type="com.example.familydesk.model.Message"></variable>
    </data>
    <LinearLayout
        android:orientation="horizontal"
        android:padding="5dp"

        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView android:layout_width="56dp" android:layout_height="56dp"
            android:src="@drawable/house_logo"/>
        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
            android:background="@drawable/chat_back_white_nine_patch"
            android:layout_marginRight="10dp"
            android:orientation="vertical">
            <TextView android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/contentTv"
                android:text="@{message.text}"
                android:gravity="right"/>
            <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
                android:text="@{message.dateTime.toString()}"
                android:gravity="right"
                android:id="@+id/time_dateTv"
                android:layout_marginTop="5dp"
                android:singleLine="true"/>
        </LinearLayout>

    </LinearLayout>
</layout>

我的代码有什么问题?任何人都可以帮助PLZ吗?

1 个答案:

答案 0 :(得分:0)

ViewHolder的构造函数中创建绑定对象,如下所示:

public BaseRVHolder(View view){
    binding = DataBindingUtil.bind(view);
}

并且不要忘记在设置值后调用executePendingBindings()

binding.setMessage(message);
binding.executePendingBindings();

请参阅this教程以获取更多信息。