Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout with DataBinding incorrect width in RecyclerView

I am trying to play around with new ConstraintLayout. And I made the layout for my RecyclerView's row. It has ConstraintLayout as root element which width is set to match_parent and when I run my app it renders just as expected

correct width

It works well until I put data binding section in my XML and the root element henceforth is layout tag which wraps ConstraintLayout and it results in some unpredictable change in behavior of ConstraintLayout - it's width henceforth doesn't renders as match_parent, it only takes small part of display width, though it's still set to match_parent in XML. I also tried to set width to some fixed value and the result was the same

enter image description here

I suspect that Data Binding processor kinda strips attributes from ConstrainLayout when process layouts during the build process. Is there any way to get things work correctly? My layout

<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <import type="android.view.View"/>
        <variable
            name="post"
            type=".entity.Post"/>
        <variable
            name="data"
            type=".entity.LinkPostData"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="16dp">
        ....
    </android.support.constraint.ConstraintLayout>
</layout>

And adapter:

class PostsAdapter(): RecyclerView.Adapter<PostsAdapter.PostItemViewHolder>() {

    private val posts: MutableList<Post> = ArrayList()

    fun setPosts(domains: List<Post>) {
        this.posts.clear()
        this.posts.addAll(domains)
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        return super.getItemViewType(position)
    }

    override fun onBindViewHolder(holder: PostItemViewHolder?, position: Int) {
        val post = posts[position]

        holder?.binding?.post = post
        holder?.binding?.data = post.data as LinkPostData
        holder?.binding?.executePendingBindings()
    }

    override fun getItemCount() = this.posts.size

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): PostItemViewHolder {
        val binding = ItemPostBinding.inflate(LayoutInflater.from(parent?.context))
        return PostItemViewHolder(binding)
    }

    class PostItemViewHolder(val binding: ItemPostBinding): RecyclerView.ViewHolder(binding.root) {

    }
}
like image 982
Oleg Osipenko Avatar asked Jul 10 '16 10:07

Oleg Osipenko


1 Answers

Thanks to @Xaver Kapeller I managed to figure out the real cause. And it was my adapter code. To make it work I had to change onCreateViewHolder() method as follows:

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): PostItemViewHolder {
    val binding = ItemPostBinding.inflate(LayoutInflater.from(parent?.context), parent, false)
    return PostItemViewHolder(binding)
}

Providing ViewGroup gives expected result

like image 98
Oleg Osipenko Avatar answered Nov 20 '22 01:11

Oleg Osipenko