简体   繁体   中英

Toolbar doesn't work with navigation drawer and multiple activities

I'm trying to develop an app for bikes with multiple activities. I developed a navigation drawer with multiple activities and toolbar instead of actionbar. In the main activity(TimerActivity), the toolbar works, but it doesn't works in the other activities when I click on the icon_drawer (it works only with the swipe). How can I fix this problem? It seems that the toolbar is covered by another layout located above. Sorry for bad english.

TimerActivity.java

public class TimerActivity extends ActionBarActivity {

    private static String CLASS_NAME;
    private Vibrator vibrate;
    private Notify notify;
    protected Toolbar toolbar;
    protected DrawerLayout drawerLayout;
    protected ActionBarDrawerToggle drawerToggle;
    public ListView leftDrawerList;
    protected ArrayList<navDrawerItems> NavDrawerItems;
    protected NavDrawerListAdapter adapter;
    private TypedArray navMenuIcons;
    protected String[] navMenuTitles;
    private CharSequence mTitle;
    private LinearLayout linearLayout;
    protected FrameLayout frameLayout;

    public TimerActivity() {
        CLASS_NAME = getClass().getName();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        Log.d(CLASS_NAME, "onCreate");
        setContentView(R.layout.activity_main);
        frameLayout = (FrameLayout) findViewById(R.id.frame_container);
        initView();
        toolbar.setTitle("Navigation Drawer");
        setSupportActionBar(toolbar);
        initDrawer();
        leftDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    }

