Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

android - Moving from fragment to fragment managed by Navigation Drawer

I took Navigation Drawer from Android Studio template. Everything works fine, but there are several problems associated with not understanding this component. I'm almost new, came from Delphi.

I need to come up with a transition between fragments from the Navigation Drawer, in the fragment itself, by pressing a button. That is, fragment1: buttonclick> fragment2.The usual Intent does not work, and the FragmentManager, as I understand it, only changes the xml files, and I need to initialize the class. All rummaged, found nothing like that.

MainActivity:

package esport.inyourlife.insport;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import esport.inyourlife.insport.ui.slideshow.ProfileFragment;

public class HomeNav extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private TextView uMail, uName;
    private FirebaseAuth mAuth;
    private final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_nav);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> Snackbar.make(view, "Позже уберу эту штуку, при оптимизаций приложений", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show());
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_tournament, R.id.nav_profile)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        uMail = findViewById(R.id.uMail);
        uName = findViewById(R.id.uName);
        /*Intent i = getIntent();
        if (i != null)
        {
            uMail.setText(i.getStringExtra("email"));
        }
        else
        {
            Toast.makeText(this, "Ошибка получения емайл", Toast.LENGTH_SHORT).show();
        }*/
        /*
        FirebaseAuth.AuthStateListener stateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (user != null)
                {
                    mAuth.removeAuthStateListener(this);
                    uMail.setText(user.getEmail());
                    if (!user.getDisplayName().equals(null))
                    {
                        uName.setText(user.getDisplayName());
                    }
                }
            }
        };
        mAuth.addAuthStateListener(stateListener);*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_nav, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.signout :
            mAuth.signOut();
            startActivity(new Intent(this, LoadFrame.class));
            finish();
            return true;
            case R.id.test :
                Toast.makeText(this, "Отсутсвует метод", Toast.LENGTH_SHORT).show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }



    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
    public void Profile(View v)
    {
        Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getApplicationContext(), ProfileFragment.class));
    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    // public void SignOut(View view) { mAuth.signOut(); }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To navigate from current fragment to next fragment, use this code in your current fragment:

Button button = view.findViewById(R.id.xml_button);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.nextFragment, null));

To navigate with a transition animation, set the id of the fragment you want to navigate to (nextFragment), to the id of the action containing the fragment transition.

Button button = view.findViewById(R.id.xml_button);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_currentFragment_to_nextFragment, null));

If you already have an onClickListener set for the button, place this inside it instead:

Navigation.findNavController(view).navigate(R.id.nextFragment);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...