AndroidのFragmentのダイナミックロード

5718 ワード


本編ではreplaceメソッドによるfragmentの置き換えと表示
——————————————————————————————————
注意:replaceメソッドは現在のfragmentを破棄します.つまり、もう一度表示するとfragmentのライフサイクル全体が経過するので、hideメソッドとshowメソッドを使用してfragmentを操作することをお勧めします.
 
 
MainActivity.class
 
 
package com.example.y.potographu;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import com.example.y.potographu.fragment.FragmentAppointment;
import com.example.y.potographu.fragment.FragmentDiscovery;
import com.example.y.potographu.fragment.FragmentHome;
import com.example.y.potographu.fragment.FragmentMine;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private FragmentTransaction transaction;
    private FragmentManager manager;
    private FragmentHome fragmentHome;
    private FragmentDiscovery fragmentDiscovery;
    private FragmentAppointment fragmentAppointment;
    private FragmentMine fragmentMine;

    @SuppressLint("CommitTransaction")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navigation = findViewById(R.id.navigation);    //bottomNavigation    Fragment
        navigation.setOnNavigationItemSelectedListener(this);

        /*        FragmentManager FragmentTransaction   Fragment   
        *           ,          showFragment()     
        */
        manager = getSupportFragmentManager();      //      
        fragmentHome = new FragmentHome();      //   Fragment
        showFragment(fragmentHome);     //    

    }

    private void showFragment(Fragment fragment) {
        transaction= manager.beginTransaction();        //    
        /*
        *       ,      Fragment    
        *         fragment   
        *          fragment
        * */
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();//    

    }


    private void hideFragment(FragmentTransaction transaction) {
        /*       fragment     
        *             
        *     replace            
        * */
        if (!fragmentHome.isHidden())
            transaction.hide(fragmentHome);
        else if (!fragmentDiscovery.isHidden())
            transaction.hide(fragmentDiscovery);
        else if (!fragmentAppointment.isHidden())
            transaction.hide(fragmentAppointment);
        else if (!fragmentMine.isHidden())
            transaction.hide(fragmentMine);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
       // hideFragment(transaction); 
        /*
        *   showFragment()    
        * */
        switch (item.getItemId()) {
            case R.id.navigation_home:
                showFragment(fragmentHome);
                return true;
            case R.id.navigation_discovery:
                if (fragmentDiscovery == null)
                    fragmentDiscovery = new FragmentDiscovery();
                showFragment(fragmentDiscovery);
                return true;
            case R.id.navigation_appointment:
                if (fragmentAppointment == null)
                    fragmentAppointment = new FragmentAppointment();
                showFragment(fragmentAppointment);
                return true;
            case R.id.navigation_mine:
                if (fragmentMine == null)
                    fragmentMine = new FragmentMine();
                showFragment(fragmentMine);
                return true;
            default:
        }
        return false;
    }
}

 
FragmentHome.class
 
package com.example.y.potographu.fragment;

        import android.os.Bundle;
        import android.support.annotation.NonNull;
        import android.support.annotation.Nullable;
        import android.support.v4.app.Fragment;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;

        import com.example.y.potographu.R;

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

 
他の3つはこのページのコードと似ているので、ここでは貼らない.
 
 
 
activity_main.xml
 
 



    
    
    

    


fragmentのレイアウトには、異なるページを識別するための背景が設定されているだけです.