'2020/07/15'에 해당되는 글 1건

728x90

메인 화면 구성을 위해서 구글에서 Github 검색으로 https://github.com/Whhoesj/sliding-content-drawer 를 하나 찾아서 테스트하고 간단하게 적어둔다.

 


화면 구성은 위와 같다.

https://link2me.tistory.com/1810 에 Navigation Drawer 구성에 대한 개념이 설명되어 있는데, SlidingContentDrawer 는 아래 Java 코드만 살펴보면 이해될 것이다.


Project build.gradle

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://navercorp.bintray.com/maps' }
        maven { url "https://jitpack.io" }
    }
}


앱 build.gradle

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'gun0912.ted:tedpermission:2.0.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.wouterhabets:slidingcontentdrawer:1.0'
    implementation 'com.naver.maps:map-sdk:3.7.1' // 네이버 지도 SDK
    implementation 'com.google.android.gms:play-services-location:17.0.0'
}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.link2me.android.slidingcontentdrawer">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="false"
        android:theme="@style/AppTheme.NoActionBar">
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MapActivity" />
        <activity android:name=".MainActivity" />

        <meta-data
            android:name="com.naver.maps.map.CLIENT_ID"
            android:value="@string/naver_app_key" />
    </application>

</manifest>


strings.xml

<resources>
    <string name="app_name">SlidingContentDrawer</string>
    <string name="navigation_drawer_open">Open navigation drawer</string>
    <string name="navigation_drawer_close">Close navigation drawer</string>
    <string name="naver_app_key">여기에 키를 적어주세요</string>
</resources>


Drawer Layout 구성

- Layout 구성을 버전에 맞춰 변경 수정했다.

- 파일을 3개의 파일로 분리 구성했다.

<?xml version="1.0" encoding="utf-8"?>
<com.wouterhabets.slidingcontentdrawer.widget.SlidingDrawerLayout
    android:id="@+id/drawer_layout"
    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="match_parent"
    android:background="?attr/colorPrimaryDark"
    android:fitsSystemWindows="true">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/background"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="menu">

        <include layout="@layout/drawer_menu"/>

    </FrameLayout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:colorBackground"
        android:elevation="6dp"
        android:fitsSystemWindows="true"
        android:tag="content">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:background="@android:color/holo_green_light"/>

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </com.google.android.material.appbar.AppBarLayout>

        <include
            layout="@layout/drawer_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</com.wouterhabets.slidingcontentdrawer.widget.SlidingDrawerLayout>

<?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:layout_gravity="start|center_vertical"
    android:orientation="vertical"
    android:paddingLeft="16dp">

    <TextView
        android:id="@+id/navermap_to"
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_gravity="start|center_vertical"
        android:text="네이버 지도"
        android:layout_marginBottom="30dp"
        android:textColor="@android:color/white"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_gravity="start|center_vertical"
        android:text="Menu item 2"
        android:layout_marginBottom="30dp"
        android:textColor="@android:color/white"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_gravity="start|center_vertical"
        android:text="Menu item 3"
        android:layout_marginBottom="30dp"
        android:textColor="@android:color/white"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_gravity="start|center_vertical"
        android:text="Menu item 4"
        android:layout_marginBottom="30dp"
        android:textColor="@android:color/white"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="42dp"
        android:layout_gravity="start|center_vertical"
        android:text="Menu item 5"
        android:textColor="@android:color/white"/>
</LinearLayout>

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/app_name"/>
    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>



MainActivity.java 구현

package com.link2me.android.slidingcontentdrawer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.link2me.android.common.BackPressHandler;
import com.link2me.android.common.PrefsHelper;
import com.wouterhabets.slidingcontentdrawer.widget.SlidingDrawerLayout;
import com.wouterhabets.slidingcontentdrawer.widget.SlidingDrawerToggle;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = this.getClass().getSimpleName();
    Context mContext;
    private BackPressHandler backPressHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        backPressHandler = new BackPressHandler(this); // 뒤로 가기 버튼 이벤트

        initView();
    }

    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        SlidingDrawerLayout drawer = (SlidingDrawerLayout) findViewById(R.id.drawer_layout);
        SlidingDrawerToggle toggle = new SlidingDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        // 메뉴 버튼 클릭 리스너
        findViewById(R.id.navermap_to).setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        SlidingDrawerLayout drawer = (SlidingDrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen()) {
            drawer.closeDrawer();
        } else {
            backPressHandler.onBackPressed();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.navermap_to:
                Intent intent = new Intent(mContext,MapActivity.class);
                startActivity(intent);
                break;
        }
    }
}


