LoginSignup
5
5

More than 5 years have passed since last update.

RecyclerView, GridLayoutManagerにCardViewを直接いれてはならない。

Last updated at Posted at 2015-07-25

RecyclerViewのLayoutManagerにGridLayoutManagerをセットする場合、そのアイテムのレイアウトとしてこのような実装をするときちんとマージンが反映されずに、カード自体が小さくなってしまう。

ダメな例

card_item.xml
<CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">
    <!--Some views -->
</CardView>

なので、GridLayoutManagerでCardViewを使用する場合、このように外側にFrameLayoutを入れる必要がある。

正しい例

card_item.xml
<Frame_Layout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">
        <!--Some views -->
    </CardView>
</FrameLayout>

なおLinearLayoutManagerではFrameLayoutなしでもちゃんと表示される。CardView以外でどうなるかは調べていないのでわかりません。

追記2015/7/28

これでもCardViewのサイズは指定通りにセット出来ますが、marginがおかしくなりますね。例えばmarginを4dpにセットした場合なぜか実際は8dpになります。なので、marginを1/2した値を設定しなければならない? これはバグなのだろうか。。。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5