我正在开发一款使用jmeinstein's Sliding Menu的Android应用。因为我需要扩展SlidingListActivity,所以我决定关注baruckis Android SlidingMenuImplementation,使用SlidingMenuBuilderBase
,SlidingMenuListAdapter
等类。我遇到的问题是菜单似乎有点关闭页面。然而,关于这个问题最有趣的部分是它有时会发生。
我上传了一段私人YouTube视频,向您展示我的意思:
https://www.youtube.com/watch?v=0eYlqRl6F3k&feature=youtu.be
正如您在视频中看到的那样,两个页面(所有活动和我的活动)都遇到了第一个菜单项被略微切断的问题。但是,随机,My Events开始正确地纠正菜单,最终,All Events也做了。但我似乎无法弄清楚为什么会发生这种情况的模式。
为了绝对清楚,我发布了错误的图像(我有时看到)和正确的图像(我有时也会看到):
现在,代码。就像我说的,我的代码与jfeinstein的直接实现不同,我遵循baruckis的实现,这应该是正确的,正如baruckis的示例应用程序所证明的那样。
private void createSlidingMenu(Session session) {
// If nothing is set, than sliding menu wont be created.
if (setSlidingMenu() != null) {
Class<?> builder = setSlidingMenu();
try {
// We use our made base builder to create a sliding menu.
slidingMenuBuilderBase = (SlidingMenuBuilderBase) builder
.newInstance();
slidingMenuBuilderBase.createSlidingMenu(this, session, userName, userId);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
从主页打开调用此方法。现在,这是创建滑动菜单:
public void createSlidingMenu(Activity activity, Session session, String userName, String userId) {
this.activity = activity;
// For actual sliding menu creation we use an external open source
// Android library called "SlidingMenu". It can be found at
// "https://github.com/jfeinstein10/SlidingMenu".
// We configure the SlidingMenu to our needs.
menu = new SlidingMenu(activity);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setAboveOffset(R.dimen.above_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(activity, SlidingMenu.SLIDING_WINDOW);
menu.setMenu(R.layout.menu_frame);
this.session = session;
this.userName = userName;
System.out.println(userName);
SlidingMenuListFragment slidingMenuListFragment = new SlidingMenuListFragment(userName, userId);
slidingMenuListFragment.setMenuBuilder(this);
// We replace a FrameLayout, which is a content of sliding menu, with
// created list fragment filled with data from menu builder.
activity.getFragmentManager().beginTransaction()
.replace(R.id.menu_frame, slidingMenuListFragment)
.commitAllowingStateLoss();
}
其他重要的代码示例:
SlidingMenuListFragment.java:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// We get a list from our specially created list data class.
slidingMenuList = SlidingMenuList.getSlidingMenu(getActivity(), userName);
// We pass our taken list to the adapter.
SlidingMenuListAdapter adapter = new SlidingMenuListAdapter(
getActivity(), R.layout.sliding_menu_holo_light_list_row, slidingMenuList, userId);
setListAdapter(adapter);
}
sliding_menu_holo_light_list.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6F0B0B"
android:divider="#c0c0c0"
android:dividerHeight="2dip"
android:paddingLeft="5dip"
android:paddingRight="5dip" />
sliding_menu_holo_light_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/row_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="@string/empty_string"
android:padding="10dp" />
<TextView
android:id="@+id/row_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#FFFFFF"
android:padding="10dp"
android:text="@string/empty_string"
android:textAppearance="@android:style/TextAppearance.Medium" />
</LinearLayout>
我一直试图解决这个问题。非常感谢所有帮助!