๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ 2๋‹จ์œผ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฉ๋ฒ•(GridLayoutManager) ๋ณธ๋ฌธ

๐Ÿค–Android/Java

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ 2๋‹จ์œผ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฉ๋ฒ•(GridLayoutManager)

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 2. 6. 22:58
๋ฐ˜์‘ํ˜•

 

๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ณ  ์—ฐ์Šตํ•˜๋ฉด์„œ ํ•œ ๋ฒˆ๋„ 2๋‹จ์œผ๋กœ ์“ธ ์ƒ๊ฐ์„ ๋ชปํ•˜๋‹ค๊ฐ€.
์ด๋ฒˆ ํ”„๋กœ์ ํŠธ ๋•Œ ํ™”๋ฉด ๊ณ„ํš์„œ๋ฅผ ๋งŒ๋“ค๋ฉด์„œ ๋งŒ๋“ค์–ด๋ณด๋Š” ๋ฐฉ๋ฒ•์„ ์ตํ˜”๋‹ค.
์ด๊ฒŒ ์—„์ฒญ ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•์€ ์•„๋‹์ง€ ๋ชฐ๋ผ๋„ 2๋‹จ์œผ๋กœ ๋‚˜์˜ค๋‹ˆ ํ›จ์”ฌ ์™„์„ฑ๋„๊ฐ€ ๋†’์•„์กŒ๋‹ค.

 

1. xml ํ™”๋ฉด์„ค๊ณ„

- row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:orientation="vertical"
    android:padding="3dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="150dp"
        android:layout_height="170dp"
        android:backgroundTint="#FFFFFF"
        app:cardCornerRadius="15dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/thumbRecipe"
                android:layout_width="170dp"
                android:layout_height="100dp"
                android:scaleType="centerCrop"
                android:src="@drawable/beaf" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:orientation="horizontal"
                android:paddingLeft="3dp"
                android:paddingTop="3dp">

                <ImageView
                    android:id="@+id/imageView6"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:src="@drawable/account_circle" />

                <TextView
                    android:id="@+id/txtNickname"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:layout_marginLeft="2dp"
                    android:layout_weight="2"
                    android:gravity="center_vertical"
                    android:text="๋‹‰๋„ค์ž„"
                    android:textColor="#111111"
                    android:textSize="12sp" />

                <ImageView
                    android:id="@+id/btnScrap"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/bookmark_18px" />

            </LinearLayout>

            <TextView
                android:id="@+id/txtRecipeView"
                android:layout_width="match_parent"
                android:layout_height="34dp"
                android:layout_margin="5dp"
                android:maxLines="2"
                android:text="์š”๋ฆฌ ์ œ๋ชฉ"
                android:textSize="11sp" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

 

- main_activity.xml

// ์ƒ๋žต

<androidx.gridlayout.widget.GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        nestedScrollingEnabled="false"
        android:layout_width="match_parent"
        android:layout_height="820dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp" />
</androidx.gridlayout.widget.GridLayout>

// ์ƒ๋žต

 

2. MainActivity.java GridLayoutManager ์„ค์ •

์ž๋ฐ” ํŒŒ์ผ์—์„œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ

recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ”์ค€๋‹ค.

GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(gridLayoutManager);

// ๊ธฐ์กด ์ฝ”๋“œ
//recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

 

3. ์‹คํ–‰ํ™”๋ฉด

์™ผ์ชฝ์ด GridLayoutManager ์„ค์ • ์ „ / ์˜ค๋ฅธ์ชฝ์ด ์„ค์ • ํ›„ ํ™”๋ฉด์ด๋‹ค.

๋ฐ˜์‘ํ˜•