1. 네비게이션드로어 만드는 방법
finish
2. 전체 구조
-총 9개 파일-
Activtiy7.java - 메인
activity7_main.xml - 메인뷰 + NevigationView사용하는거(포함)
nav_header_7.xml - 네비게이션드로어 뷰(옆으로 나와서 보여줄 화면)
Fragment7_1, 7_2, 7_3.java - 프래그먼트
FragmentCallback.java -
activity7.xml - 옵션 아이템(아이콘)
activity7_main_drawer.xml - 네비게이션드로어 아이템(아이콘)
3. 결과
4. 코드
app:headerLayout="@layout/nav_header_7" 속성은 nav_header_7.xml을 네비게이션드로어(바로가기)에 보여줄 xml파일을 설정 해주는 겁니다.
Activtiy7.java - 메인
public class Activity7 extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,FragmentCallback {
// 프레임레이아웃 안에다가 프래그먼트 추가할려고
Fragment7_1 f1;
Fragment7_2 f2;
Fragment7_3 f3;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity7_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // 화면에 툴바를 설정 할려면! 꼭 넣주기
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// 프래그먼트 생성
f1 = new Fragment7_1();
f2 = new Fragment7_2();
f3 = new Fragment7_3();
// 프래임레이아웃에 프래그먼트 추가하는 방법
getSupportFragmentManager().beginTransaction().add(R.id.container, f1).commit();
}
// 이거 인터페이스 만들어서 사용함
@Override
public void onFragmentSelected(int position, Bundle bundle) {
Fragment curenFragment =null;
if(position == 0){
curenFragment = f1;
toolbar.setTitle("첫번째 화면");
}else if(position ==1){
curenFragment =f2;
toolbar.setTitle("두번째 화면");
}else if(position ==2){
curenFragment = f3;
toolbar.setTitle("셋번째 화면");
}
// 지정한 걸로 화면 보여줄려고 한다면!!
getSupportFragmentManager().beginTransaction().replace(R.id.container, curenFragment).commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity7, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_0) {
Toast.makeText(this,"첫번째11",Toast.LENGTH_LONG).show();
onFragmentSelected(0,null);
} else if (id == R.id.nav_1) {
Toast.makeText(this,"두번째22",Toast.LENGTH_LONG).show();
onFragmentSelected(1,null);
} else if (id == R.id.nav_2) {
Toast.makeText(this,"셋번째33",Toast.LENGTH_LONG).show();
onFragmentSelected(2,null);
} else if (id == R.id.xxxx1) {
// 확인 할려고( 어떻게 나오는지 )
} else if (id == R.id.xxxx2) {
// 확인 할려고( 어떻게 나오는지 )
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
activity7_main.xml - 메인뷰 + NevigationView사용하는거(포함)
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="nevigationdrawer7.Activity7">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.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" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_7"
app:menu="@menu/activity7_main_drawer" />
</android.support.v4.widget.DrawerLayout>
nav_header_7.xml - 네비게이션드로어 뷰(옆으로 나와서 보여줄 화면)
<LinearLayout 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="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@drawable/heart" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="닉네임"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="부가설명~~~~~~~~~~~~~~~~~~~" />
</LinearLayout>
Fragment7_1, 7_2, 7_3.java - 프래그먼트
FragmentCallback.java -
activity7.xml - 옵션 아이템(아이콘)
activity7_main_drawer.xml - 네비게이션드로어 아이템(아이콘)
'■ Android > Tip' 카테고리의 다른 글
[Android] include<태그> -"중복 코드 해결" (0) | 2020.02.05 |
---|---|
[Android] 네비게이션드로어 메뉴(바로가기 메뉴 = 옆메뉴) - 2.구분선 넣는법(NavigationDrawer border line) (0) | 2020.02.04 |
[Android] 뷰페이저(ViewPager) - 2.타이틀스트립 사용법 (0) | 2020.02.02 |
[Android] 뷰페이저(ViewPager) - 1.기본 사용법 (0) | 2020.02.02 |
[Android] 탭(Tab) - 2. (0) | 2020.02.01 |