---
# System prepended metadata

title: Android Studio漢堡選單(左側選單)2

---

# Android Studio漢堡選單(左側選單)2
{%hackmd bVWcIzSWRcOp-bzo6RbA9A %}
## 主線任務-點選項換頁
首先我的作法是 一個頁面=Class(Fragment)+xml
以書籤頁面為例
圖
### 在layout新增一個xml檔
![image](https://hackmd.io/_uploads/rJItO4_6p.png)

```
<?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:layout_marginStart="170dp"
        android:layout_marginTop="315dp"
        android:layout_marginEnd="184dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
XML取名為fragment_book
裡面放了text當測試

### 在java新增class
![image](https://hackmd.io/_uploads/H1kUtE_6a.png)

```
public class BookFragment extends Fragment {

    private TextView tx1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book, container, false);//綁定前面建立fragment_book XML檔

        return view;
    }
```
class取名為 BookFragment
記得繼承Fragment
![image](https://hackmd.io/_uploads/Hka65VuT6.png)

```
public void onResume() {
        super.onResume();

        tx1.setText("Book Fragment");
		}
}
```
在XML檔的text改個顯示內容當測試~


### 選單點擊要換頁啦
在MainActivity裡code
![image](https://hackmd.io/_uploads/Bki3oVu6T.png)
![image](https://hackmd.io/_uploads/HysaiV_T6.png)
![image](https://hackmd.io/_uploads/SJZ-xrdT6.png)

```
private BookFragment bookFragment;
private NavigationView navigation_view;
private DrawerLayout drawerLayout;
```
宣告要用的的Fragment和漢堡選單

**接下來都在Oncreate裡**
![image](https://hackmd.io/_uploads/Sy8B6EdaT.png)
navigation_view綁定XML漢堡選單的ID
![image](https://hackmd.io/_uploads/HJZW0EdTT.png)
開啟app的第一個頁面是map(也可換成剛剛宣告的book)
![image](https://hackmd.io/_uploads/HJasyrd6p.png)
![image](https://hackmd.io/_uploads/Sk2wlBdp6.png)

選單點擊的時候
```
drawerLayout.closeDrawer(GravityCompat.START);
```
從左邊滑出選單
```
 int id = item.getItemId();
```
取得選項的ID(在menu.xml檔有取(上一篇))
```
                if (id == R.id.action_home) {

                    mapsFragment = new MapsFragment();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_container,mapsFragment,"map").commit();
                    Toast.makeText(MainActivity.this, "Dora map", Toast.LENGTH_SHORT).show();
                    return true;


                }
                else if (id == R.id.action_book) {

                    bookFragment = new BookFragment();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_container,bookFragment,"book").commit();
                    Toast.makeText(MainActivity.this, "book", Toast.LENGTH_SHORT).show();
                    return true;



                }
                else if (id == R.id.action_history) {

                    historyFragment=new HistoryFragment();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_container,historyFragment,"history").commit();
                    Toast.makeText(MainActivity.this, "歷史紀錄", Toast.LENGTH_SHORT).show();

                    return true;
                }
                else if (id == R.id.action_setting) {
                    settingFragment=new SettingFragment();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_container,settingFragment,"setting").commit();
                    Toast.makeText(MainActivity.this, "設定", Toast.LENGTH_SHORT).show();

                    return true;
                }

                return false;
```
用if判斷ID執行換頁
```
bookFragment = new BookFragment();
 getSupportFragmentManager().beginTransaction().replace(R.id.fl_container,bookFragment,"book").commit();
```
每一次點擊都會建立Fragment(若不建立只能點一次)
getSupportFragmentManager()>管理Fragment
beginTransaction()>執行操作Fragment 操作
replace(R.id.fl_container, bookFragment, "book")fl_container這個容器換成bookFragment

**onCreate全部程式碼**
```
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navigation_view = (NavigationView) findViewById(R.id.navigation_view);

        //初始化地圖(第一個頁面是MAP)
        mapsFragment = new MapsFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fl_container,mapsFragment,"map").commit();
        //選單點擊
        navigation_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                // 點選時收起選單
                drawerLayout.closeDrawer(GravityCompat.START);

                // 取得選項id
                int id = item.getItemId();

                // 依照id判斷點了哪個項目並做相應事件
                if (id == R.id.action_home) {

                    mapsFragment = new MapsFragment();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_container,mapsFragment,"map").commit();
                    Toast.makeText(MainActivity.this, "Dora map", Toast.LENGTH_SHORT).show();
                    return true;


                }
                else if (id == R.id.action_book) {

                    bookFragment = new BookFragment();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_container,bookFragment,"book").commit();
                    Toast.makeText(MainActivity.this, "book", Toast.LENGTH_SHORT).show();
                    return true;



                }
                else if (id == R.id.action_history) {

                    historyFragment=new HistoryFragment();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_container,historyFragment,"history").commit();
                    Toast.makeText(MainActivity.this, "歷史紀錄", Toast.LENGTH_SHORT).show();

                    return true;
                }
                else if (id == R.id.action_setting) {
                    settingFragment=new SettingFragment();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fl_container,settingFragment,"setting").commit();
                    Toast.makeText(MainActivity.this, "設定", Toast.LENGTH_SHORT).show();

                    return true;
                }

                return false;
            }
        });

    }
```
圖



