18

我正在尝试使用 Jeremy Feinstein 的优秀图书馆滑动菜单

就我而言,如果活动是在滑动菜单打开的情况下启动的,那就太好了

在此处输入图像描述

而不是像滑动菜单一样关闭

在此处输入图像描述

我试图投入toggle()活动,但没有用。

滑动SherlockFragmentActivity

它与SlidingFragmentActivity相同,只是它扩展了 SherlockFragmentActivity 而不是 FragmentActivity。

基本活动

public class BaseActivity extends SlidingSherlockFragmentActivity {
    private int mTitleRes;
    protected ListFragment mFrag;

    public BaseActivity(int titleRes) {
        mTitleRes = titleRes;
    }

    private static final String TAG = "BaseActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle(mTitleRes);

        // set the Behind View
        setBehindContentView(R.layout.menu_frame);
        FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
        mFrag = new SampleListFragment();
        t.replace(R.id.menu_frame, mFrag);
        t.commit();

        // customize the SlidingMenu
        SlidingMenu sm = getSlidingMenu();
        sm.setShadowWidthRes(R.dimen.shadow_width);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        sm.setFadeDegree(0.35f);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        sm.setOnOpenListener(new SlidingMenu.OnOpenListener() {
            @Override
            public void onOpen() {
                Log.d(TAG, "onOpen");
            }
        });
        sm.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
            @Override
            public void onOpened() {
                Log.d(TAG, "onOpened");
            }
        });
        sm.setOnCloseListener(new SlidingMenu.OnCloseListener() {
            @Override
            public void onClose() {
                Log.d(TAG, "onClose");
            }
        });
        sm.setOnClosedListener(new SlidingMenu.OnClosedListener() {
            @Override
            public void onClosed() {
                Log.d(TAG, "onClosed");
            }
        });



    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                toggle();
                return true;

        }
        return super.onOptionsItemSelected(item);
    }

}

翻转检查

public class FlipperCheck extends BaseActivity {

    private ViewFlipper flipper;
    private TextView secondaryText;

    public FlipperCheck() {
        super(R.string.app_name);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setSlidingActionBarEnabled(true);


        setContentView(R.layout.main);
        flipper = (ViewFlipper) findViewById(R.id.voicerecorder_textflipper);
        flipper.startFlipping();
        flipper.setInAnimation(AnimationUtils
                .loadAnimation(getApplicationContext(), android.R.anim.fade_in));
        flipper.setOutAnimation(AnimationUtils
                .loadAnimation(getApplicationContext(), android.R.anim.fade_out));

        secondaryText = (TextView) findViewById(R.id.voicerecorder_secondarytext);
        secondaryText.setText("Why cannot I see u...");

        final TextView firstText = (TextView) findViewById(R.id.firsttext);

        final TextView secondText = (TextView) findViewById(R.id.secondtext);


        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopFlipper();
                flipper.setVisibility(View.INVISIBLE);
                secondaryText.setVisibility(View.VISIBLE);
            }
        });

        Button secondButton = (Button) findViewById(R.id.second_button);
        secondButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                firstText.setText("Replaying");
                secondText.setText("Replaying");

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        toggle();
    }


    private void stopFlipper() {
        flipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {

            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {

                int displayedChild = flipper.getDisplayedChild();
                int childCount = flipper.getChildCount();

                if (displayedChild == childCount - 1) {
                    flipper.stopFlipping();
                }
            }
        });
    }


}

[编辑 2]

看起来像toggle()onStart()显示活动之前激活滑动菜单的打开和关闭。

01-01 19:27:53.780: D/BaseActivity(351): onOpen
01-01 19:27:53.780: D/BaseActivity(351): onOpened
01-01 19:27:53.780: D/BaseActivity(351): onClose
01-01 19:27:53.790: D/BaseActivity(351): onClosed
01-01 19:27:54.241: D/dalvikvm(351): GC_EXTERNAL_ALLOC freed 2725 objects / 223104 bytes in 80ms
01-01 19:27:54.370: D/dalvikvm(351): GC_EXTERNAL_ALLOC freed 290 objects / 12352 bytes in 75ms
01-01 19:27:54.520: I/ActivityManager(59): Displayed activity com.abc.FlipperCheck/.FlipperCheck: 3084 ms (total 3084 ms)

我的问题

使用 Android 库“滑动菜单”时,如何在滑动菜单打开的情况下启动活动?

4

3 回答 3

27

您可以使用:

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            toggle();
        }
    }, 1000);
}
于 2013-01-01T14:34:04.290 回答
11

Handler如果您从其中一个类继承,则有一种更好的方法可以做到这一点,而无需向您自己发布SlidingActivity

@Override
public void onPostCreate(Bundle savedInstanceState) {
    if (savedInstanceState == null) {
        savedInstanceState = new Bundle();
        savedInstanceState.putBoolean("SlidingActivityHelper.open", true);
    }
    super.onPostCreate(savedInstanceState);
}

这是有效的,因为SlidingActivityHelper从保存的实例状态恢复菜单打开/关闭状态。如果没有保存状态,那么将其设置为默认状态并没有什么坏处。

于 2013-07-05T16:06:25.600 回答
0
getSlidingMenu().post(new Runnable() {

        @Override
        public void run() {

           getSlidingMenu().showMenu();

        }
    });

把它放在 onCreate 中对我有用。

于 2013-12-19T06:52:55.287 回答