我有Activity
使用NavigationView
来显示我的应用菜单。
当菜单打开并关闭时,我的应用栏会根据预期从抽屉图标更改为后退箭头。
我有一个场景,我从我的应用程序菜单中单击第1项,然后在我的FrameLayout
中打开一个片段,然后我需要单击一个按钮打开另一个片段,替换前一个片段。
应用栏中有2个片段中的抽屉图标,我想在第二个片段上有一个后退箭头,所以我可以返回第一个片段
这是我的MainActivity
public class MainActivity extends SuperActivity {
@BindView(R.id.nav_view) NavigationView navigationView;
@BindView(R.id.drawer_layout) DrawerLayout drawerLayout;
@BindView(R.id.container) FrameLayout frameLayout;
@BindView(R.id.toolbar) Toolbar toolbar;
ActionBarDrawerToggle drawerToggle;
SuperFragment nextFragment;
FragmentManager fragmentManager;
String activeName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
fragmentManager = getSupportFragmentManager();
setupView();
if (savedInstanceState == null) showHome();
}
private void setupView() {
setSupportActionBar(toolbar);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(drawerToggle);
drawerLayout.setScrimColor(Color.TRANSPARENT);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
setupDrawerContent(navigationView);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
drawerLayout.closeDrawer(Gravity.LEFT);
activeName = item.getTitle().toString();
toolbar.setTitle(activeName);
selectDrawerItem(item);
return false;
}
});
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
}
private void showHome() {
selectDrawerItem(navigationView.getMenu().getItem(0));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
private void selectDrawerItem(MenuItem menuItem) {
drawerLayout.closeDrawer(Gravity.LEFT);
switch (menuItem.getItemId()) {...}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
drawerToggle.syncState();
super.onPostCreate(savedInstanceState);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
private Runnable Logout_Callback = new Runnable() {
public void run() {
Toast.makeText(getApplication(),"Log out",Toast.LENGTH_SHORT).show();
}
};
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}
从此
延伸 public class SuperActivity extends AppCompatActivity implements InterfaceHostActivity{
private SuperFragment selectedFragment;
// Navigation adapter
public NavigationListAdapter mNavigationAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void replaceFragment(SuperFragment fragment, boolean addToBackStack) {
replaceFragment(fragment, addToBackStack, R.id.container);
}
public void replaceFragment(SuperFragment fragment, boolean addToBackStack, int layout) {
try {
FragmentManager manager = getSupportFragmentManager();
if (fragment.isMainFragment) {
manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
String backStateName = ((Object) fragment).getClass().getName();
boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped) {
FragmentTransaction ft = manager.beginTransaction();
ft.replace(layout, fragment, backStateName);
ft.setTransition(FragmentTransaction.TRANSIT_NONE);
if (addToBackStack)
ft.addToBackStack(backStateName);
ft.commit();
}
getSupportFragmentManager().executePendingTransactions();
} catch (Exception e) {
LogManager.logException(e);
}
}
@Override
public void setSelectedFragment(SuperFragment fragment) {
this.selectedFragment = fragment;
}
@Override
public void popBackStack() {
getSupportFragmentManager().popBackStackImmediate();
}
@Override
public void popBackStackUntilTag(String tag) {
getSupportFragmentManager().popBackStackImmediate(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
@Override
public void addFragment(SuperFragment fragment, boolean addToBackStack) {
try {
String backStateName = ((Object) fragment).getClass().getName();
String fragmentTag = backStateName;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, fragment, fragmentTag);
ft.setTransition(FragmentTransaction.TRANSIT_NONE);
if (addToBackStack)
ft.addToBackStack(backStateName);
ft.commit();
} catch (Exception e) {
LogManager.logException(e);
}
}
@Override
public void addMultipleFragments(SuperFragment[] fragments) {
}
@Override
public void onBackPressed() {
boolean eventConsumed = false;
if (selectedFragment != null) {
eventConsumed = eventConsumed || selectedFragment.onBackPressed();
}
if (!eventConsumed) {
super.onBackPressed();
}
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
}
和我的XML
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="pt.sibs.android.mpos.activities.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize" />
<include layout="@layout/toolbar" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:customTypeface="@string/font_lato_light"
android:background="@color/menuBG"
android:layout_marginTop="?attr/actionBarSize"
app:itemTextColor="@color/drawer_text_color_selector"
app:itemTextAppearance="@style/TextViewBody"
app:itemIconTint="@color/menu_icon_selector"
app:itemBackground="@drawable/menu_item_bg"
app:menu="@menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
您可以使用界面
科特林
Interface
:
interface IChangeFragmentListener {
fun updateToolbar(showBackButton: Boolean = false)
}
在Interface
中实施MainActivity
:
class MainActivity : IChangeFragmentListener {
private lateinit var toggle: ActionBarDrawerToggle
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
decorateApplication()
setSupportActionBar(toolbar)
if (supportActionBar != null) {
supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?
.setDisplayHomeAsUpEnabled(true)
}
toggle = ActionBarDrawerToggle(
this,
drawer_layout,
toolbar as Toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
...
}
override fun updateToolbar(showBackButton: Boolean) {
//remove hamburger icon and show arrow icon because
//setDisplayHomeAsUpEnabled in actionbar o toolbar is true
toggle.isDrawerIndicatorEnabled = !showBackButton
toggle.setToolbarNavigationClickListener {
if (showBackButton) onBackPressed()
}
}
...
}
在Fragment
中附加接口,然后在OnResume
上调用它:
open class SomeFragment : Fragment() {
var iChangeFragmentListener: IChangeFragmentListener? = null
override fun onAttach(context: Context) {
super.onAttach(context)
try {
if (context is IChangeFragmentListener)
iChangeFragmentListener = context
} catch (ex: Exception) {
Timber.e(ex)
}
}
override fun onResume() {
super.onResume()
iChangeFragmentListener?.updateToolbar(true)
}
...
}