4

内Wrap_Contentと高さを変更していない私はCardViewを使用して別のカードで満たされているStaggeredLayoutRecyclerViewを持って、それらのカードの内側に空のすべてのフィールドを持つConstraintLayoutは通常あります。すべてがwrap_contentの高さです。ConstraintLayoutが動的にRecyclerView

私の問題は、データがいっぱいになり、その後更新されないうちに、CardViewの高さが計算されているようです。私はすでに​​をのitemViewに設定して、無効にしてみました。その結果、下の項目は親の下端に制約がありますが、問題は残っています。次のように

DataExample example = mItems.get(position); 
Card4ViewHolder vhCon = (Card4ViewHolder) holder; 

vhCon.mIcon.setImageResource(R.drawable.ic_data); 

vhCon.mItem1Label.setText(R.string.A_label); 
vhCon.mItem1.setText(example.getA()); 

vhCon.mItem2Label.setText(R.string.B_label); 
vhCon.mItem2.setText(example.getB()); 

vhCon.mItem3Label.setText(R.string.C_label); 
vhCon.mItem3.setText(example.getC()); 

vhCon.mItem4Label.setText(R.string.D_label); 
vhCon.mItem4.setText(example.getD()); 

RecyclerViewが初期化されている:次のように

フィールド

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_margin="@dimen/home_card_half_min_gutter"> 

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:padding="@dimen/content_spacing"> 


    <ImageView 
     android:id="@+id/iv_icon" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="@dimen/content_spacing" 
     android:contentDescription="@string/icon" 
     app:layout_constraintBottom_toTopOf="@+id/guideline" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:srcCompat="@drawable/ic_airplanemode_inactive_accent_dark_24dp" 
     android:scaleType="fitCenter"/> 

    <android.support.constraint.Guideline 
     android:id="@+id/guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.35" /> 

    <TextView 
     android:id="@+id/tv_item_1_label" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="0dp" 
     android:textAppearance="@style/TextAppearance.AppCompat.Caption" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/guideline5" 
     app:layout_constraintTop_toTopOf="@+id/guideline" 
     tools:text="TextView" /> 

    <TextView 
     android:id="@+id/tv_item_1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/content_half_spacing" 
     android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" 
     app:layout_constraintLeft_toLeftOf="@+id/tv_item_1_label" 
     app:layout_constraintRight_toLeftOf="@+id/guideline5" 
     app:layout_constraintTop_toBottomOf="@+id/tv_item_1_label" 
     tools:text="TextView" /> 

    <TextView 
     android:id="@+id/tv_item_2_label" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="@style/TextAppearance.AppCompat.Caption" 
     app:layout_constraintLeft_toLeftOf="@+id/guideline5" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="@+id/guideline" 
     tools:text="TextView" /> 

    <TextView 
     android:id="@+id/tv_item_2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/content_half_spacing" 
     android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" 
     app:layout_constraintLeft_toLeftOf="@+id/tv_item_2_label" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/tv_item_2_label" 
     tools:text="TextView" /> 

    <TextView 
     android:id="@+id/tv_item_3_label" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/content_spacing" 
     android:textAppearance="@style/TextAppearance.AppCompat.Caption" 
     app:layout_constraintLeft_toLeftOf="@+id/tv_item_1" 
     app:layout_constraintRight_toLeftOf="@+id/guideline5" 
     app:layout_constraintTop_toBottomOf="@+id/tv_item_1" 
     tools:text="TextView" /> 

    <TextView 
     android:id="@+id/tv_item_3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/content_half_spacing" 
     android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" 
     app:layout_constraintLeft_toLeftOf="@+id/tv_item_3_label" 
     app:layout_constraintRight_toLeftOf="@+id/guideline5" 
     app:layout_constraintTop_toBottomOf="@+id/tv_item_3_label" 
     tools:text="TextView" /> 

    <TextView 
     android:id="@+id/tv_item_4_label" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/content_spacing" 
     android:textAppearance="@style/TextAppearance.AppCompat.Caption" 
     app:layout_constraintLeft_toLeftOf="@+id/tv_item_2" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/tv_item_2" 
     tools:text="TextView" /> 

    <TextView 
     android:id="@+id/tv_item_4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/content_half_spacing" 
     android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" 
     app:layout_constraintLeft_toLeftOf="@+id/tv_item_4_label" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/tv_item_4_label" 
     tools:text="TextView" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/guideline5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.5" /> 
