반응형
메인 액티비티 자바
package grocery.gohool.slidefragmentmenu;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {
//슬라이드메뉴
private DrawerLayout drawerLayout;
private View slide;
private Button openBtn;
private Button closeBtn;
//fragment
private Button btn1,btn2,btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openBtn = (Button) findViewById(R.id.openBtn);
closeBtn = (Button) findViewById(R.id.closeBtn);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
slide = (View)findViewById(R.id.slide);
openBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.openDrawer(slide);
}
});
closeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawers();
}
});
btn1 = (Button) findViewById(R.id.frag1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawers();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.frame, fragment1);
fragmentTransaction.commit();
;
}
});
btn2 = (Button) findViewById(R.id.frag2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawers();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.frame,fragment2);
fragmentTransaction.commit();
drawerLayout.closeDrawers();
}
});
btn3 = (Button) findViewById(R.id.frag3);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawers();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment3 fragment3 = new Fragment3();
fragmentTransaction.replace(R.id.frame,fragment3);
fragmentTransaction.commit();
drawerLayout.closeDrawers();
}
});
}
}
메인액티비티.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:id="@+id/frame"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/openBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="메뉴열기"/>
</LinearLayout>
<include layout="@layout/slidemenu"/>
</androidx.drawerlayout.widget.DrawerLayout>
슬라이드메뉴.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/slide"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:background="@color/teal_700"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="@+id/closeBtn"
android:text="메뉴닫기"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:id="@+id/frag1"
android:text="메뉴1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/frag2"
android:text="메뉴2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/frag3"
android:text="메뉴3"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
fragment.xml 1~3
<?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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="메뉴1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment. 자바 1~3
package grocery.gohool.slidefragmentmenu;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
public Fragment1(){
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1,container,false);
}
}
주의
처음에 그냥 합치려고 했다가 저기 초록색 슬라이드메뉴안에 frgment가 갖히게 되었다.
replace의 첫번째 인자를 대체해주기 위해 MainActivity.java에 frameLayout을 넣어줌으로써,
어떤 fragment에서나 메뉴열기버튼을 가져와서 메뉴를 열 수 있게 해주었다.
반응형
'개발언어 > JAVA' 카테고리의 다른 글
안드로이드 로그인 + 회원가입 만들기 (0) | 2020.12.27 |
---|---|
안드로이드 하단 메뉴만들기 (0) | 2020.12.26 |
안드로이드 Fragment (0) | 2020.12.25 |
안드로이드 생명주기 (0) | 2020.12.25 |
안드로이드 슬라이드 메뉴 만들기 (0) | 2020.12.24 |