代码拉取完成,页面将自动刷新
这一步是添加FragmentTabHost的,大家可以在网上搜一下FragmentTabHost的用法,在这里添加FragmentTabHost,Tab是在MainTab.java枚举出来的,
app / ui / MainTab.java,
app / widget / MyFragmentTabHost.java
MainActivity应该是这样:
第三讲
import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import com.xiaozhong.myapplication.smalfragment.NavigationDrawerFragment;
public class MainActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks,
OnTabChangeListener, View.OnClickListener,
OnTouchListener {
//定义FragmentTabHost对象
private MyFragmentTabHost mTabHost;
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
public NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
AppManager.getAppManager().addActivity(this);
}
/**
* 初始化组件
*/
public void initView() {
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
//实例化TabHost对象,得到TabHost
mTabHost = (MyFragmentTabHost)findViewById(R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
initTabs();
mTabHost.setCurrentTab(0);
mTabHost.setOnTabChangedListener(this);
}
/**
* 初始化TabHost
*/
private void initTabs() {
//定义数组
MainTab[] tabs = MainTab.values();
final int size = tabs.length;
for (int i = 0; i < size; i++) {
MainTab mainTab = tabs[i];
TabHost.TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));
// 实例化布局对象 为每一个Tab按钮设置图标、文字和内容
View indicator = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.tab_indicator, null);
TextView title = (TextView) indicator.findViewById(R.id.tab_title);
Drawable drawable = this.getResources().getDrawable(
mainTab.getResIcon());
title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
null);
if (i == 2) {
indicator.setVisibility(View.INVISIBLE);
mTabHost.setNoTabChangedTag(getString(mainTab.getResName()));
}
title.setText(getString(mainTab.getResName()));
tab.setIndicator(indicator);
tab.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
return new View(MainActivity.this);
}
});
//将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tab, mainTab.getClz(), null);
//设置Tab按钮的背景
mTabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void initData() {
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_menu, menu);
if (!mNavigationDrawerFragment.isDrawerOpen()) {
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void onTabChanged(String tabId) {
final int size = mTabHost.getTabWidget().getTabCount();
for (int i = 0; i < size; i++) {
View v = mTabHost.getTabWidget().getChildAt(i);
if (i == mTabHost.getCurrentTab()) {
v.setSelected(true);
} else {
v.setSelected(false);
}
}
supportInvalidateOptionsMenu();
}
@Override
public void onClick(View v) {
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
super.onTouchEvent(event);
boolean consumed = false;
// use getTabHost().getCurrentTabView to decide if the current tab is
// touched again
if (event.getAction() == MotionEvent.ACTION_DOWN
&& v.equals(mTabHost.getCurrentTabView())) {
// use getTabHost().getCurrentView() to get a handle to the view
// which is displayed in the tab - and to get this views context
Fragment currentFragment = getCurrentFragment();
if (currentFragment != null
&& currentFragment instanceof OnTabReselectListener) {
OnTabReselectListener listener = (OnTabReselectListener) currentFragment;
listener.onTabReselect();
consumed = true;
}
}
return consumed;
}
private Fragment getCurrentFragment() {
return getSupportFragmentManager().findFragmentByTag(
mTabHost.getCurrentTabTag());
}
}
cannot be cast to android.support.v4.app.Fragment: 你要么统一Fragament ,将所有Fragment加入一个list然后去get取出来,
要么Activity必须继承FragmentActivity。所以当出现这个错误的时候要查看import android.support.v4.app.Fragment;是否相同
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。