gpt4 book ai didi

android - (导航组件)返回首页fragment时如何在activity上显示返回箭头?

转载 作者:行者123 更新时间:2023-12-03 22:00:11 32 4
gpt4 key购买 nike

我必须对“编辑服务” Activity 中显示的折扣 fragment 和编辑服务 fragment 进行分段。
当我返回编辑服务 fragment 时,折扣 fragment 上显示的后退箭头箭头消失我想显示它以从编辑服务 Activity 导航上一个 Activity 。

Activity 布局
..................................................... …………

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar

android:id="@+id/toolbar_hesham"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="@string/edit_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent" />

<fragment
android:id="@+id/nav_host_edit_service"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar_hesham"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/edit_service_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>


<include layout="@layout/confirm_request_bottom_sheet"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Activity 代码
..................................................... ............
public class EditServicesActivity extends BaseActivity<MainViewModel> {


public Toolbar toolbar;
public NavController navController;

@Override
protected void initActivityComponent() {
component = ProServeTechApp.getComponent(this)
.plus(new ActivityModule(this));
component.inject(this);
}

@Override
protected int getLayout() {
return R.layout.activity_edit_services;
}

@Override
protected Class<MainViewModel> getViewModelClass() {
return MainViewModel.class;
}

@Override
protected void initActivity() {
viewModel.getRequestDetails(getIntent().getExtras().getString("requestId"), String.valueOf(0));
viewModel.getIssues(getIntent().getStringExtra("requestId"));

toolbar = findViewById(R.id.toolbar_hesham);
setSupportActionBar(toolbar);

navController = Navigation.findNavController(this, R.id.nav_host_edit_service);
NavigationUI.setupWithNavController(toolbar, navController );

}



}

导航
…………………………………………………………………………………………………………………………………………
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/edit_service_nav"
app:startDestination="@id/edi_service_fragment">

<fragment
android:id="@+id/edi_service_fragment"
android:name="com.unicomg.proservetech.ui.requestdetails.edit.service.fragments.EditServiceFragment"
android:label="@string/edit_service"
tools:layout="@layout/edit_service_fragment">

<action
android:id="@+id/action_edi_service_fragment_to_discount_fragment"
app:destination="@id/discount_fragment"
app:enterAnim="@anim/slide_up"
app:exitAnim="@anim/slide_bottom"
app:popEnterAnim="@anim/slide_up"
app:popExitAnim="@anim/slide_bottom" />
</fragment>

<fragment
android:id="@+id/discount_fragment"
android:name="com.unicomg.proservetech.ui.requestdetails.edit.service.fragments.DiscountFragment"
android:label="@string/add_discount"
tools:layout="@layout/discount_fragment">


</fragment>

</navigation>

最佳答案

根据 setupWithNavController(Toolbar, NavController) documentation :

The start destination of your navigation graph is considered the only top level destination. On all other destinations, the Toolbar will show the Up button.



如果您还想在开始目的地上显示向上按钮(即转到上一个 Activity ),您需要使用带有 AppBarConfiguration 的版本。 .

根据 Update UI components documentation on AppBarConfiguration , AppBarConfiguration允许您准确设置您想要作为顶级目的地的目的地。要在每个目的地上显示向上按钮,您将使用一组空的顶级目的地:
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder().build();

请注意,由于您使用的是 setSUpportActionBar() ,您应该关注 Action Bar documentation并使用 setupActionBarWithNavController()方法而不是 Toolbar版本。您还必须覆盖 onSupportNavigateUp()处理向上按钮。

因此,您的完整代码如下所示:
public class EditServicesActivity extends BaseActivity<MainViewModel> {


public Toolbar toolbar;
public AppBarConfiguation appBarConfiguation;
public NavController navController;

@Override
protected void initActivityComponent() {
component = ProServeTechApp.getComponent(this)
.plus(new ActivityModule(this));
component.inject(this);
}

@Override
protected int getLayout() {
return R.layout.activity_edit_services;
}

@Override
protected Class<MainViewModel> getViewModelClass() {
return MainViewModel.class;
}

@Override
protected void initActivity() {
viewModel.getRequestDetails(getIntent().getExtras().getString("requestId"), String.valueOf(0));
viewModel.getIssues(getIntent().getStringExtra("requestId"));

toolbar = findViewById(R.id.toolbar_hesham);
setSupportActionBar(toolbar);

navController = Navigation.findNavController(this, R.id.nav_host_edit_service);
appBarConfiguration = new AppBarConfiguration.Builder().build();
NavigationUI.setupActionBarWithNavController(this, navController,
appBarConfiguration);
}

@Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}

关于android - (导航组件)返回首页fragment时如何在activity上显示返回箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60173227/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com