自定义滑动菜单

时间:2014-07-02 09:37:43

标签: android slidingmenu

我在这里看过很多帖子,有人帮我,但我觉得我错过了什么......

我用它创建了一个ActivitySlider(只是一个代码示例)

public ActivitySlider(FragmentActivity activity, String loginU, String passU, String phpsessid)
{
    this.activity = activity;

    bundle = new Bundle();
    bundle.putString("loginU", loginU);
    bundle.putString("passU", passU);
    bundle.putString("phpsessid", phpsessid);

    // We get the View of the Activity
    this.content = (View) activity.findViewById(R.id.drawer_layout).getParent();
    ListView listTmp = (ListView) content.findViewById(R.id.left_drawer);
    String[] mMenuTitles = activity.getResources().getStringArray(R.array.menu_array);
    String[] tmp = {"Hello","'Suuppppp","  ?  "};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.activity, R.layout.drawer_list_item, tmp);
    listTmp.setAdapter(adapter);
    // And its parent


    // The container for the menu Fragment is added to the parent. We set an id so we can perform FragmentTransactions later on
    this.menuContainer = new FrameLayout(this.activity);

    this.menuContainer.setId(R.id.content_frame);

    // We set visibility to GONE because the menu is initially hidden
    //this.menuContainer.setVisibility(View.GONE);
    ViewGroup parent = (ViewGroup) this.content.getParent();
    parent.addView(this.menuContainer);
}

public <T extends Fragment> void setMenuFragment(Class<T> cls)
{
    Fragment fragment = Fragment.instantiate(this.activity, cls.getName());
    setMenuFragment(fragment);
}

public void setMenuFragment(Fragment fragment)
{
    FragmentManager manager = this.activity.getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    fragment.setArguments(bundle);
    transaction.replace(R.id.content_frame, fragment);
    transaction.commit();
}`

所以,我在另一个Activity中调用了这个Activity(我不能使用带有切换的导航抽屉,因为我已经有了标签):

 slider = new ActivitySlider(this, loginU, passU, sessid);
 slider.setMenuFragment(MenuFragment.class);

这是我的MenuFragment代码:

/**
 * "Constructeur" : inflate la vue
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    //Récupération des arguments
    int i = getArguments().getInt(ARG_MENU_NUMBER);
    String loginU = getArguments().getString("loginU");
    String passU = getArguments().getString("passU");
    String sessid = getArguments().getString("phpsessid");
    Log.i("MenuFrag loginU + passU",loginU + " - " + passU);

    Log.i("resArg", i + "");
    String section = "";
    View rootView = null;

    switch(i)
    {
        //Home
        case 0:
            rootView = inflater.inflate(R.layout.home, container, false);
            section = getResources().getStringArray(R.array.menu_array)[i];

            getActivity().setTitle(section);
            return rootView;
        //Demandes/Propositions
        case 1:
            rootView = inflater.inflate(R.layout.activity_viewpagertabs, container, false);
            section = getResources().getStringArray(R.array.menu_array)[i];

            getActivity().setTitle(section);

            Intent askpurp = new Intent(getActivity(),PagerWithTab.class);
            Bundle bundle = new Bundle();
            bundle.putString("loginU",loginU);
            bundle.putString("passU",passU);
            bundle.putString("phpsessid",sessid);
            askpurp.putExtras(bundle);
            getActivity().startActivity(askpurp);

            return rootView;
        //Awesome
        case 2:
            rootView = inflater.inflate(R.layout.awesome, container, false);
            section = getResources().getStringArray(R.array.menu_array)[i];

            getActivity().setTitle(section);
            return rootView;

        default:
            rootView = inflater.inflate(R.layout.awesome2, container, false);
            section = getResources().getStringArray(R.array.menu_array)[i];

            getActivity().setTitle(section);
            return rootView;
    }
}

滑动工作正常,但我左边有一个白色的盒子,我希望我的菜单在那里。我该怎么办?

1 个答案:

答案 0 :(得分:0)

不能像这样使用MenuFragment类。这里是新的MenuFragment:

public class MenuFrag extends ListFragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.menutest, null, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        String[] sections = getResources().getStringArray(R.array.menu_array);
        ArrayAdapter<String> sectionAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, sections);
        setListAdapter(sectionAdapter);
    }
}

这是布局:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/android:list"
            android:layout_alignParentRight="true"
            android:background="@color/wezo_color_blue"                          android:layout_alignParentTop="true"/>
</RelativeLayout>

您现在可以致电:

slider.setMenuFragment(MenuFrag.class);

要解决的最后一个问题是菜单大小。所有需要做的就是改变以下行:

this.content = (View) activity.findViewById(R.id.drawer_layout).getParent();

this.content = (View) activity.findViewById(android.R.id.content).getParent();

如果你不这样做,那么它需要实际的内容大小(在标签之后)。