21 FragmentTabHost +Fragment代码案例
来源:互联网 发布:洛阳青峰网络工资待遇 编辑:程序博客网 时间:2024/06/05 14:57
注意头导航标签过多会被压缩并
- 结构
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>
1 0
- 21 FragmentTabHost +Fragment代码案例
- Fragment+FragmentTabHost
- Android Fragment FragmentTabHost问题
- FragmentTabHost and Fragment 用法
- Android--FragmentTabHost+Fragment
- FragmentTabHost+Fragment的使用
- FragmentTabHost+Fragment实现底部菜单栏
- Fragment+FragmentTabHost实现底部菜单栏
- 使用FragmentTabHost+Fragment打造布局
- 【Android实战】SlideMenu+Fragment+FragmentTabHost
- 底部导航栏:FragmentTabHost+Fragment
- Fragment+FragmentTabHost实现底部菜单栏
- FragmentTabHost+Fragment实现底部菜单栏
- FragmentTabHost+fragment中获得fragment的对象
- FragmentTabHost添加Fragment后获取Fragment对象
- 重写FragmentTabHost,防止FragmentTabHost切换fragment重新调用onCreateView()
- Fragment案例
- drawerNavigation被fragment overlay, FragmentTabHost使用
- (三)2.字符型设备的操作open、close、read、write
- Java class文件的JDK编译版本号
- JS实现选项卡效果
- 第三周项目4-顺序表应用(1)
- 编译课程实验-识别浮点常量问题
- 21 FragmentTabHost +Fragment代码案例
- Graphics Settings
- 工作相关
- java定时器用法
- ViewPager实现各个选项卡之间的切换
- The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder i
- 括号平衡和正确嵌套
- QQ聊天界面的布局和设计(IOS篇)-第二季
- eclipse中的Console 脱离主窗口解决办法