이번 시간에는 Navigation Drawer
쉽게 꾸미는 방법에 대하여 알아보겠습니다.
1. 실행 화면
2. 라이브러리 등록
3. 프래그먼트 생성
4. 메인 화면 구성 activity_main.xml
5. 메인 코드 구현 MainActivity.java
build.gradle(Module:프로젝트명:app)
dependencies 괄호 안에 아래 코드를 넣어주시면 됩니다.
implementation 'com.github.shrikanth7698:Custom-Navigation-Drawer:v0.0.1'
setting.gradle
repositories 괄호 안에 아래 코드를 넣어주시면 됩니다.
maven { url 'https://jitpack.io' }
참조 문서
https://github.com/shrikanth7698/Custom-Navigation-Drawer
java: MessageFm, NewsFm
xml: fragment_message_fm, fragment_news_fm
2022.01.08 - [안드로이드] - [안드로이드 스튜디오] 프래그먼트(Fragemnt) 만드는 방법
MessageFragment
public class MessageFm extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_message_fm, container, false);
}
}
NewsFragment
public class NewsFm extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_news_fm, container, false);
}
}
fragment_message.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".MessageFm">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Message"
android:textSize="50sp" />
</FrameLayout>
fragment_news
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".NewsFm">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="News"
android:textSize="50sp" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<com.shrikanthravi.customnavigationdrawer2.widget.SNavigationDrawer 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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout"/>
</com.shrikanthravi.customnavigationdrawer2.widget.SNavigationDrawer>
public class MainActivity extends AppCompatActivity {
SNavigationDrawer sNavigationDrawer;
Class aClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sNavigationDrawer = findViewById(R.id.navigation_drawer);
//리스트 생성
List<MenuItem> itemList = new ArrayList<>();
//리스트에 메뉴 등록
itemList.add(new MenuItem("Message", R.drawable.message_bg));
itemList.add(new MenuItem("News", R.drawable.news_bg));
//네비게이션에 메뉴리스트 추가
sNavigationDrawer.setMenuItemList(itemList);
//기본 화면 설정
aClass = MessageFragment.class;
openFragment(); //프래그먼트 생성
//네비게이션 메뉴 선택 이벤트
sNavigationDrawer.setOnMenuItemClickListener(new SNavigationDrawer.OnMenuItemClickListener() {
@Override
public void onMenuItemClicked(int position) {
switch(position){
case 0:
aClass = MessageFragment.class;
break;
case 1:
aClass = NewsFragment.class;
break;
}
}
});
//네비게이션 이벤트
sNavigationDrawer.setDrawerListener(new SNavigationDrawer.DrawerListener() {
@Override
public void onDrawerOpening() {
}
@Override
public void onDrawerClosing() {
openFragment();
}
@Override
public void onDrawerOpened() {
}
@Override
public void onDrawerClosed() {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
/**
* 프래그먼트 생성
*/
private void openFragment() {
try{
//프래그 먼트 초기화
Fragment fragment = (Fragment) aClass.newInstance();
//프래그먼트 실행
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(android.R.anim.fade_in
,android.R.anim.fade_out)
.replace(R.id.frame_layout, fragment)
.commit();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
댓글 영역