所以,我试图创建我的应用程序的导航抽屉,但我不知道为什么我不能调用public void onDrawerOpened,当我手动键入它时,它只是显示错误消息void is an invalid type for the variable onDrawerOpened

所以这是我的NavigationDrawerFragment.java代码

package id.WKKR.ktcafe;

import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class NavigationDrawerFragment extends Fragment {

    public DrawerLayout mDrawerLayout;
    public ActionBarDrawerToggle mDrawerToggle;

    public NavigationDrawerFragment() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_navigation_drawer, container,
                false);

    }

    public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {
        // TODO Auto-generated method stub
        mDrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout,
                toolbar, R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);
        {
            public void onDrawerOpened(View drawerView){
                super.onDrawerOpened(drawerView);
            }

            public void onDrawerClosed(View drawerView){
                super.onDrawerClosed(drawerView);
            }

        };
    }
}


感谢任何帮助。

(编辑)

所以这是我的mainactivity.java(称为MenuUtama.java)



► ► package id.WKKR.ktcafe;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;

@SuppressWarnings("deprecation")
public class MenuUtama extends ActionBarActivity{

	Toolbar toolbar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu_utama);

		toolbar = (Toolbar) findViewById(R.id.app_bar);
		setSupportActionBar(toolbar);

		NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
		drawerFragment.setUp((DrawerLayout)findViewById(R.id.drawer_layout),toolbar);


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.menu_utama, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Toast.makeText(this, "This Is " + item.getTitle() + " Button", Toast.LENGTH_SHORT).show();
			return true;
		}

		if (id == R.id.bill) {
			startActivity(new Intent(this, BillTagihanAnda.class));
		}
		return super.onOptionsItemSelected(item);
	}
}

最佳答案

您需要重写onDrawerOpened和onDrawerClosed方法。添加@override注释或实现,如下所示:

mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerlayout, toolBar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

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

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

            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);

                        };


将活动中的设置方法更改为:

NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setup(R.id.fragment_navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);


片段中的设置方法包括id片段,如我的评论之一所述。

10-08 06:03