实施侧导航以在活动之间切换

时间:2013-03-22 14:45:53

标签: java android android-side-navigation

所以我尝试在我的应用中实现侧面导航,但我似乎找不到按照我想要的方式实现它的方法。我已经看过多个例子,我从korovyansk那里得到的这个非常符合我想要的模具,但是当按下菜单项时,我似乎无法在活动之间切换。我让应用程序通过调整" onListItemClick"来切换活动。并启动一个新的Intent,但是当活动开始时它会占据整个屏幕并重叠侧边栏动画,从而使它看起来很邋..我希望它在右边的布局中加载活动,而侧面导航正在关闭左边的类似facebook。有没有简单的方法可以做到这一点?

SampleActivity.java

public class SampleActivity extends FragmentActivity {

@TargetApi(11)
@Override

public void onCreate(Bundle savedInstanceState) {
    int poss;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        getActionBar().hide();
    }
    findViewById(R.id.sample_button).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
                    SlideoutActivity.prepare(SampleActivity.this, R.id.inner_content, width);
                    startActivity(new Intent(SampleActivity.this,
                            MenuActivity.class));
                    overridePendingTransition(0, 0);
                }
            });
}


}

MenuActivity.java

public class MenuActivity extends FragmentActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSlideoutHelper = new SlideoutHelper(this);
    mSlideoutHelper.activate();
    getSupportFragmentManager().beginTransaction().add(com.korovyansk.android.slideout.R.id.slideout_placeholder, new MenuFragment(), "menu").commit();
    mSlideoutHelper.open();
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        mSlideoutHelper.close();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


public SlideoutHelper getSlideoutHelper(){
    return mSlideoutHelper;
}

private SlideoutHelper mSlideoutHelper;


}

SlideoutHelper.java

public class SlideoutHelper {

private static Bitmap sCoverBitmap = null;
private static int sWidth = -1;

public static void prepare(Activity activity, int id, int width) {
    if (sCoverBitmap != null) {
        sCoverBitmap.recycle();
    }
    Rect rectgle = new Rect();
    Window window = activity.getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int statusBarHeight = rectgle.top;

    ViewGroup v1 = (ViewGroup) activity.findViewById(id).getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap source = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    if (statusBarHeight != 0) {
        sCoverBitmap = Bitmap.createBitmap(source, 0, statusBarHeight, source.getWidth(), source.getHeight() - statusBarHeight);
        source.recycle();
    } else {
        sCoverBitmap = source;
    }
    sWidth = width;
}

public SlideoutHelper(Activity activity) {
    this(activity, false);
}

public SlideoutHelper(Activity activity, boolean reverse) {
    mActivity = activity;
    mReverse = reverse;
}

public void activate() {
    mActivity.setContentView(R.layout.slideout);
    mCover = (ImageView) mActivity.findViewById(R.id.slidedout_cover);
    mCover.setImageBitmap(sCoverBitmap);
    mCover.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            close();
        }
    });
    int x = (int) (sWidth * 1.2f);
    if (mReverse) {
        @SuppressWarnings("deprecation")
        final android.widget.AbsoluteLayout.LayoutParams lp = new android.widget.AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, x, 0);
        mActivity.findViewById(R.id.slideout_placeholder).setLayoutParams(lp);
    } else{
        @SuppressWarnings("deprecation")
        final android.widget.AbsoluteLayout.LayoutParams lp = new android.widget.AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 0, 0);
        mActivity.findViewById(R.id.slideout_placeholder).setLayoutParams(lp);
    }
    initAnimations();
}

public void open() {
    mCover.startAnimation(mStartAnimation);
}

public void close() {
    mCover.startAnimation(mStopAnimation);
}

