多个活动上的导航抽屉动画

时间:2013-11-07 17:10:59

标签: android layout navigation drawer

我想在多个活动中使用navigationDrawer选择使用AbstractActivity的方法,该AbstractActivity承载NavDrawerl布局和初始化,我的继承活动只是将布局设置为drawerLayout的内容。一切正常,抽屉可以在任何地方到达。然后单击一个List项应该开始一个新活动。

我选择尝试这个,因为我不想要一个单独的活动,而不是托管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方法。

0 个答案:

没有答案