2013-03-22 66 views
0

所以我試圖在我的應用程序實現側導航,我似乎無法找到一種方法來實現它我想要的方式。我已經看過多個例子,並且我從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 公共類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; 
} 



} 

但是它現在與錯誤消息崩潰:11月3日至22日:54:24.469:E/AndroidRuntime(31673):java.lang.IllegalArgumentException:沒有找到id爲0x7f060000的片段虛擬視圖{42540328#1 id = 0x7f060000} 對此的任何想法。

回答

1

這是一個簡單的答案:

不要使用活動!

只有一個包含R.id.menu(菜單)和R.id.content(示例名稱)的佈局的活動,那麼您將使用Fragment和FragmentTransaction以及FragmentManager來處理R中的片段。 id.content(而菜單將很好地動畫)。

此外,我建議你使用這個庫的菜單:https://github.com/jfeinstein10/SlidingMenu我以前使用它,它的工作真的很好。

快樂編碼。

+0

謝謝,請你看看編輯,看看你能幫我嗎? – user1823974 2013-03-22 16:05:10