注意头导航标签过多会被压缩并

21 FragmentTabHost +Fragment代码案例_xml

  • 结构
    21 FragmentTabHost +Fragment代码案例_控件_02

MainActivity.java

package com.qf.day21_fragmenttabhost_demo1;

import com.qf.day21_fragmenttabhost_demo1.fragment.MyFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.TabHost.TabSpec;

/**
* 步骤1:找出书签控件
* 步骤2:初始化化书签控件
* 步骤3:添加书签到书签控件
*
*
*/
public class MainActivity extends FragmentActivity {

//声明书签控件
private FragmentTabHost tabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//1找出书签控件
tabHost = (FragmentTabHost) findViewById(R.id.tabHost);

//初始化书签控件
/**
* 参数1:上下文
* 参数2:Fragment管理者对象
* 参数3:接收Fragment的容器
*/
tabHost.setup(MainActivity.this, getSupportFragmentManager(), R.id.layout_content_id);

//获取书签的名称
String[] arryTabs = getResources().getStringArray(R.array.arrTabs);

//向TabHost添加书签
for(int i=0;i<arryTabs.length;i++){

//创建书签 setIndicator设置内容
TabSpec tabSpec = tabHost.newTabSpec("tab"+i).setIndicator(arryTabs[i]);
Bundle bundle = new Bundle();
bundle.putInt("index", i+1);
/**
* 添加书签
*
* 参数1:书签
* 参数2:Fragment对象
* 参数3:是否向Fragment传值
*/
tabHost.addTab(tabSpec, MyFragment.class, bundle);

}


}



}


MyFragment.java

package com.qf.day21_fragmenttabhost_demo1.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.qf.day21_fragmenttabhost_demo1.R;

public class MyFragment extends ListFragment {

private TextView tvShow;

private int index =0;

@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);

Bundle bundle = getArguments();
if(bundle!=null){
index = bundle.getInt("index");
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_layout, container, false);

tvShow = (TextView) v.findViewById(R.id.tv_show);
return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
switch (index) {
case 1:
tvShow.setText("您点击了书签1");

break;
case 2:
tvShow.setText("您点击了书签2");
break;
case 3:
tvShow.setText("您点击了书签3");
break;

default:
break;
}

SimpleAdapter adapter = new SimpleAdapter(
getActivity(),
loadNetWorkData(),
R.layout.item,
new String[]{"icon","title","content"},
new int[]{R.id.iv_item,R.id.title_item,R.id.content_item});

setListAdapter(adapter);

}

/**
* 假设从网络获取数据
* @return
*/
private List<Map<String,Object>> loadNetWorkData(){

List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for(int i=0;i<20;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("icon", R.drawable.ic_launcher);
map.put("title", "郭XX大战曹XXX"+i+"tab"+index);
map.put("content", "降龙十八掌赢"+i+"tab"+index);
list.add(map);

}

return list;

}

@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}

@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
}

@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
}

@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
}

@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
}

}


activity_main.xml

<android.support.v4.app.FragmentTabHost 
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:id="@+id/tabHost"
tools:context=".MainActivity" >

<FrameLayout
android:id="@+id/layout_content_id"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</android.support.v4.app.FragmentTabHost>


fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00"
android:text="AAA"
/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>


</LinearLayout>