    protected void initView() {
        //load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
        //nav drawer icons from resources
        navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        leftDrawerList = (ListView) findViewById(R.id.left_drawer);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        NavDrawerItems = new ArrayList<navDrawerItems>();
        //adding nav drawer items to array
        //Home
        NavDrawerItems.add(new navDrawerItems(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        //User
        NavDrawerItems.add(new navDrawerItems(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        //Map
        NavDrawerItems.add(new navDrawerItems(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        //Routes
        NavDrawerItems.add(new navDrawerItems(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
        //Camera
        NavDrawerItems.add(new navDrawerItems(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        //Weather
        NavDrawerItems.add(new navDrawerItems(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));

        //Recycle the typed array
        navMenuIcons.recycle();
        //setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),NavDrawerItems);
        leftDrawerList.setAdapter(adapter);





    }
    protected void initDrawer() {

        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);


            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);



            }
        };
        drawerLayout.setDrawerListener(drawerToggle);


    }
    private void gotoRoutes() {
        Log.d(CLASS_NAME, "gotoRoutes");

        Intent routes = new Intent(this, RoutesActivity.class);

        startActivity(routes);
    }

    /**
     * Called when the settings button is clicked on.
     * 
     * @param view
     * the button clicked on
     */
    public void gotoSettings(View view) {
        Log.d(CLASS_NAME, "gotoSettings");

        Intent settings = new Intent(this, SettingsActivity.class);

        startActivity(settings);
    }

    private void gotoHome() {
        Log.d(CLASS_NAME, "gotoHome");

        Intent timer = new Intent(this, TimerActivity.class);
        timer.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        startActivity(timer);
    }

    public void gotoMap() {
        Log.d(CLASS_NAME, "gotoMap");

        Intent map = new Intent(this, MapActivity.class);

        startActivity(map);
    }

    public void gotoPhoto() {
        Log.d(CLASS_NAME, "gotoPhoto");

        Intent photo = new Intent(this, PhotoActivity.class);

        startActivity(photo);
    }

    public void shareActivity() {
        Log.d(CLASS_NAME, "shareActivity");
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }  



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
        case android.R.id.home:
            gotoHome();
            return true;
        case R.id.menu_settings:
            gotoSettings(null);
            return true;
        case R.id.menu_routes:
            gotoRoutes();
            return true;
        case R.id.menu_map:
            gotoMap();
            return true;
        case R.id.menu_photo:
            gotoPhoto();
            return true;
        case R.id.menu_share:
            shareActivity();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }

    }


    public void notification(String title, String message) {
        notify.notify(title, message);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(CLASS_NAME, "onDestroy");
    }

    @Override
    public void onRestart() {
        super.onRestart();
        Log.d(CLASS_NAME, "onRestart");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.d(CLASS_NAME, "onCreateOptionsMenu");

        getMenuInflater().inflate(R.menu.activity_main, menu);

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Keep the screen on depending on the stay awake setting
     */
    protected void stayAwakeOrNot() {
        Log.d(CLASS_NAME, "stayAwakeOrNot");

        Settings settings = ((OnYourBike) getApplication()).getSettings();

        if (settings.isCaffeinated(this)) {
            // Log.i(CLASS_NAME, "Staying awake");
            getWindow()
            .addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        } else {
            // Log.i(CLASS_NAME, "Not staying awake");
            getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }
    public void takePhoto(View view){
        Log.d(CLASS_NAME, "takePhoto");

        //if(camera.hasCamera() && camera.hasCameraApplication()){
        //  camera.takePhoto();

        Intent photo = new Intent(this, PhotoActivity.class);
        startActivity(photo);

    }



    public void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch(position){
        case 0:
            Intent intent1 = new Intent(this, CounterActivity.class);
            startActivity(intent1);
            finish();
            break;
        case 1:
            Intent intent2 = new Intent(this, UserActivity.class);
            startActivity(intent2);
            finish();
            break;
        case 2:
            Intent intent3 = new Intent(this, MapActivity.class);
            startActivity(intent3);
            finish();
            break;
        case 3:
            Intent intent4 = new Intent(this, RoutesActivity.class);
            startActivity(intent4);
            finish();
            break;
        case 4:
            Intent intent5 = new Intent(this, PhotoActivity.class);
            startActivity(intent5);
            finish();
            break;
        case 5:
            Intent intent6 = new Intent(this, FragmentWeather.class);
            startActivity(intent6);
            finish();
            break;
        default: 

            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
            // update selected item and title, then close the drawer
            leftDrawerList.setItemChecked(position,true);
            leftDrawerList.setSelection(position);
            //setTitle(navMenuTitles[position]);
            //drawerLayout.closeDrawer(leftDrawerList);
            drawerLayout.closeDrawer(leftDrawerList);
            toolbar.setTitle(navMenuTitles[position]);
        }
        else{
            // error in creating fragment
            Log.e("TimerActivity", "Error in creating fragment");

        }



    }

    class SlideMenuClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            displayView(position);

        }

    }

}

activity_main.xml (layout for TimerActivity)

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp" >


     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <include layout="@layout/toolbar" />

    </LinearLayout>
    <!-- Framelayout to display Fragments -->

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#A0E843"
        android:divider="#eee"
        android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

UserActivity.java

public class UserActivity extends TimerActivity {

private int position;
private DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_user);
    getLayoutInflater().inflate(R.layout.activity_user, frameLayout );
    leftDrawerList.setItemChecked(position,true);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    //frameLayout = (FrameLayout) findViewById(R.id.frame_container);
    toolbar.setTitle("Useeeer activity");
    setSupportActionBar(toolbar);
    drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
    setSupportActionBar(toolbar);
    super.initView();
    super.initDrawer();


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.user, menu);
    return true;
}


}

user_activity.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <include layout="@layout/toolbar" />
    </FrameLayout>

    <!-- Framelayout to display Fragments -->


    <!-- navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#A0E843"
        android:divider="#eee"
        android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.v7.widget.Toolbar>

what i see from your codes is in the working activity you use toolbar in this way :

     <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<include layout="@layout/toolbar" />

</LinearLayout>
<!-- Framelayout to display Fragments -->

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

which is true! but the other layout does not follow this structure , its like this :

    <FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <include layout="@layout/toolbar" />
</FrameLayout>

try to fix this and tell me if it works!

I have had problems like this before. Your activities could be taking the focus away from your toolbar.

In your Activity XML which contains your toolbar, try adding these to your <Toolbar> item in the .xml file: android:descendantFocusability="false android:focusable="false"

Try to change activity_main.xml - move the toolbar outside to DrawerLayout:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <include layout="@layout/toolbar" />

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp" >

    </LinearLayout>
    <!-- Framelayout to display Fragments -->

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#A0E843"
        android:divider="#eee"
        android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM