为主页创建导航抽屉布局

时间:2014-09-14 15:46:01

标签: android android-layout navigation-drawer

我已经自定义了默认Android Studio ActionBar Tab + Swipe Views,现在我希望有一个主页,我有Navigation Drawer布局,当我向左滑动它时,我会在标签布局中输入。请问你能帮帮我吗?我不知道从哪里开始。

修改 这是我的MainActivity:

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener, android.app.ActionBar.TabListener {
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminateVisibility(true);
    setProgressBarIndeterminateVisibility(false);
    setContentView(R.layout.activity_main);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);

    if (!welcomeScreenShown) {


        String whatsNewText = getResources().getString(R.string.Text);
        new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT).setMessage(whatsNewText).setPositiveButton(
                android.R.string.ok, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean(welcomeScreenShownPref, true);
        editor.commit(); // Very important to save the preference
    }

    mCollectionPagerAdapter = new CollectionPagerAdapter(
            getSupportFragmentManager());

    final android.app.ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setStackedBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.tab_color)));
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override

        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

    });
    for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mCollectionPagerAdapter.getPageTitle(i))
                .setTabListener(this));

    }

}

public void onTabUnselected(ActionBar.Tab tab,
                            FragmentTransaction fragmentTransaction) {

}

public void onTabSelected(ActionBar.Tab tab,
                          FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());

}

public void onTabReselected(ActionBar.Tab tab,
                            FragmentTransaction fragmentTransaction) {

}

@Override
public void onTabSelected(android.app.ActionBar.Tab tab, android.app.FragmentTransaction ft) {
    mViewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(android.app.ActionBar.Tab tab, android.app.FragmentTransaction ft) {

}

@Override
public void onTabReselected(android.app.ActionBar.Tab tab, android.app.FragmentTransaction ft) {

}

public class CollectionPagerAdapter extends FragmentPagerAdapter {

    //final int NUM_ITEMS = 11; // number of tabs

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int position)
    {

        switch (position) {
            case 0:
                return new Tab1Activity();
            case 1:
                return new Tab2Activity();
            case 2:
                return new Tab3Activity();
            case 3:
                return new Tab4Activity();
            case 4:
                return new Tab5Activity();
            case 5:
                return new Tab6Activity();
            case 6:
                return new Tab7Activity();
            case 7:
                return new Tab8Activity();
            case 8:
                return new Tab9Activity();
            case 9:
                return new Tab10Activity();
            case 10:
                return new Tab11Activity();
        }

        return null;
    }

    @Override
    public int getCount()
    {
        return 11;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position)
        {
            case 0:
                return getString(R.string.label1);
            case 1:
                return getString(R.string.label2);
            case 2:
                return getString(R.string.label3);
            case 3:
                return getString(R.string.label4);
            case 4:
                return getString(R.string.label5);
            case 5:
                return getString(R.string.label6);
            case 6:
                return getString(R.string.label7);
            case 7:
                return getString(R.string.label8);
            case 8:
                return getString(R.string.label9);
            case 9:
                return getString(R.string.label10);
            case 10:
                return getString(R.string.label11);
        }
        return null;
    }
}

@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;
}
@Override
 public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    int i = item.getItemId();
    if (i == R.id.actiorn) {
        Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
        MainActivity.this.startActivity(intent);
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }
}

这里我有11个标签,所以11个布局,但没有动作栏。 我只想要Navigation Drawer主页上有trasparent actionbar(我做了一个示意图,让你更好地理解)。 enter image description here

1 个答案:

答案 0 :(得分:1)

我认为你应该从documentation开始,了解如何在Android中创建导航抽屉。

或者你在实施它时遇到了一些问题?如果是,请提供详细信息。