在 Navigation Drawer 管理的片段之间移动
Moving from fragment to fragment managed by Navigation Drawer
我从 Android Studio 模板中获取了 Navigation Drawer。一切正常,但有几个问题与不理解这个组件有关。我几乎是新手,来自 Delphi.
我需要通过按下按钮在片段本身的导航抽屉中的片段之间进行转换。也就是说,fragment1: buttonclick> fragment2.The usual Intent 不起作用,据我了解,FragmentManager 仅更改 xml 个文件,我需要初始化 class。翻遍了,没找到。
主要活动:
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(); }
}
要从当前片段导航到下一个片段,请在当前片段中使用此代码:
Button button = view.findViewById(R.id.xml_button);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.nextFragment, null));
要使用过渡动画进行导航,请将要导航到的片段 (nextFragment) 的 ID 设置为包含片段过渡的动作的 ID。
Button button = view.findViewById(R.id.xml_button);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_currentFragment_to_nextFragment, null));
如果您已经为按钮设置了 onClickListener,请将其放在其中:
Navigation.findNavController(view).navigate(R.id.nextFragment);
我从 Android Studio 模板中获取了 Navigation Drawer。一切正常,但有几个问题与不理解这个组件有关。我几乎是新手,来自 Delphi.
我需要通过按下按钮在片段本身的导航抽屉中的片段之间进行转换。也就是说,fragment1: buttonclick> fragment2.The usual Intent 不起作用,据我了解,FragmentManager 仅更改 xml 个文件,我需要初始化 class。翻遍了,没找到。
主要活动:
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(); }
}
要从当前片段导航到下一个片段,请在当前片段中使用此代码:
Button button = view.findViewById(R.id.xml_button);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.nextFragment, null));
要使用过渡动画进行导航,请将要导航到的片段 (nextFragment) 的 ID 设置为包含片段过渡的动作的 ID。
Button button = view.findViewById(R.id.xml_button);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_currentFragment_to_nextFragment, null));
如果您已经为按钮设置了 onClickListener,请将其放在其中:
Navigation.findNavController(view).navigate(R.id.nextFragment);