gpt4 book ai didi

java - 如何在选项卡菜单中使用抽屉导航

转载 作者:太空宇宙 更新时间:2023-11-04 15:10:05 25 4
gpt4 key购买 nike

我一直在开发一个应用程序,它在底部和其中一个页面有可滑动菜单,应该有一个抽屉导航来更改自己的 fragment 。

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;
}
}

应该包含抽屉导航的 Fragment 是 Resturants ,抽屉的 mainactivity 也是使用 Sherlock Library(SherlockFragmentActivity) 实现的。我很困惑如何从 SherlockActivity 调用 SherlockFragmentActivity :)

谢谢

最佳答案

本教程应该可以帮助您:

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

这是切换fragment的代码 fragment

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

关于java - 如何在选项卡菜单中使用抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21442054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com