我已经成功实现了导航抽屉,但我遇到了问题。
选择片段后,单击后退按钮,弹出当前片段,显示上一个片段。
但显示片段的标题仍然是前一片段的标题。
此外,抽屉中检查的项目仍然是前一个片段的项目。
请您知道如何更新标题和检查项目吗?
我的活动
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private final String TAG = "MainActivity";
NavigationView navigationView;
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
String about_blog = "http://example.com/page/json/urls/p-about";
String contri = "http://example.com/page/json/urls/contribute";
String privy = "http://example.com/page/json/urls/privacy-rules";
String contact = "http://example.com/page/json/urls/contact_us";
String advert = "http://example.com/page/json/urls/advertise-here";
String spons = "http://example.com/page/json/urls/sponsor-us";
Fragment fragment = null;
if (id == R.id.menu_home) {
fragment = new PostFragment();
}
if (id == R.id.about_blog) {
fragment = new PageFragment();
} else if (id == R.id.nav_contri) {
fragment = new PageFragment();
} else if (id == R.id.nav_contact) {
fragment = new PageFragment();
} else if (id == R.id.nav_privy) {
fragment = new PageFragment();
} else if (id == R.id.nav_advert) {
fragment = new PageFragment();
} else if (id == R.id.spons) {
fragment = new PageFragment();
} else if (id == R.id.app_about) {
//fragment = new abtFragment();
} else if (id == R.id.settings) {
//fragment = new SettingFragment;
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Device rotated and onCreate called");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setCheckedItem(R.id.menu_home);
navigationView.getMenu().performIdentifierAction(R.id.menu_home, 0);
}
我已经解决了在每次背压后更新每个片段的标题的问题。我所做的是将标题从 MainActivity 发送到片段,然后在我收到它的片段中调用:
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);
这是我的onNavigationItemSelected Code之后的样子
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
id = item.getItemId();
Bundle bundle = new Bundle();
String about_blog = "http://example.com/page/json/urls/p-about";
String contri = "http://example.com/page/json/urls/contribute";
String privy = "http://example.com/page/json/urls/privacy-rules";
String contact = "http://example.com/page/json/urls/contact_us";
String advert = "http://example.com/page/json/urls/advertise-here";
String spons = "http://example.com/page/json/urls/sponsor-us";
Fragment fragment = null;
if (id == R.id.menu_home) {
fragment = new PostFragment();
bundle.putString("title", getString(R.string.home));
fragment.setArguments(bundle);
}
if (id == R.id.about_blog) {
bundle.putString("title", getString(R.string.about));
fragment = new PageFragment();
fragment.setArguments(bundle);
} else if (id == R.id.nav_contri) {
bundle.putString("title", getString(R.string.contri));
fragment = new PageFragment();
fragment.setArguments(bundle);
} else if (id == R.id.nav_contact) {
bundle.putString("title", getString(R.string.contac));
fragment = new PageFragment();
fragment.setArguments(bundle);
} else if (id == R.id.nav_privy) {
bundle.putString("title", getString(R.string.privy_p));
fragment = new PageFragment();
fragment.setArguments(bundle);
} else if (id == R.id.nav_advert) {
bundle.putString("title", getString(R.string.advert));
fragment = new PageFragment();
fragment.setArguments(bundle);
} else if (id == R.id.spons) {
bundle.putString("title", getString(R.string.spons));
fragment = new PageFragment();
fragment.setArguments(bundle);
} else if (id == R.id.app_about) {
//fragment = new abtFragment();
} else if (id == R.id.settings) {
//fragment = new SettingFragment;
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment, "visible_fragment");
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
//setTitle(item.getTitle()); // No longer needed.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
在片段的onCreateView
中,我收到了String:
String title = getArguments().getString("title");
然后仍然在onCreateView
我做了:
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);
标题现在正在更新。
现在唯一的问题是导航抽屉项目拒绝更新。
Android Studio也在抱怨方法调用((AppCompatActivity)getActivity())。getSupportActionBar()。setTitle(title); mayproduce java.lang.NullPointerException 。
答案 0 :(得分:0)
您也可以这样做。
@Override
public void onBackPressed() {
super.onBackPressed();
getSupportActionBar().setTitle(getResources().getString(R.string.titlefragment));
}
答案 1 :(得分:0)
您需要为每个Title
设置 Fragment Transaction
。
请尝试更改您的onNavigationItemSelected
。
Fragment fragment = null;
String title = "Test";
if (id == R.id.menu_home) {
fragment = new PostFragment();
title = "Post";
}
if (id == R.id.about_blog) {
fragment = new PageFragment();
title = "Page";
} else if (id == R.id.nav_contri) {
fragment = new PageFragment();
title = "Page";
} else if (id == R.id.nav_contact) {
fragment = new PageFragment();
title = "Page";
} else if (id == R.id.nav_privy) {
fragment = new PageFragment();
title = "Page";
} else if (id == R.id.nav_advert) {
fragment = new PageFragment();
title = "Page";
} else if (id == R.id.spons) {
fragment = new PageFragment();
title = "Page";
} else if (id == R.id.app_about) {
//fragment = new abtFragment();
} else if (id == R.id.settings) {
//fragment = new SettingFragment;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
**// set the toolbar title
getSupportActionBar().setTitle(title);**
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
编辑1:
OverRide onResume of your MainActivity
。
使用此代码。
@Override
public void onResume() {
super.onResume();
// Set title
getActivity().getActionBar()
.setTitle("Your Title Goes Here");
}