2swan
Fragment 예제 본문
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"
android:orientation="vertical"
tools:context=".MainActivity3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="음악별"/>
<Button
android:id="@+id/btnArtist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="가수별"/>
<Button
android:id="@+id/btnAlbum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="앨범별"/>
</LinearLayout>
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
</LinearLayout>
song.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"
android:background="#00ff00"
tools:context=".SongFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Song Fragment"
android:layout_gravity="center"
android:textColor="#ffffff"/>
</FrameLayout>
artist.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"
android:background="#ff0000"
tools:context=".ArtistFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Artist Fragment"
android:layout_gravity="center"
android:textColor="#ffffff"/>
</FrameLayout>
album.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"
android:background="#334455"
tools:context=".AlbumFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Album Fragment"
android:layout_gravity="center"
android:textColor="#ffffff"/>
</FrameLayout>
main.activity
public class MainActivity3 extends AppCompatActivity implements View.OnClickListener{
Button btnSong, btnArtist, btnAlbum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
btnSong = findViewById(R.id.btnSong);
btnArtist = findViewById(R.id.btnArtist);
btnAlbum = findViewById(R.id.btnAlbum);
btnSong.setOnClickListener(this::onClick);
btnArtist.setOnClickListener(this::onClick);
btnAlbum.setOnClickListener(this::onClick);
}
@Override
public void onClick(View view) {
Fragment fr = null;
if(view.getId() == R.id.btnSong){
fr = new SongFragment();
} else if (view.getId() == R.id.btnAlbum) {
fr = new AlbumFragment();
} else if (view.getId() == R.id.btnArtist) {
fr = new ArtistFragment();
}
//첫번째 방법
// FragmentManager fragmentManager = getSupportFragmentManager();
// FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.replace(R.id.fragment_container, fr);
// fragmentTransaction.addToBackStack(null);
// fragmentTransaction.commit();
//두번째 방법
getSupportFragmentManager().
beginTransaction().
replace(R.id.fragment_container, fr).
addToBackStack(null).
commit();
}
Fragment 파일을 생성한 후 작업 해준다.
결과 값
'Programming > Android Example' 카테고리의 다른 글
SQLite 일기장 앱 (0) | 2023.08.09 |
---|---|
RecyclerView 목록 만들기 (0) | 2023.08.09 |
SQLite 예제2 (0) | 2023.08.08 |
그리드 뷰 (0) | 2023.08.04 |
리스트뷰 동적 추가 (0) | 2023.08.04 |