1 Star 0 Fork 0

型猿咚咚/Step by step to build an app

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Three Step 6.91 KB
一键复制 编辑 原始数据 按行查看 历史
型猿咚咚 提交于 2015-10-24 17:38 . new file
这一步是添加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;是否相同
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Android
1
https://gitee.com/xydd/Step-by-step-to-build-an-app.git
git@gitee.com:xydd/Step-by-step-to-build-an-app.git
xydd
Step-by-step-to-build-an-app
Step by step to build an app
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385