标签:
FragmentTabHost+Fragment
官网的API dome
Special TabHost that allows the use of Fragment objects
for its tab content. When placing this in a view hierarchy, after inflating the hierarchy you must call setup(Context,
FragmentManager, int) to complete the initialization of the tab host.
import com.example.android.supportv4.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
/**
* This demonstrates how you can implement switching between the tabs of a
* TabHost through fragments, using FragmentTabHost.
*/
public class FragmentTabs extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
LoaderCustomSupport.AppListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
}
}import com.example.android.supportv4.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTabsFragmentSupport extends Fragment {
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
FragmentStackSupport.CountingFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
LoaderCustomSupport.AppListFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
return mTabHost;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}常用的方法| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
||||||||||
|
|
|
||||||||||
|
|
Register a callback to be invoked when the selected state of any of the items in this list changes
|
||||||||||
|
|
This method is deprecated. Don‘t call the original TabHost setup, you must instead call
setup(Context,
FragmentManager) or setup(Context,
FragmentManager, int). |
||||||||||
|
|
|
||||||||||
|
|
|
||||||||||
main_tab_explore:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/widget_bar_explore_over" android:state_selected="true"></item>
<item android:drawable="@drawable/widget_bar_explore_nor" ></item>
</selector>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.skyfin.oschina.MainActivity" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<com.skyfin.oschina.MyFragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/Fragment_tab_bg">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</com.skyfin.oschina.MyFragmentTabHost>
</LinearLayout>
package com.skyfin.oschina;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
public class MainActivity extends FragmentActivity {
String old_tag;
private MyFragmentTabHost mTabHost;
private LayoutInflater layoutInflater;
private View fragmentQuickoption;
private Class fragmentArray[] = {FragmentNews.class,
FragmentTweet.class,FragmentQuickoption.class,
FragmentExplore.class,FragmentMe.class};
private int mImageArray[] = {R.drawable.main_tab_news,
R.drawable.main_tab_tweet,R.drawable.main_tab_quickoption,
R.drawable.main_tab_explore,R.drawable.main_tab_me};
private String mTextArray[]={
"综合","动弹","添加","发现","我"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
// 实例化布局对象
layoutInflater = LayoutInflater.from(this);
// 实例化TabHost对象,得到TabHost
mTabHost = (MyFragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
// 得到fragment的个数
int count = fragmentArray.length;
for (int i = 0; i < count; i++) {
//View indicator = LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab_item_btn_view, null);
// 为每一个Tab按钮设置图标、文字和内容
TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
.setIndicator(getTabItemView(i));
// 将Tab按钮添加进Tab选项卡中
if(i == 2)
{
//indicator.setVisibility(View.INVISIBLE);
mTabHost.setNoTabChangedTag(mTextArray[i]);
}
//tabSpec.setIndicator(indicator);
/*tabSpec.setContent(new TabContentFactory(){
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return null;
}
});*/
mTabHost.addTab(tabSpec, fragmentArray[i], null);
// 设置Tab按钮的背景
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.color.Fragment_tab_bg);
//去掉竖线
mTabHost.getTabWidget().setDividerDrawable(null);
}
//设置按钮的监听
mTabHost.setOnTabChangedListener(new OnTabChangedListener());
}
/**
* 按钮的监听的类
* @author Skyfin
*
*/
class OnTabChangedListener implements OnTabChangeListener {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
//Toast.makeText(getApplication(), tabId,Toast.LENGTH_SHORT).show();
if (old_tag != tabId) {
Toast.makeText(getApplicationContext(), tabId, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "添加", Toast.LENGTH_SHORT).show();
}
old_tag = tabId;
}
}
/**
* 给Tab按钮设置图标和文字
*/
private View getTabItemView(int index) {
View view = layoutInflater.inflate(R.layout.tab_item_view, null);
if(index != 2){
ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImageArray[index]);
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(mTextArray[index]);
}else {
view = layoutInflater.inflate(R.layout.tab_item_btn_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImageArray[index]);
}
return view;
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.skyfin.oschina;
import android.content.Context;
import android.support.v4.app.FragmentTabHost;
import android.util.AttributeSet;
public class MyFragmentTabHost extends FragmentTabHost {
private String mCurrentTag;
private String mNoTabChangedTag;
public MyFragmentTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onTabChanged(String tag) {
if (tag.equals(mNoTabChangedTag)) {
setCurrentTabByTag(mCurrentTag);
} else {
super.onTabChanged(tag);
mCurrentTag = tag;
}
}
public void setNoTabChangedTag(String tag) {
this.mNoTabChangedTag = tag;
}
}
Fragment:package com.skyfin.oschina;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentNews extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_news, container, false);
}
}
代码下载:http://download.csdn.net/detail/u012938203/8989689
版权声明:本文为博主原创文章,未经博主允许不得转载。
仿oschina 主界面的实现(一) -------FragmentTabHost+Fragment
标签:
原文地址:http://blog.csdn.net/u012938203/article/details/47414173