๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ฑ/๐Ÿ“˜Project

[Kotlin] ๋„์„œ ๊ฒ€์ƒ‰ ์•ฑ ๋งŒ๋“ค๊ธฐ๏ผ‘

๋„์„œ ๊ฒ€์ƒ‰ ์•ฑ์„ ๋งŒ๋“ค๊ธฐ ์ „์— Retrofit2์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•ด๋ด…์‹œ๋‹ค.

 

โ–ช๏ธFragment

https://jslee-tech.tistory.com/13

 

(Retrofit2) 1. ์‹œ์ž‘ํ•˜๊ธฐ

1. Retrofit2 ๋ž€? Android์—์„œ REST API ํ†ต์‹ ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Type-Safe ํ•œ HTTP ํด๋ผ์ด์–ธํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ Client๊ฐ€ ํ•„์š”ํ•œ ํ˜•ํƒœ์˜ ๊ฐ์ฒด๋กœ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ ์กด์žฌํ•˜๋Š” HTTP ํ†ต์‹ 

jslee-tech.tistory.com

 

๋จผ์ € UI ๊ตฌ์„ฑ๋ถ€ํ„ฐ ํ–ˆ์œผ๋ฉฐ ์ด 5๊ฐœ์˜ ํŒŒ์ผ๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

 

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/act_main_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragment.HomeFragment"
    android:padding="5dp">

    <LinearLayout
        android:id="@+id/fra_hom_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SearchBook"
            android:textSize="20dp"
            android:textColor="@color/black"
            android:fontFamily="@font/gothic_bold"
            android:layout_gravity="center"/>

    </LinearLayout>

    <Space
        android:id="@+id/fra_hom_space1"
        android:layout_width="match_parent"
        android:layout_height="5dp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fra_hom_top"/>

    <LinearLayout
        android:id="@+id/fra_hom_search_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp"
        android:background="@drawable/background_round"
        android:backgroundTint="#EAEAEA"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fra_hom_space1">

        <EditText
            android:id="@+id/fra_hom_search_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="์ฑ… ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”"
            android:textSize="15dp"
            android:textColor="@color/black"
            android:fontFamily="@font/gothic_bold"
            android:background="@null"
            android:padding="5dp"
            android:inputType="text"
            android:maxLines="1"/>

        <Space
            android:layout_width="5dp"
            android:layout_height="match_parent"/>

        <ImageView
            android:id="@+id/fra_hom_search_button"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/icon_search"
            android:background="#EAEAEA"
            android:layout_gravity="center"/>

    </LinearLayout>

    <Space
        android:id="@+id/fra_hom_space2"
        android:layout_width="match_parent"
        android:layout_height="10dp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fra_hom_search_bar"/>

    <TextView
        android:id="@+id/fra_hom_empty_history"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="โ—์ตœ๊ทผ ๊ฒ€์ƒ‰ํ•œ ์ฑ…์ด ์—†์Šต๋‹ˆ๋‹ค"
        android:textSize="20dp"
        android:textColor="@color/black"
        android:fontFamily="@font/gothic_regular"
        android:gravity="center"
        android:visibility="gone"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fra_hom_space2"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <LinearLayout
        android:id="@+id/fra_hom_history"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fra_hom_space2"
        app:layout_constraintBottom_toBottomOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="โ–ช์ตœ๊ทผ ๊ฒ€์ƒ‰ํ•œ ์ฑ…"
                android:textSize="17dp"
                android:textColor="@color/black"
                android:fontFamily="@font/gothic_bold"/>

            <Space
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <TextView
                android:id="@+id/fra_hom_history_delete_all"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="๋น„์šฐ๊ธฐ"
                android:textSize="13dp"
                android:textColor="#E91E63"
                android:fontFamily="@font/gothic_regular"
                android:textStyle="bold"
                android:layout_gravity="center"/>

        </LinearLayout>

        <Space
            android:layout_width="match_parent"
            android:layout_height="5dp"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/fra_hom_history_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

fragment_searce.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragment.SearchFragment"
    android:padding="5dp">

    <LinearLayout
        android:id="@+id/fra_sea_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/fra_sea_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_back"
            android:layout_gravity="center"/>

        <Space
            android:layout_width="5dp"
            android:layout_height="match_parent"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:background="@drawable/background_round"
            android:backgroundTint="#EAEAEA"
            android:padding="5dp">

            <EditText
                android:id="@+id/fra_sea_search_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="์ฑ… ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”"
                android:textSize="15dp"
                android:textColor="@color/black"
                android:fontFamily="@font/gothic_bold"
                android:background="@null"
                android:padding="5dp"
                android:inputType="text"
                android:maxLines="1"/>

            <Space
                android:layout_width="5dp"
                android:layout_height="match_parent"/>

            <ImageView
                android:id="@+id/fra_sea_search_button"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/icon_search"
                android:background="#EAEAEA"
                android:layout_gravity="center"/>

        </LinearLayout>

    </LinearLayout>

    <Space
        android:id="@+id/fra_sea_space1"
        android:layout_width="match_parent"
        android:layout_height="10dp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fra_sea_top"/>

    <TextView
        android:id="@+id/fra_sea_empty_search_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="โ—๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค"
        android:textSize="20dp"
        android:textColor="@color/black"
        android:fontFamily="@font/gothic_regular"
        android:gravity="center"
        android:visibility="gone"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fra_sea_space1"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/fra_sea_search_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:visibility="gone"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fra_sea_space1"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

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

    <TextView
        android:id="@+id/history_list_item_search_word"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="์ฑ… ์ œ๋ชฉ"
        android:textColor="@color/black"
        android:textSize="15dp"
        android:fontFamily="@font/gothic_bold"
        android:layout_gravity="center"/>

    <ImageView
        android:id="@+id/history_list_item_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_delete"/>

</LinearLayout>

 

search_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:padding="10dp"
    android:background="@drawable/list_round"
    android:backgroundTint="#EAEAEA">

    <ImageView
        android:id="@+id/search_list_item_thumbnail"
        android:layout_width="170dp"
        android:layout_height="245dp"
        android:background="@color/black"/>

    <Space
        android:layout_width="5dp"
        android:layout_height="match_parent"/>

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

        <TextView
            android:id="@+id/search_list_item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/gothic_regular"
            android:text="์ฑ… ์ œ๋ชฉ"
            android:textColor="@color/black"
            android:textSize="16dp"
            android:textStyle="bold"/>

        <Space
            android:layout_width="match_parent"
            android:layout_height="5dp"/>

        <TextView
            android:id="@+id/search_list_item_authors"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/gothic_regular"
            android:text="์ €์ž"
            android:textColor="@color/black"
            android:textSize="14dp"/>

        <Space
            android:layout_width="match_parent"
            android:layout_height="5dp"/>

        <TextView
            android:id="@+id/search_list_item_contents"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/gothic_regular"
            android:text="์ฑ… ์ค„๊ฑฐ๋ฆฌ"
            android:textColor="@color/black"
            android:textSize="15dp"
            android:maxLines="7"/>

        <Space
            android:layout_width="match_parent"
            android:layout_height="5dp"/>

        <TextView
            android:id="@+id/search_list_item_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/gothic_regular"
            android:text="์ •๊ฐ€"
            android:textColor="@color/black"
            android:textSize="14dp"/>

        <Space
            android:layout_width="match_parent"
            android:layout_height="5dp"/>

        <TextView
            android:id="@+id/search_list_item_status"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/gothic_regular"
            android:text="ํŒ๋งค์—ฌ๋ถ€"
            android:textColor="@color/black"
            android:textSize="14dp"/>

        <Space
            android:layout_width="match_parent"
            android:layout_height="5dp"/>

        <TextView
            android:id="@+id/search_list_item_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="false"
            android:fontFamily="@font/gothic_bold"
            android:text="๐Ÿ” ์ฑ… ์ •๋ณด ์ž์„ธํžˆ ๋ณด๊ธฐ"
            android:textColor="#2196F3"
            android:textSize="14dp"/>

    </LinearLayout>

</LinearLayout>

 

activity_main.xml : Fragment๋ฅผ ๋„์›Œ ์ค„ ์•กํ‹ฐ๋น„ํ‹ฐ

fragment_home.xml : ๋ฉ”์ธ ํ™”๋ฉด์ด ๋  ํ”„๋ž˜๊ทธ๋จผํŠธ

fragment_search.xml : ๊ฒ€์ƒ‰ ํ™”๋ฉด์ด ๋  ํ”„๋ž˜๊ทธ๋จผํŠธ

history_list_item.xml : ๊ฒ€์ƒ‰ ๊ธฐ๋ก์— ๋„์›Œ์งˆ ํ•ญ๋ชฉ

search_list_item.xml : ์ž…๋ ฅํ•œ ๊ฒ€์ƒ‰์–ด์™€ ์ผ์น˜ํ•˜๋Š” ์ฑ…๋“ค์„ ๋„์›Œ ์ค„ ํ”„๋ž˜๊ทธ๋จผํŠธ

 

To be Continued..๐Ÿง