selfstarter

Android Example with Fragment and ToolBar 본문

App/Android

Android Example with Fragment and ToolBar

selfstarter 2020. 6. 4. 17:41

# layout

- activity_main.xml

```

<?xml version="1.0" encoding="utf-8"?>

 

    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"

    >

    <androidx.appcompat.widget.Toolbar

        android:id="@+id/toolbar"

        android:layout_width="match_parent"

        android:layout_height="?attr/actionBarSize"

        android:background="?attr/colorPrimary"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        >

        <TextView

            android:id="@+id/toolbar_title"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="List"

            android:textSize="30dp"

            android:layout_gravity = "center"

            />

    </androidx.appcompat.widget.Toolbar>

 

    <fragment

        android:id="@+id/fragment"

        android:layout_width="match_parent"

        android:layout_height="0dp"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintTop_toBottomOf="@id/toolbar"

        android:name="com.tmoney.taxi.toolbarexample.ListFragment"

        >

    </fragment>

 

</androidx.constraintlayout.widget.ConstraintLayout>

```

 

- fragment_detail.xml

```

<?xml version="1.0" encoding="utf-8"?>

 

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:background="#ff5500"

    >

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="TEST TEST"

        android:textSize="50dp"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        />

 

</androidx.constraintlayout.widget.ConstraintLayout>

```

 

- fragment_list.xml

```

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout

    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

    android:layout_height="match_parent"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:background="#00aaff"

    >

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="LIST 화면"

        android:textSize="50dp"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        />

</androidx.constraintlayout.widget.ConstraintLayout>

```

# JAVA Code

- DetailFragment.java

```

public class DetailFragment extends Fragment {

 

    public DetailFragment() {

    }

 

    @Nullable

    @Override

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_detail, container, false);

    }

}

```

- ListFragment.java

```

public class ListFragment extends Fragment {

    public ListFragment() {

    }

 

    @Nullable

    @Override

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_list, container, false);

    }

}

```

- MainActivity.java

```

public class MainActivity extends AppCompatActivity {

    private ListFragment listFragment;

    private DetailFragment detailFragment;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

 

        getSupportActionBar().setDisplayShowTitleEnabled(false);

 

        listFragment = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragment);

        detailFragment = new DetailFragment();

    }

 

    // toolbar에 표시하기

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.toolbar_menu, menu);

        return true;

    }

 

    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case android.R.id.home:

                getSupportActionBar().setDisplayHomeAsUpEnabled(false);

                Log.d("TEST", "home icon click");

                // 기존 프래그먼트를 사용하면서 에러남..

                changeFragment(listFragment);

                return true;

            case R.id.option:

                getSupportActionBar().setDisplayHomeAsUpEnabled(true);

                Log.d("TEST", "option icon click");

                changeFragment(detailFragment);

                return true;

            default:

                return super.onOptionsItemSelected(item);

        }

    }

 

    public void changeFragment (Fragment fragment) {

        FragmentManager fm = getSupportFragmentManager();

        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right);

        fragmentTransaction.replace(R.id.fragment, fragment);

        fragmentTransaction.addToBackStack(null);

        fragmentTransaction.commit();

    }

}

```

 

 

'App > Android' 카테고리의 다른 글

constraintlayout weight  (0) 2020.06.20
Android drawable image에서 bitmap으로 변환  (0) 2020.06.15
Android fragment 정리  (0) 2020.06.04
XML에 선언된 Fragment Java객체로 가져오기  (0) 2020.05.31
Android DrawerLayout  (0) 2020.05.25
Comments