如何在标签菜单中使用导航抽屉

时间:2014-01-29 20:34:57

标签: java android android-layout android-fragments drawer

我一直在开发一个应用程序,其底部有一个Swipeable菜单,其中一个页面应该有一个导航抽屉来更改自己的碎片。

MainActivity.java

package com.example.elizehprotowithshlkandvpis;
import com.example.elizehprotowithshlkandvpis.adapter.MainMenuAdapter;
import com.viewpagerindicator.TabPageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity {
    private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };
    private ViewPager mViewPager;
    private FragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter    = new MainMenuAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(adapter);

        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(mViewPager);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public static String[] getCONTENT() {
        return CONTENT;
    }
}

MainMenuAdapter.java

package com.example.elizehprotowithshlkandvpis.adapter;
import com.example.elizehprotowithshlkandvpis.MainActivity;
import com.example.menuclasses.AboutUs;
import com.example.menuclasses.CustomerClub;
import com.example.menuclasses.Maps;
import com.example.menuclasses.Neccesseries;
import com.example.menuclasses.Tours;
import com.example.menuclasses.UrgentCall;
import com.example.menuclasses.Resturants;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MainMenuAdapter extends FragmentPagerAdapter {
    private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };

    private final int HOME_INDEX         = 0;
    private final int ABOUTUS_INDEX      = 1;
    private final int MAPS_INDEX         = 2;
    private final int RESTURANTS_INDEX   = 3;
    private final int TOURS_INDEX        = 4;
    private final int CUSTOMERCLUB_INDEX = 5;
    private final int NECCESSERIES_INDEX = 6;
    private final int URGENTCALL_INDEX   = 7;
    private final int NUMBEROFMENUS      = CONTENT.length;

    public MainMenuAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String[] CONTENT = MainActivity.getCONTENT();
        return CONTENT[position % CONTENT.length].toUpperCase();
    }


    @Override
    public Fragment getItem(int index) {
        switch(index) {
        case HOME_INDEX:
            return new AboutUs();

        case ABOUTUS_INDEX:
            return new AboutUs();

        case MAPS_INDEX:
            return new Maps();

        case RESTURANTS_INDEX:
            return new Resturants();

        case TOURS_INDEX:
            return new Tours();

        case CUSTOMERCLUB_INDEX:
            return new CustomerClub();

        case NECCESSERIES_INDEX:
            return new Neccesseries();

        case URGENTCALL_INDEX:
            return new UrgentCall();
        }
        return null;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return NUMBEROFMENUS;
    }
}

应该包含导航抽屉的碎片是Resturants,抽屉的主要活动也是用Sherlock Library(SherlockFragmentActivity)实现的。 我很困惑如何从SherlockActivity调用SherlockFragmentActivity:)

由于

1 个答案:

答案 0 :(得分:0)

本教程可以帮助您:

http://developer.android.com/training/implementing-navigation/nav-drawer.html

这是切换片段的代码段

// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.content_frame, fragment)
               .commit();