为什么不为工具栏使用ClickListeners?

时间:2016-05-10 13:58:02

标签: android android-fragments materialdrawer

我使用MaterialDrawer,代码MainActivity是:

public class MainActivity extends AppCompatActivity implements KitchenFragment.CallbackOne {
    public static final String TAG = "myLogTag";

    private Toolbar mToolbar;
    private Drawer drawer;
    private FragmentManager fm;

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

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "CLICK NOCL");
            }
        });
        setSupportActionBar(mToolbar);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(false);


        fm = getFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.content_frame);

        if (fragment == null) {
            fragment = MenuFragment.newInstance();
            fm.beginTransaction()
                    .add(R.id.content_frame, fragment)
                    .commit();

        }

        drawer = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(mToolbar)
                .withActionBarDrawerToggle(true)
                .withHeader(R.layout.drawer_header)
                .addDrawerItems(
                        new PrimaryDrawerItem().withName(R.string.menu).withIdentifier(1),
                        new PrimaryDrawerItem().withName(R.string.kitchen_title).withIdentifier(2),
                        new PrimaryDrawerItem().withName(R.string.information_title).withEnabled(false)
                ).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        Log.d(TAG, "position clicked: " + position);
                        Fragment fragment = MenuFragment.newInstance();
                        switch (position) {
                            case 1:
                                fragment = MenuFragment.newInstance();
                                break;
                            case 2:
                                fragment = KitchenFragment.newInstance();
                                break;
                            default:
                                fragment = new Fragment();
                        }
                        fm.beginTransaction().replace(R.id.content_frame, fragment).commit();
                        return false;
                    }
                })
                .withFireOnInitialOnClick(true)
                .withSavedInstance(savedInstanceState)
                .withOnDrawerNavigationListener(new Drawer.OnDrawerNavigationListener() {
                    @Override
                    public boolean onNavigationClickListener(View view) {
                        Log.d(TAG, "CLICK in DNL");
                        if (!drawer.getActionBarDrawerToggle().isDrawerIndicatorEnabled()) {
                            onBackPressed();
                            return true;
                        } else
                            return false;
                    }
                })
                .build();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(TAG, "CLICK OIS");
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
            case android.R.id.home:
                getFragmentManager().popBackStackImmediate();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState = drawer.saveInstanceState(outState);
        super.onSaveInstanceState(outState);

    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen())
            drawer.closeDrawer();
        else if (getFragmentManager().getBackStackEntryCount() == 1) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            drawer.getActionBarDrawerToggle().syncState();
            getFragmentManager().popBackStack();
        } else if (getFragmentManager().getBackStackEntryCount() > 0)
            getFragmentManager().popBackStack();
        else
            super.onBackPressed();
    }

    @Override
    public void setFirstSelected() {
        drawer.setSelection(1);
    }
}

我正在尝试追踪mToolbar.setNavigationOnClickListener中的点击次数 和withOnDrawerNavigationListeneronOptionsItemSelected。 没有听众没有回应点击。

如何看待我使用Activity启动Fragment(1),后者又允许Fragment(2和3)。在Fragment的{​​{1}}中我使用OnCreate,我需要按下后退按钮(在工具栏中),返回上一个片段,而不是打开抽屉

2 个答案:

答案 0 :(得分:1)

MaterialDrawer已经为您处理了所有侦听器。如果您需要在抽屉打开或关闭后执行操作,您可以通过DrawerBuilder

提供听众

对于你有这个监听器的图标: OnDrawerNavigationListener https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/DrawerBuilder.java#L1158

对于抽屉关闭/打开此侦听器: OnDrawerListener https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/DrawerBuilder.java#L1116

答案 1 :(得分:0)

我建议你使用android studio向导创建新的{5F30A70F-8256-4ee3-8FCD-A65CF62C25CA}。文件 - >新建 - >活动 - >导航抽屉活动。 在这种情况下,使用Activity

ActionBarDrawerToaggle

此切换包含用于处理打开/关闭抽屉功能的单击侦听器。 您也可以通过ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

实施该项目点击