1

我想选择在多个活动中使用navigationDrawer 的方法,该方法使用一个承载NavDrawerl 布局和初始化的AbstractActivity,而我继承的活动只是将布局设置为drawerLayout 的内容。一切正常,抽屉随处可见。单击一个列表项应该会开始一个新的活动。

我选择尝试这个,因为我不想要一个单一的活动,而不是托管 10k 行代码来实现我所有的进程间通信和后台服务的东西。一切正常。除了抽屉确实在我新开始的活动中关闭。这不好,因为通常如果您从列表中选择某些内容,它会很好地滑回。我想找到一个折衷方案,只是为新活动的结束设置动画,这不会有太大的不同。但我无法让它工作,因为我所有的电话


public abstract class AbstractNavDrawerActivity extends FragmentActivity {

    private CharSequence mTitle;
    private CharSequence mDrawerTitle;
    private ActionBarDrawerToggle mDrawerToggle;

    private DrawerLayout mDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.addOnLayoutChangeListener(new OnLayoutChangeListener() {

            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
                    int oldBottom) {

            }
        });
        // mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(
                this, /* host Activity */
                mDrawerLayout, /* DrawerLayout object */
                R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open, /* "open drawer" description for accessibility */
                R.string.drawer_close /* "close drawer" description for accessibility */
                ) {
                    public void onDrawerClosed(View view) {
                        getActionBar().setTitle(mTitle);
                        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                    }

                    public void onDrawerOpened(View drawerView) {
                        getActionBar().setTitle(mDrawerTitle);
                        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                    }
                };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        ViewGroup flContent = (ViewGroup) findViewById(R.id.content);
        flContent.addView(getContentView());

        ViewGroup leftDrawer = (ViewGroup) findViewById(R.id.left_drawer);
        MenuFragment mf = new MenuFragment();
        mf.setListener(new DrawerItemClickListener());
        getSupportFragmentManager().beginTransaction().add(R.id.left_drawer, mf)
                .commitAllowingStateLoss();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (getIntent().hasExtra("close")) {
            if (getIntent().getExtras().getBoolean("close")) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        }
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();

    }
    @Override
    protected void onResume() {
        super.onResume();
        if (getIntent().hasExtra("close")) {
            if (getIntent().getExtras().getBoolean("close")) {
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            }
        }
    }

    protected abstract View getContentView();

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

    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        switch (position) {
        case 0:
            startActivity(new Intent(this, OneActivity.class));
            break;
        case 1:
            Intent it = new Intent(this, TwoActivity.class);
            it.putExtra("close", true);
            startActivity(it);
            break;

        case 2:
            mDrawerLayout.closeDrawer(Gravity.LEFT);
            break;
        default:
            break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


public class OneActivity extends AbstractNavDrawerActivity {

    @Override
    protected View getContentView() {
        return getLayoutInflater().inflate(R.layout.ac_one, null);
    }

有没有人知道我们如何才能让它看起来“酷”。我认为这对于复杂的应用程序来说是一个很好的解决方案。我不喜欢 One Activity 1943209483280 Fragment Action Listener 方法。

4

0 回答 0