我正在使用Sliding Menu和ActionBar Sherlock开发一款Android应用,我迷失了。
我已按照此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.为什么单击主页按钮时无法打开菜单?请参见下图:
要将Action Bar Sherlock与Sliding菜单集成,我已经这样做了:
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
。
答案 0 :(得分:3)
使用类似的东西:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
menu.toggle();
return true;
}
答案 1 :(得分:2)
- 我必须使用片段吗?
醇>
是的,该库是为它而构建的。另外,没有两个Activity可以同时在前台。
2。为什么我点击主页按钮时无法打开菜单?
这是因为该按钮不打算打开滑动菜单。
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
将启用应用图标作为后退按钮。这从堆栈返回。另见Navigation。你的slindermenu是一个片段放在你的活动的一边,因此它们在同一时刻存在,因此不与任何导航无关。
要打开菜单,您需要致电:
SlidingMenu sm = getSlidingMenu();
sm.toggle();//or .open();
如果您要覆盖onOptionsItemSelected
,则需要:
extends SlidingFragmentActivity
修改滑动菜单库。让所有片段从sherlock库相关片段扩展。 (顺便说一下,这是用设置ActionBarSherlock 下的docs写的。)。