在导航抽屉管理的片段之间移动
Posted
技术标签:
【中文标题】在导航抽屉管理的片段之间移动【英文标题】:Moving from fragment to fragment managed by Navigation Drawer 【发布时间】:2021-04-24 06:13:26 【问题描述】:我从 android Studio 模板中获取了 Navigation Drawer。一切正常,但有几个与不理解此组件相关的问题。我几乎是新人,来自德尔福。
我需要通过按下按钮在片段本身中从导航抽屉中的片段之间进行过渡。即fragment1:buttonclick>fragment2。通常的Intent不起作用,而FragmentManager,据我了解,只是更改xml文件,我需要初始化类。翻遍了,没有找到类似的东西。
主活动:
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();
【问题讨论】:
【参考方案1】:要从当前片段导航到下一个片段,请在当前片段中使用以下代码:
Button button = view.findViewById(R.id.xml_button);
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.nextFragment, null));
要使用过渡动画进行导航,请将要导航到的片段的 ID (nextFragment) 设置为包含片段过渡的操作的 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);
【讨论】:
哦,原来一切都变得容易多了?!谢谢安德鲁,你是我的英雄救星。顺便问一下,你是俄罗斯人吗?所以顺便说一句,既然你是安德烈..当然,我开始为自己复杂化任务,并试图通过 MainActivity 获得 navhost 甚至没有阅读消息,并且有几天我向模板的作者发誓))该死的,非常感谢安德鲁 不幸的是,没有任何声誉可以放一个加号或以某种方式感谢你,你不知道我是如何被这个导航折磨的以上是关于在导航抽屉管理的片段之间移动的主要内容,如果未能解决你的问题,请参考以下文章