Programming/Android

안드로이드 FrameLayout 트랜잭션

JinWooHong Dev 2020. 1. 2. 18:07

BottomNavigationView 아이콘 설정 이후의 작업

https://jalbin.tistory.com/13

 

1. activity_main.xml 에 FrameLayout 설정

 <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/navigationView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"></FrameLayout>

 

2. FrameLayout에 들어갈 Layout  및 Java Class 생성

 

3. JavaClass 파일 설정

<FragmentHistory 설정>

public class FragmentHistory extends Fragment {
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_history,container,false);
    }

<FragmentUser 설정>

public class FragmentUser extends Fragment {
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                return inflater.inflate(R.layout.fragment_user,container,false);
    }
}

<FragmentHome 설정>

public class FragmentHome extends Fragment {
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

<MainActivity 설정>

public class MainActivity extends AppCompatActivity {
    private FragmentManager fragmentManager =getSupportFragmentManager();
    private FragmentHome fragmentHome=new FragmentHome();
    private FragmentHistory fragmentHistory = new FragmentHistory();
    private FragmentUser fragmentUser=new FragmentUser();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frameLayout,fragmentHome).commitAllowingStateLoss();

        BottomNavigationView bottomNavigationView = findViewById(R.id.navigationView);
       bottomNavigationView.setOnNavigationItemSelectedListener(new ItemSelectedListener());

    }


    class ItemSelectedListener implements BottomNavigationView.OnNavigationItemSelectedListener{
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem){
            FragmentTransaction transaction = fragmentManager.beginTransaction();

            switch (menuItem.getItemId())
            {
                case R.id.homeItem:
                    transaction.replace(R.id.frameLayout, fragmentHome).commitAllowingStateLoss();
                    break;
                case R.id.historyItem:
                    transaction.replace(R.id.frameLayout,fragmentHistory).commitAllowingStateLoss();
                    break;
                case R.id.userItem:
                    transaction.replace(R.id.frameLayout,fragmentUser).commitAllowingStateLoss();
                    break;
            }
            return true;
        }
    }
}