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

Posted

技术标签:

【中文标题】实现侧边导航以在活动之间切换【英文标题】:Implementing Side Navigation to switch between activities 【发布时间】:2013-03-12 11:28:02 【问题描述】:

所以我试图在我的应用程序中实现侧边导航,但我似乎无法找到一种方法来按照我想要的方式实现它。我看过多个示例,我从科罗维扬斯克遇到了这个,它非常符合我想要的模式,但是当按下菜单项时,我似乎无法在活动之间切换。我通过调整“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 公共类 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_
android:layout_
android:background="@drawable/bg_android" >

<RelativeLayout
    android:layout_
    android:layout_
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:background="#bb000000">

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

    <TextView android:layout_

        android:layout_
        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_
 android:layout_>

<FrameLayout
    android:id="@+id/slideout_placeholder"
    android:layout_
    android:layout_
    android:background="#777777"/>


<ImageView
    android:id="@+id/slidedout_cover"
    android:layout_
    android:layout_
    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: No view found for id 0x7f060000 for fragment Dummy42540328 #1 id=0x7f060000 知道为什么会这样。

【问题讨论】:

【参考方案1】:

这是一个简单的答案:

不要使用活动!

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

此外,我建议您将这个库用于菜单:https://github.com/jfeinstein10/SlidingMenu 我以前用过它,它真的很好用。

编码愉快。

【讨论】:

谢谢你,你能看看编辑,看看你能不能帮帮我?

以上是关于实现侧边导航以在活动之间切换的主要内容,如果未能解决你的问题,请参考以下文章

想要在一项活动中添加底部和侧边导航,但如何?

在片段和活动之间导航

使用单击侧边栏导航菜单项调用活动

引导侧边栏导航菜单滚动到活动面板标题的顶部

Reload Activity 以在 Fragment 之间重新切换

想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