</android.support.constraint.ConstraintLayout> 
アダプターのメソッド onBindViewHolder内に充填されている

DisplayMetrics displayMetrics = res.getDisplayMetrics(); 
float pixelWidth = displayMetrics.widthPixels; 
int noOfColumns = (int) (pixelWidth/res.getDimension(R.dimen.home_card_min_width)); 
mLayoutManager = new StaggeredGridLayoutManager(noOfColumns, 
     StaggeredGridLayoutManager.VERTICAL); 
mLayoutManager.setGapStrategy(StaggeredGridLayoutManager 
     .GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); 
mHomeCardAdapter = new HomeCardAdapter(activity); 
mCardsRecyclerView.setAdapter(mHomeCardAdapter); 
mCardsRecyclerView.setLayoutManager(mLayoutManager); 

およびレイアウトをどのここに定義されているフラグメントが内部にあります:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/home_rv_dashboard_cards" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</ScrollView> 

スクロールので、問題なく動作上、RecyclerViewのサイズもon the flyが更新されます。

編集:すでにStaggeredLayoutLinearLayoutManagerに変更し、さらにCardViewを削除してみました。これらのテストの後、私は問題がConstraintLayout自体にあることを確認したようです。

+1

Recyclerviewを投稿できますか? – FARID

+0

元の投稿を編集して詳細を追加しました。 – Raeglan

+0

'ConstraintLayout'の目的は入れ子を避けることではありません。今度は 'CardView'の中に' FrameLayout'をラップすると、それは再び入れ子になり、無駄なオーバードローになります。 –

答えて

1

かなりの時間を捜した後、私はその問題が私の一部の大きな見落としであることに気付きました。私たちは高さが制約によって定義され、かつ制約底がガイドラインであることがわかりImageView

<ImageView 
    ... 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    ... 
    app:layout_constraintBottom_toTopOf="@+id/guideline" 
    ... /> 

ガイドライン自体はあるが、カードの最終高さに基づいてパーセンテージ位置しています。

<android.support.constraint.Guideline 
    android:id="@+id/guideline" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    app:layout_constraintGuide_percent="0.35" /> 

私がここに来た相互依存関係のケースだったそれでは、カード全体の高さは、内の項目によって定義され、画像の高さは、その高さの割合です。私が考えているのは、カードの高さが内側の高さ(画像の0dp)に基づいて検索され、次に画像が拡大縮小され、すべてのものがその下に移動することです。

TLDR:私は相互依存関係を監督し、画像のサイズを設定することで問題が解決されます。

0

私は、これはあなたを助けることができると信じて: 変更:

<android.support.v7.widget.RecyclerView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/home_rv_dashboard_cards" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layoutManager="LinearLayoutManager"/> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/home_rv_dashboard_cards" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</ScrollView> 

次に、あなたのコードからこの行を削除する(あなたはrecyclerviewを初期化する場合):

mLayoutManager = new StaggeredGridLayoutManager(noOfColumns, 
     StaggeredGridLayoutManager.VERTICAL); 
mLayoutManager.setGapStrategy(StaggeredGridLayoutManager 
     .GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS); 
mCardsRecyclerView.setLayoutManager(mLayoutManager); 
+0

これは私が達成しようとしていることを根本的に変えます。私が作業しているプロジェクトでは、グリッド内に異なるカードサイズの非線形カードレイアウトが必要です。しかし、変更を提案しても、 'wrap_content'はまだ動作しません。 'LinearLayoutManager'を使用していても問題が残るので、問題は' CardView'です。 – Raeglan

+0

あなたが得たものと達成したいもののイメージを追加するほうが助けになるかもしれません。私はその方法にもっと役立つだろう;) – FARID

+1

私はちょうど私の問題を解決した答えを掲載した。それは相互依存関係であり、画像のサイズは、そのコンテンツのビューサイズとビューサイズに基づいていました。しかし、あなたの助けてくれてありがとう、私はあなたのコメントといくつかの実験に基づいてCardViewとRecyclerViewを捨てました。 – Raeglan

関連する問題