我使用片段制作了导航抽屉和操作栏标签,但NavigationDrawer
的内容与ActionBarTabs
内容重叠。如何克服此问题并像Google Play商店应用一样工作。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// create new tabs and and set up the titles of the tabs
ActionBar.Tab mFindTab = actionbar.newTab().setText(getString(R.string.ui_tabname_find));
ActionBar.Tab mChatTab = actionbar.newTab().setText(getString(R.string.ui_tabname_chat));
ActionBar.Tab mMeetTab = actionbar.newTab().setText(getString(R.string.ui_tabname_meet));
ActionBar.Tab mPartyTab = actionbar.newTab().setText(getString(R.string.ui_tabname_party));
// create the fragments
Fragment mFindFragment = new FindFragment();
Fragment mChatFragment = new ChatFragment();
Fragment mMeetFragment = new MeetFragment();
Fragment mPartyFragment = new PartyFragment();
// bind the fragments to the tabs - set up tabListeners for each tab
mFindTab.setTabListener(new MyTabsListener(mFindFragment,getApplicationContext()));
mChatTab.setTabListener(new MyTabsListener(mChatFragment,getApplicationContext()));
mMeetTab.setTabListener(new MyTabsListener(mMeetFragment,getApplicationContext()));
mPartyTab.setTabListener(new MyTabsListener(mPartyFragment,getApplicationContext()));
// add the tabs to the action bar
actionbar.addTab(mFindTab);
actionbar.addTab(mChatTab);
actionbar.addTab(mMeetTab);
actionbar.addTab(mPartyTab);
if (savedInstanceState != null)
{
Toast.makeText(getApplicationContext(),
"tab is " + savedInstanceState.getInt(TAB_KEY_INDEX, 0),
Toast.LENGTH_SHORT).show();
actionbar.setSelectedNavigationItem(savedInstanceState.getInt(
TAB_KEY_INDEX, 0));
}
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);
dataList.add(new DrawerItem("Notifications", R.drawable.ic_action_email));
dataList.add(new DrawerItem("Messages", R.drawable.ic_action_good));
dataList.add(new DrawerItem("Today's Task for You", R.drawable.ic_action_gamepad));
dataList.add(new DrawerItem("Milestone", R.drawable.ic_action_labels));
dataList.add(new DrawerItem("Your Working On", R.drawable.ic_action_search));
dataList.add(new DrawerItem("Calender", R.drawable.ic_action_cloud));
dataList.add(new DrawerItem("Leaderboard", R.drawable.ic_action_camera));
dataList.add(new DrawerItem("Your Score", R.drawable.ic_action_video));
dataList.add(new DrawerItem("Your Productivity", R.drawable.ic_action_group));
dataList.add(new DrawerItem("Your Badges",R.drawable.ic_action_import_export));
dataList.add(new DrawerItem("Your Skills Cloud", R.drawable.ic_action_about));
dataList.add(new DrawerItem("Your Strength", R.drawable.ic_action_settings));
dataList.add(new DrawerItem("Your Weakness", R.drawable.ic_action_help));
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,dataList);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer,
R.string.drawer_open,R.string.drawer_close)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
SelectItem(0);
}
}
class MyTabsListener implements ActionBar.TabListener
{
public Fragment fragment;
public Context context;
public MyTabsListener(Fragment fragment, Context context)
{
this.fragment = fragment;
this.context = context;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
Toast.makeText(context, "Reselected!", Toast.LENGTH_SHORT).show();
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
Toast.makeText(context, "Selected!", Toast.LENGTH_SHORT).show();
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
Toast.makeText(context, "Unselected!", Toast.LENGTH_SHORT).show();
ft.remove(fragment);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_gravity="center">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#ffff"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>