简体   繁体   English

工具栏不适用于导航抽屉和多个活动

[英]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). 在主活动(TimerActivity)中,工具栏可以工作,但是当我单击icon_drawer时,它在其他活动中却不起作用(仅适用于滑动)。 How can I fix this problem? 我该如何解决这个问题? It seems that the toolbar is covered by another layout located above. 似乎工具栏位于上方的另一个布局中。 Sorry for bad english. 对不起,英语不好。

TimerActivity.java 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) activity_main.xml(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 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 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 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" 在包含工具栏的Activity XML中,尝试将它们添加到.xml文件中的<Toolbar>项中: android:descendantFocusability="false android:focusable="false"

Try to change activity_main.xml - move the toolbar outside to DrawerLayout: 尝试更改activity_main.xml-将工具栏移到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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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