Mi Lugarcito

공공데이터 파싱예제 - 미세먼지 Android 앱 만들기 본문

API

공공데이터 파싱예제 - 미세먼지 Android 앱 만들기

selene park 2021. 1. 29. 12:38

미세먼지 Open API - 네트워크 통신 : Retrofit2

현재 위치 : FusedLocationProvider

지역 저장, 삭제 Realm

새로 고침 SwipeRefreshLayout

 

 

#뷰 만들기

content_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:orientation="vertical"
    tools:showIn="@layout/app_bar_main">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        app:tabTextColor="#000000"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

 

 

bundle.app에서 vectorDrawables.useSupportLibrary=true  추가하기

버전이 17이하로 낮을때는 (하위버전에서는 vector drawable 아이콘이 안나타남)

vectorDrawables.useSupportLibrary=true 추가해서

app:srcCompat="@drawable/ic_baseline_location_on_24"   --> 사용가능하도록 만들어야한다.

(하위버전 이미지뷰는 src 가 아니라 srcCompat 사용해야한다.)

 

 

 

fragment_fine_dust.xml 

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    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">

    <LinearLayout

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


        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="8dp"
            app:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="8dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        app:srcCompat="@drawable/ic_baseline_location_on_24"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:paddingLeft="4dp"
                        android:paddingStart="4dp"
                        android:text="관측장소"
                        android:textAppearance="@style/TextAppearance.AppCompat.Large"
                        />
                </LinearLayout>
                <TextView
                    android:id="@+id/result_location_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="8dp"
                    android:paddingLeft="24dp"
                    android:paddingStart="24dp"
                    android:paddingTop="8dp"
                    tools:text="수원시"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
            </LinearLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="8dp"
            app:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="8dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        app:srcCompat="@drawable/ic_baseline_access_time_24"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:paddingLeft="4dp"
                        android:paddingStart="4dp"
                        android:text="관측시간"
                        android:textAppearance="@style/TextAppearance.AppCompat.Large"
                        />
                </LinearLayout>
                <TextView
                    android:id="@+id/result_time_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="8dp"
                    android:paddingLeft="24dp"
                    android:paddingStart="24dp"
                    android:paddingTop="8dp"
                    tools:text="수원시"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
            </LinearLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="8dp"
            app:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="8dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="4dp">

                    <ImageView
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        app:srcCompat="@drawable/ic_baseline_blur_on_24"
                        />
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:paddingLeft="4dp"
                        android:paddingStart="4dp"
                        android:text="미세먼지"
                        android:textAppearance="@style/TextAppearance.AppCompat.Large"
                        />
                </LinearLayout>
                <TextView
                    android:id="@+id/result_dust_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="8dp"
                    android:paddingLeft="24dp"
                    android:paddingStart="24dp"
                    android:paddingTop="8dp"
                    tools:text="수원시"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
            </LinearLayout>
        </androidx.cardview.widget.CardView>

    </LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

 

 

#SwipeRefreshLayout사용하려면

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" 라이브러리 추가하기

 

최신버전은 아래 링크에서 참조하기

 

developer.android.com/jetpack/androidx/releases/swiperefreshlayout

 

Swiperefreshlayout  |  Android 개발자  |  Android Developers

Swiperefreshlayout 스와이프하여 새로고침 UI 패턴을 구현합니다. 최근 업데이트 현재 안정화 버전 다음 버전 후보 베타 버전 알파 버전 2020년 7월 22일 1.1.0 - - 1.2.0-alpha01 종속 항목 선언 SwipeRefreshLayout

developer.android.com