protected void initAnimations() {
    int displayWidth = ((WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
    final int shift = (mReverse ? -1 : 1) * (sWidth - displayWidth);
    mStartAnimation = new TranslateAnimation(
            TranslateAnimation.ABSOLUTE, 0,
            TranslateAnimation.ABSOLUTE, -shift,
            TranslateAnimation.ABSOLUTE, 0,
            TranslateAnimation.ABSOLUTE, 0
            );

    mStopAnimation = new TranslateAnimation(
            TranslateAnimation.ABSOLUTE, 0,
            TranslateAnimation.ABSOLUTE, shift,
            TranslateAnimation.ABSOLUTE, 0,
            TranslateAnimation.ABSOLUTE, 0
            );
    mStartAnimation.setDuration(DURATION_MS);
    mStartAnimation.setFillAfter(true);
    mStartAnimation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mCover.setAnimation(null);
            @SuppressWarnings("deprecation")
            final android.widget.AbsoluteLayout.LayoutParams lp = new android.widget.AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, -shift, 0);
            mCover.setLayoutParams(lp);
        }
    });

    mStopAnimation.setDuration(DURATION_MS);
    mStopAnimation.setFillAfter(true);
    mStopAnimation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mActivity.finish();
            mActivity.overridePendingTransition(0, 0);
        }
    });
}

private static final int DURATION_MS = 400;
private ImageView mCover;
private Activity mActivity;
private boolean mReverse = false;
private Animation mStartAnimation;
private Animation mStopAnimation;
}    

MenuFragement.java     public class MenuFragment扩展了ListFragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, new String[] { " First", " Second", " Third", " Fourth", " Fifth", " Sixth"}));
    getListView().setCacheColorHint(0);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);


     ((MenuActivity)getActivity()).getSlideoutHelper().close();

}


}

XML ..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inner_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_android" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="45dip"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:background="#bb000000">

    <Button style="@android:style/Widget.Button.Small"
        android:id="@+id/sample_button"
        android:layout_width="35dip"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dip"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:text=">" />

    <TextView android:layout_width="wrap_content"

        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/sample_button"
        android:layout_centerVertical="true"
        android:textSize="19sp"
        android:textColor="#ffffff"
        android:text="Facebook-like slide-out nav"/>
</RelativeLayout>

slideout.xml

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

<FrameLayout
    android:id="@+id/slideout_placeholder"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#777777"/>


<ImageView
    android:id="@+id/slidedout_cover"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY" />

</AbsoluteLayout>

编辑: 好的是试图避免这种情况,但现在似乎不可避免。所以即时尝试使用FragmentTransaction。

在onListItemClick下的MenuFragment.java中我有:

     @Override
     public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);


     ((MenuActivity)getActivity()).getSlideoutHelper().close();
     Fragment dummy = new Dummy();
     FragmentTransaction transaction = getFragmentManager().beginTransaction();

     // Replace whatever is in the fragment_container view with this fragment,
     // and add the transaction to the back stack
     transaction.replace(R.id.inner_content, dummy);
     transaction.addToBackStack(null);

     // Commit the transaction
     transaction.commit();
}

Dummy.java

public class Dummy extends Fragment{
 View blag;
 @TargetApi(11)

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

}
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                      Bundle savedInstanceState) {

   blag = inflater.inflate(R.layout.samplex, container, false);
  //(samplex is same as sample.xml just different logo
  return blag;
}



}

但它现在崩溃并显示错误消息:03-22 11:54:24.469:E / AndroidRuntime(31673):java.lang.IllegalArgumentException:找不到ID为0x7f060000的视图,用于片段虚拟{42540328# 1 id = 0x7f060000} 任何关于为什么会这样的想法。

1 个答案:

答案 0 :(得分:1)

这是一个简单的答案:

不要使用活动!

只有一个活动的布局包含R.id.menu(菜单)和R.id.content(示例名称),然后您将使用Fragment和FragmentTransaction以及FragmentManager来操作R内的片段.id.content(虽然菜单会有很好的动画效果)。

此外,我建议您使用此库作为菜单:https://github.com/jfeinstein10/SlidingMenu我以前使用它并且它非常好用。

快乐的编码。

相关问题