MapActivity.java 구현

package com.link2me.android.slidingcontentdrawer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.link2me.android.common.PrefsHelper;
import com.naver.maps.geometry.LatLng;
import com.naver.maps.map.CameraAnimation;
import com.naver.maps.map.CameraUpdate;
import com.naver.maps.map.LocationTrackingMode;
import com.naver.maps.map.MapView;
import com.naver.maps.map.NaverMap;
import com.naver.maps.map.OnMapReadyCallback;
import com.naver.maps.map.UiSettings;
import com.naver.maps.map.overlay.InfoWindow;
import com.naver.maps.map.util.FusedLocationSource;
import com.naver.maps.map.widget.LocationButtonView;

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
    private final String TAG = this.getClass().getSimpleName();
    Context mContext;

    // NaverMap API 3.0
    private MapView mMapView;
    private LocationButtonView locBtnView;
    DrawerLayout drawer_layout;
    ImageView btn_drawer;
    CardView cardView;

    // FusedLocationSource (Google)
    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1000;
    private FusedLocationSource locationSource;

    private boolean  isFirstLocation = true;
    private NaverMap mNaverMap;
    private InfoWindow mInfoWindow; // 지도 위에 올리는 정보 창을 정의
    private boolean showMarket = false;

    String latitude;
    String longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mContext = MapActivity.this;

        // https://console.ncloud.com/mc/solution/naverService/application 에서 패키지명 등록 확인부터하라.
        mMapView = findViewById(R.id.map_view);
        mMapView.onCreate(savedInstanceState);
        mMapView.getMapAsync(this);

        // 내 현재 위치 버튼
        locBtnView = findViewById(R.id.btn_curr_location);
        // implementation "com.google.android.gms:play-services-location:17.0.0" 안해주면 에러 발생
        // 내 위치 찾기 위한 소스
        locationSource = new FusedLocationSource(this,LOCATION_PERMISSION_REQUEST_CODE);

        FloatingActionButton fab_message = findViewById(R.id.fab_message);
        fab_message.setOnClickListener(view -> {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        });
    }

    @Override
    public void onMapReady(@NonNull NaverMap naverMap) {
        UiSettings uiSettings = naverMap.getUiSettings();
        uiSettings.setLocationButtonEnabled(false);
        uiSettings.setCompassEnabled(true);

        // 카메라 초기 위치 설정
        LatLng initialPosition = new LatLng(37.506855, 127.066242);

        mNaverMap = naverMap;

        locBtnView.setMap(naverMap);

        // Location Change Listener를 사용하기 위한 FusedLocationSource 설정
        naverMap.setLocationSource(locationSource);
        naverMap.setLocationTrackingMode(LocationTrackingMode.NoFollow); // LocationTrackingMode.Face

        naverMap.addOnLocationChangeListener(location -> {
            if (isFirstLocation) { // 현재 위치로 초기 화면 이동
                naverMap.setLocationTrackingMode(LocationTrackingMode.None);
                LatLng initialPosition1 = new LatLng(location.getLatitude(),location.getLongitude()); // 현재 위치
                latitude = String.valueOf(location.getLatitude());
                longitude = String.valueOf(location.getLongitude());
                PrefsHelper.write("latitude",latitude);
                PrefsHelper.write("longitude",longitude);
                //Log.e(TAG,"latitude:"+latitude+" , longitude:"+longitude);
                CameraUpdate cameraUpdate = CameraUpdate.scrollTo(initialPosition1).animate(CameraAnimation.Easing);
                naverMap.moveCamera(cameraUpdate);
            }
            isFirstLocation = false;
        });

    }
}


지도 처리를 위해서는 Naver Cloud 에 앱패키지명을 등록해야 한다.


테스트에 사용한 파일을 첨부한다.

slidingcontentdrawer.zip


블로그 이미지

Link2Me

,