package com.wangbb.naruto.app.activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.wangbb.naruto.R;
import com.wangbb.naruto.app.fragment.MainDrawerMenuFragment;
import com.wangbb.naruto.app.fragment.MainHomeFragment;
import com.wangbb.naruto.app.fragment.MainSettingFragment;
import com.wangbb.naruto.app.fragment.MainTempFragment;
public class MainActivity extends BaseFragmentActivity {
public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SWITCH_FRAGMENT:
attachFragment(String.valueOf(msg.obj));
break;
}
}
};
public static final int SWITCH_FRAGMENT = 0x1024;
public static final String HOME = "home";
public static final String TEMP = "temp";
public static final String SETTING = "setting";
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;
private MainDrawerMenuFragment menuFragment;
private MainHomeFragment homeFragment;
private MainTempFragment tempFragment;
private MainSettingFragment settingFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
initMenuView();
attachFragment(HOME);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (!(getVisibleFragment() instanceof MainHomeFragment)){
attachFragment(HOME);
return true;
}
return super.onKeyDown(keyCode, event);
}
private void initMenuView() {
if (menuFragment == null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
menuFragment = new MainDrawerMenuFragment();
ft.replace(R.id.drawerlayout_menu, menuFragment);
ft.commit();
}
}
private void attachFragment(String tabId) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (homeFragment != null) {
ft.hide(homeFragment);
}
if (tempFragment != null) {
ft.hide(tempFragment);
}
if (settingFragment != null) {
ft.hide(settingFragment);
}
switchFragment(ft, tabId);
ft.commit();
}
private void switchFragment(FragmentTransaction ft, String tab) {
if (tab.equals(HOME)) {
if (homeFragment == null) {
homeFragment = new MainHomeFragment();
ft.add(R.id.drawerlayout_content, homeFragment);
} else {
ft.show(homeFragment);
}
} else if (tab.equals(TEMP)) {
if (tempFragment == null) {
tempFragment = new MainTempFragment();
ft.add(R.id.drawerlayout_content, tempFragment);
} else {
ft.show(tempFragment);
}
} else if (tab.equals(SETTING)) {
if (settingFragment == null) {
settingFragment = new MainSettingFragment();
ft.add(R.id.drawerlayout_content, settingFragment);
} else {
ft.show(settingFragment);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
ShareActionProvider shareProvider = (ShareActionProvider) MenuItemCompat
.getActionProvider(menu.findItem(R.id.share));
shareProvider
.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
shareProvider.setShareIntent(getShareIntent());
return true;
}
private void initView() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setToolbar();
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true); //���÷��ؼ�����
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
// set a custom shadow that overlays the main content when the drawer opens
// drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawerlayout_open, R.string.drawerlayout_close) {
@Override
public void onDrawerClosed(View drawerView) {
toolbar.setTitle(R.string.drawerlayout_close);
}
@Override
public void onDrawerOpened(View drawerView) {
toolbar.setTitle(R.string.drawerlayout_open);
// toolbar.getMenu().removeItem(R.id.share);
}
};
toggle.syncState();
drawerLayout.setDrawerListener(toggle);
}
private void setListener() {
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.about:
startActivity(new Intent(MainActivity.this, AboutActivity.class));
break;
}
return false;
}
});
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawerLayout.isDrawerOpen(Gravity.START))
drawerLayout.closeDrawer(Gravity.START);
else
drawerLayout.openDrawer(Gravity.START);
}
});
}
public DrawerLayout getDrawerLayout() {
return drawerLayout;
}
private Intent getShareIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "����Naruto������Naruto");
intent.setType("text/plain");
Intent.createChooser(intent, "Share");
return intent;
}
private void setToolbar() {
// toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle("Naruto");
}
}