0

新しいConstraintLayoutを使いこなそうとしています。そして私はRecyclerViewの行のレイアウトを作った。それはmatch_parentに設定されている幅ルート要素としてConstraintLayoutを持っていると私は私のアプリを実行すると、それは私が私のXMLでセクションをデータバインディングを入れて、ルート要素は今後になるまでそれがうまく機能RecyclerViewでDataBindingの幅が正しくないConstraintLayout

correct width

を期待と同じようにレンダリングlayoutタグはConstraintLayoutをラップして、ConstraintLayoutの動作が予期せず変更されることがあります。これ以降の幅はmatch_parentと表示されません。表示の幅はわずかですが、まだXMLではmatch_parentに設定されています。私はまた、いくつかの固定値に幅を設定しようと結果は同じ

enter image description here

たIは、データバインディングプロセッサがちょっとビルドプロセス中ConstrainLayoutプロセスレイアウトから属性ストリップと思われます。物事を正しく行うための方法はありますか? マイレイアウト

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

とアダプタ:

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) { 

    } 
} 
+0

を与えるあなたは 'xml'をあなたのレイアウトを投稿し、そしてまたあなたの' Adapter'& 'RecyclerView'に関連するコードができます? – yennsarah

+0

私の質問を編集しましたが、原因が見つかりました - データバインディングと 'ConstraintLayout'の非互換性です。どうすればそれを連携させるのでしょうか。 –

+1

データバインディングは間違いなく問題ではないと言えます。私はデータ・バインディングと制約レイアウトをリリース以来使っており、問題は一度もありませんでした。また、彼らは本当に問題になることはありません。データバインディングは、作成したものから新しい通常のレイアウトファイルを生成することによって機能します。ビルドフォルダで実際に見ることができます。生成されたどこか。 –

答えて

5

私は本当の原因を把握するために管理@Xaver Kapellerに感謝します。それは私のアダプターコードでした。それは次のように私はonCreateViewHolder()方法を変更しなければならなかった動作させるには:のViewGroupを提供

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

が期待される結果

関連する問題