问题描述
我有一个单一的活动(MainActivity)延伸actionbaractivity并实现NavigationDrawer。我设置了活动本身。在drawertoggle
我创建从由另一个片段后者又由MainActivity创建创建的新片段的片段。 (MainActivity - > HomeFragment - > AnotherFragment)。
MainActivity.java
公共类MainActivity扩展ActionBarActivity实现FragmentDrawer.FragmentDrawerListener,TitleSetter,NavigationDrawerEnabler {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
myToolbar =(工具栏)findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar()setHomeButtonEnabled(真)。
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerFragment =(FragmentDrawer)
。getSupportFragmentManager()findFragmentById(R.id.fragment_navigation_drawer);
设置(R.id.fragment_navigation_drawer,(DrawerLayout)findViewById(R.id.drawer_layout),myToolbar);
mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
drawerFragment.setDrawerListener(本);
//显示在应用程序启动的第一个抽屉式导航视图
displayView(0);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.menu_main,菜单);
返回super.OncreateOptionsMenu(菜单);
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。将操作栏
//自动在主/向上按钮操作的点击,只要
//你在AndroidManifest.xml中指定一个父活动。
/ *此功能不会被调用,即使向上插入符按钮是pressed! * /
如果(mDrawerToggle.isDrawerIndicatorEnabled()&安培;&安培;
mDrawerToggle.onOptionsItemSelected(项目))
返回true;
开关(item.getItemId()){
案例android.R.id.home:
/ *这种情况不会发生* /
返回true;
案例R.id.action_settings:
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}
@覆盖
公共无效onDrawerItemSelected(查看视图,INT位置){
mDrawerLayout.closeDrawer(containerView);
displayView(位置);
}
私人无效displayView(INT位置){
片段片段= NULL;
字符串标题=的getString(R.string.app_name);
开关(位置){
情况下0:
片段=新HomeFragment();
标题=的getString(R.string.title_home);
打破;
情况1:
片段=新SomeFragment();
标题=的getString(R.string.title_something);
打破;
案例2:
//片段=新SomeotherFragment();
//标题=的getString(R.string.title_somethings);
打破;
默认:
打破;
}
如果(片段!= NULL){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body,片段);
fragmentTransaction.commit();
getSupportActionBar()的setTitle(职称)。
}
}
@覆盖
公共无效的setTitle(字符串名称){
getSupportActionBar()的setTitle(职称)。
}
公共无效设置(INT fragmentId,DrawerLayout drawerLayout,最终的工具栏工具栏){
containerView = findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle =新ActionBarDrawerToggle(这一点,drawerLayout,工具栏,R.string.drawer_open,R.string.drawer_close){
@覆盖
公共无效onDrawerOpened(查看drawerView){
super.onDrawerOpened(drawerView);
。getSupportActionBar()invalidateOptionsMenu();
}
@覆盖
公共无效onDrawerClosed(查看drawerView){
super.onDrawerClosed(drawerView);
。getSupportActionBar()invalidateOptionsMenu();
}
@覆盖
公共无效onDrawerSlide(查看drawerView,浮slideOffset){
super.onDrawerSlide(drawerView,slideOffset);
toolbar.setAlpha(1 - slideOffset / 2);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(新的Runnable(){
@覆盖
公共无效的run(){
mDrawerToggle.syncState();
}
});
}
@覆盖
公共无效onBack pressed(){
super.onBack pressed();
enableNavigationDrawer(真正的);
}
@覆盖
公共无效enableNavigationDrawer(布尔IsEnabled的){
如果(IsEnabled的){
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
getSupportActionBar()setDisplayHomeAsUpEnabled(假)。
mDrawerToggle.setDrawerIndicatorEnabled(真正的);
mDrawerToggle.syncState();
}
其他 {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerToggle.setDrawerIndicatorEnabled(假);
getSupportActionBar()setDisplayShowHomeEnabled(真)。
getSupportActionBar()setHomeButtonEnabled(真)。
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
mDrawerToggle.syncState();
}
}
}
HomeFragment.java
公共类HomeFragment扩展片段实现View.OnClickListener {
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.home,集装箱,假);
//充气的布局该片段
FAB =(FloatingActionButton)rootView.findViewById(R.id.fab);
fab.setOnClickListener(本);
((TitleSetter)getActivity())的setTitle(家)。
返回rootView;
}
@覆盖
公共无效onAttach(活动活动){
super.onAttach(活动);
}
@覆盖
公共无效onDetach(){
super.onDetach();
}
@覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.fab:
((NavigationDrawerEnabler)getActivity())enableNavigationDrawer(假)。
片段newFragment =新NewWord();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body,newFragment);
fragmentTransaction.addToBackStack(空);
//提交事务
fragmentTransaction.commit();
打破;
}
}
}
AnotherFragment.java
公共类NewWord扩展片段{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(真正的);
}
@覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
super.onCreateOptionsMenu(菜单,充气);
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//获取项目选择和处理它
/ *不会连这个* /
Log.d(KEY:,将String.valueOf(item.getItemId()));
开关(item.getItemId()){
案例android.R.id.home:
/ *不执行* /
Log.d(片段,我在这里);
getActivity()onBack pressed()。
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.new_word,集装箱,假);
返回rootView;
}
@覆盖
公共无效onAttach(活动活动){
super.onAttach(活动);
((TitleSetter)活动).setTitle(文字);
}
另外,抽屉布局
< android.support.v4.widget.DrawerLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的LinearLayout
机器人:ID =@ + ID / container_toolbar
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
<包括
机器人:ID =@ + ID /工具栏
布局=@布局/工具栏/>
< / LinearLayout中>
<的FrameLayout
机器人:ID =@ + ID / container_body
机器人:layout_width =FILL_PARENT
机器人:layout_height =0dp
机器人:layout_weight =1>
< /的FrameLayout>
< / LinearLayout中>
<片段
机器人:ID =@ + ID / fragment_navigation_drawer
机器人:名称=com.calgen.wordbook.activity.FragmentDrawer
机器人:layout_width =@扪/ nav_drawer_width
机器人:layout_height =match_parent
机器人:layout_gravity =开始
应用程序:布局=@布局/ fragment_nav_drawer
工具:布局=@布局/ fragment_nav_drawer/>
和menu_main.xml
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
工具:上下文=MainActivity。>
<项目
机器人:ID =@ + ID / ACTION_SEARCH
机器人:标题=@字符串/ ACTION_SEARCH
机器人:orderInCategory =100
机器人:图标=@可绘制/ ic_action_search
应用程序:showAsAction =ifRoom/>
<项目
机器人:ID =@ + ID / action_settings
机器人:标题=@字符串/ action_settings
机器人:orderInCategory =100
应用程序:showAsAction =从不/>
我实际上是指这个教程设置导航抽屉片段:的。
我也提到在这里找到答案计算器上类似的问题:
1 <一个href="http://stackoverflow.com/questions/17258020/switching-between-android-navigation-drawer-image-and-up-caret-when-using-fragme?lq=1">Switching Android的导航抽屉的图像和向上插入符号之间在使用碎片
2.My问题是类同的: Android的抽屉式导航栏显示指标的下级片段。 - 我不能在这里评论,因为我没有50次。我实现什么确切地告诉记者,在意见。
但还是点击了插入符不运行onOptionItemSelected功能,即使我已经为homeButton启用!
我现在将我的自定义styles.xml,可能会在这方面的一些错误?
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
&LT;样式名称=MyMaterialTheme父=MyMaterialTheme.Base&GT;&LT; /风格&GT;
&LT;样式名称=MyMaterialTheme.Base父=Theme.AppCompat&GT;
&LT;项目名称=windowNoTitle&GT;真&LT; /项目&GT;
&LT;项目名称=windowActionBar&GT;假&LT; /项目&GT;
&LT;项目名称=机器人:activatedBackgroundIndicator&GT; @可绘制/ nav_background&LT; /项目&GT;
&LT;项目名称=colorPrimary&GT; @色/ colorPrimary&LT; /项目&GT;
&LT;项目名称=colorPrimaryDark&GT; @色/ colorPrimaryDark&LT; /项目&GT;
&LT;项目名称=机器人:textColorPrimary&GT; @色/ textColorPrimary&LT; /项目&GT;
&LT;项目名称=colorAccent&GT; @色/ colorAccent&LT; /项目&GT;
&LT; /风格&GT;
&LT; /资源&GT;
另外,自定义toolbar.xml
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT; android.support.v7.widget.Toolbar
的xmlns:机器人=http://schemas.android.com/apk/res/android
XMLNS:地方=http://schemas.android.com/apk/res-auto
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:=了minHeight?ATTR / actionBarSize
机器人:ATTR / colorPrimary后台=
本地:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
本地:popupTheme =@风格/ ThemeOverlay.AppCompat.Light/&GT;
解决FINALLY
我想通了这个问题,这是由于我使用的自定义工具栏。当自定义工具栏设置,我们还必须设置在向上插入符号的监听器,并猜测什么方法称为drawertoggler.setNaviagtionOnClickListener! [FACEPALM]。
在我们需要添加下面的code中的活动的onCreate()方法:
mtoolbar.setToolbarNavigationClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
。getSupportFragmentManager()popBackStackImmediate();
}
});
请注意:如果您已经定义了ActionBarDrawerToggler在不同的fragment.Then使其静态,并添加一个getter
。其他的情况下,像OnBack pressed可以通过在活动设置OnStackChangeListener进行处理,并进行必要的修改。这显然是在这里解释: @riwnodennyk
希望我会尽力做一个标准的模板,当加入GitHub的汇报。
I've a single activity(MainActivity) which extends actionbaractivity and implements NavigationDrawer. I've setup the drawertoggle in the activity itself.
I'm creating new fragments from a fragment that was created by another fragment which is in turn created by the MainActivity. ( MainActivity -> HomeFragment ->AnotherFragment).
MainActivity.java
public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener, TitleSetter, NavigationDrawerEnabler{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), myToolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
displayView(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.OncreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
/* This function is never called even if the Up caret button is pressed!! */
if (mDrawerToggle.isDrawerIndicatorEnabled() &&
mDrawerToggle.onOptionsItemSelected(item))
return true;
switch (item.getItemId()) {
case android.R.id.home:
/*This case doesn't occur*/
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onDrawerItemSelected(View view, int position) {
mDrawerLayout.closeDrawer(containerView);
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new SomeFragment();
title = getString(R.string.title_something);
break;
case 2:
//fragment = new SomeotherFragment();
//title = getString(R.string.title_somethings);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
getSupportActionBar().setTitle(title);
}
}
@Override
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
containerView = findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().invalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
toolbar.setAlpha(1 - slideOffset / 2);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
enableNavigationDrawer(true);
}
@Override
public void enableNavigationDrawer(boolean isEnabled) {
if ( isEnabled ) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.syncState();
}
else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle.syncState();
}
}
}
HomeFragment.java
public class HomeFragment extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
// Inflate the layout for this fragment
fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
fab.setOnClickListener(this);
((TitleSetter) getActivity()).setTitle("Home");
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fab:
((NavigationDrawerEnabler) getActivity()).enableNavigationDrawer(false);
Fragment newFragment = new NewWord();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, newFragment);
fragmentTransaction.addToBackStack(null);
// Commit the transaction
fragmentTransaction.commit();
break;
}
}
}
AnotherFragment.java
public class NewWord extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Get item selected and deal with it
/*Not even this*/
Log.d("KEY: ", String.valueOf(item.getItemId()));
switch (item.getItemId()) {
case android.R.id.home:
/* Doesn't execute*/
Log.d("Fragment", "I'm here");
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_word, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((TitleSetter) activity).setTitle("Text");
}
Also the drawer layout
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="@+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.calgen.wordbook.activity.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_nav_drawer"
tools:layout="@layout/fragment_nav_drawer" />
And menu_main.xml
<menu 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"
tools:context=".MainActivity">
<item
android:id="@+id/action_search"
android:title="@string/action_search"
android:orderInCategory="100"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
I actually referred this tutorial to setup navigation drawer fragment : Android Hive.
I also referred similar questions answered here on stackoverflow :
1.Switching between Android Navigation Drawer image and Up caret when using fragments
2.My problem is similiar to this : Android Navigation Drawer Show Up Indicator for Lower Level Fragments.- I couldn't comment here as i do not have 50 reps. I implemented what was exactly told in the comments.
But still clicking on the up caret doesn't run the function onOptionItemSelected, even though i've homebutton enabled.!
I'm now adding my custom styles.xml, may be some error in this?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base"></style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:activatedBackgroundIndicator">@drawable/nav_background</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
Also the custom toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
SOLVED FINALLY
I figured out the problem,it is due to the custom toolbar that i used. When a custom toolbar is set up, we also have to set a listener on the "up caret", and Guess-what the method is called drawertoggler.setNaviagtionOnClickListener ! [FACEPALM].
In the onCreate() method of the activity we need to add the following code:
mtoolbar.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager().popBackStackImmediate();
}
});
Note: If you have defined the ActionBarDrawerToggler in a different fragment.Then make it static and add a getter.
Other cases like OnBackPressed can be handled by setting a OnStackChangeListener in the activity and make necessary changes. It is clearly explained here :@riwnodennyk.
Hopefully i'll try to make a standard template and report back when added to GitHub.
这篇关于不能听上了插入符点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!