2013-07-08 123 views
1

我正在開發一個安卓應用程序Sliding MenuActionBar Sherlock我迷路了。我無法打開滑動菜單

我跟着這Spanish tutorial將它們添加到我的項目。

這是我的代碼:

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; 
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; 

import android.os.Bundle; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.view.View; 

public class MainActivity extends SlidingFragmentActivity 
{ 
    /** 
    * Used as TAG on Log messages. 
    */ 
    public static final String TAG = "MainActivity"; 
    private SlidingMenu menu; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Generate keys to encrypt and decrypt user password. 
     generateCryptoKeys(); 

     setBehindContentView(R.layout.menu); 

     setSlidingActionBarEnabled(false);   

     menu = new SlidingMenu(this); 
     menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); 
     menu.setShadowWidthRes(R.dimen.shadow_width); 
     menu.setShadowDrawable(R.drawable.shadow); 
     menu.setBehindOffsetRes(R.dimen.slidingmenu_offset); 
     menu.setFadeDegree(0.35f); 
     menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT); 

     getSupportActionBar().setDisplayShowCustomEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
} 

這是菜單片段:

public class MenuFragment extends ListFragment { 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.menu_list, null); 
    } 

    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     SampleAdapter adapter = new SampleAdapter(getActivity()); 
     for (int i = 0; i < 20; i++) { 
      adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search)); 
     } 
     setListAdapter(adapter); 
    } 

    private class SampleItem { 
     public String tag; 
     public int iconRes; 
     public SampleItem(String tag, int iconRes) { 
      this.tag = tag; 
      this.iconRes = iconRes; 
     } 
    } 

    public class SampleAdapter extends ArrayAdapter<SampleItem> { 

     public SampleAdapter(Context context) { 
      super(context, 0); 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      if (convertView == null) { 
       convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row, null); 
      } 
      ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon); 
      icon.setImageResource(getItem(position).iconRes); 
      TextView title = (TextView) convertView.findViewById(R.id.row_title); 
      title.setText(getItem(position).tag); 

      return convertView; 
     } 
    } 
} 

我有幾個問題:

1.我必須使用片段?
2.爲什麼我點擊主頁按鈕後無法打開菜單?
看到如下圖所示:

enter image description here

要操作欄福爾摩斯與滑動菜單集成我已經做到了這一點:

Go into the SlidingActivities that you plan on using make them extend Sherlock___Activity instead of ___Activity. 

而且,當我做到這一點:

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

我得到這個錯誤:

Cannot override the final method from SherlockFragmentActivity

回答

2
  1. Do I have to use fragments?

是,該庫是專爲它。另外,沒有兩個Activity可以同時在前臺。

2 . Why I can't open the menu when I click on home button?

這是因爲該按鈕不打算打開滑動菜單。

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

將啓用應用程序圖標作爲後退按鈕。這從堆棧返回。另見Navigation。你的slindermenu是一個片段,放在你的活動一側,因此它們在同一時刻存在,因此不會與任何導航無關。

要打開菜單,你需要調用:

SlidingMenu sm = getSlidingMenu(); 
    sm.toggle();//or .open(); 

,如果你想覆蓋onOptionsItemSelected,您需要:

extends SlidingFragmentActivity 

修改滑動菜單庫。所有的碎片都從sherlock庫相關的碎片中延伸出來。 (順便說一句,這是寫在docs設置與ActionBarSherlock。)。

+0

謝謝你的回答,但是:我需要做些什麼才能打開菜單? – VansFannel

+0

如果我這樣做:'public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)',我可以打開滑動菜單。 – VansFannel

3

使用財產以後這樣的:

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

     return true; 
    } 
+0

我不能那樣做。我嘗試過,但我得到這個錯誤:'不能覆蓋從SherlockFragmentActivity'最後的方法。 – VansFannel

+0

在你的導入中,刪除導入的「menu」並替換爲:'com.actionbarsherlock.view。*。'你可能導入了錯誤的包 – marshallino16