如何在Fragment中使用tabhost

最近在做一个仿电商的APP,由于前面使用了Fragment技术,现在想要在一个Fragment中做出TabHost的界面效果,经过查找资料找到了解决办法,特分享出来!(新人勿喷!)

首先要使用的控件是Support V4里面的控件,XML如图

<android.support.v4.app.FragmentTabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/action_fun" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" >
            </TabWidget>
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            android:layout_weight="1" >

            <ImageView
            android:id="@+id/img_scrollbar_fun"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_weight="2"
            android:scaleType="fitXY"
            android:src="@drawable/scrollbar" />

            <View
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1" />
            </LinearLayout>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />

            <FrameLayout
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

这个XML是从Support V4中找的控件,多出来的是自定义的动态滚动条。

下面是具体的代码实现,如下

public class FUNFragment extends Fragment implements  OnTabChangeListener {

	public FUNFragment() {
		// Required empty public constructor
	}
	private FragmentTabHost mTabHost_fun;
	private String[] fun_tabs = new String[] { "推荐", "标签","关注"};
	private ImageView mImgScrollbar_fun;
	private float lastoffset_fun;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View layout = inflater.inflate(R.layout.fragment_fun, container, false);
		mImgScrollbar_fun = (ImageView) layout.findViewById(R.id.img_scrollbar_fun);
		initActionBar();
//		initTabFragment();
		initTabHost(layout);
		return layout;
	}

	private void initActionBar() {
		FragmentManager fm = getChildFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		ActionFragment actionFragment = new ActionFragment();
		ft.add(R.id.action_fun, actionFragment);
		actionFragment.setActionName("FUN");
		ft.commit();
	}
	//初始化FragmentTabHost
	public void initTabHost(View layout) {
		mTabHost_fun = (FragmentTabHost)layout.findViewById(android.R.id.tabhost);
		mTabHost_fun.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
		mTabHost_fun.setOnTabChangedListener(this);
		for (int i = 0; i < fun_tabs.length; i++) {
			View view = getActivity().getLayoutInflater().inflate(
					R.layout.fun_tabhost_item, null);
			TextView mTextView = (TextView) view.findViewById(R.id.tv_fun_tab);
			mTextView.setText(fun_tabs[i]);
			mTabHost_fun.addTab(mTabHost_fun.newTabSpec("tag" + i).setIndicator(view),
	                RecFragment.class, null);
            
		}
	}

	@Override
	public void onTabChanged(String tabId) {
		int position = mTabHost_fun.getCurrentTab();
		//设置滚动动画条
	    setScrollAnimation(position);
	}
	//设置
	private void setScrollAnimation(int position) {
		//获取屏幕的宽度
		WindowManager mWindowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
		Display display = mWindowManager.getDefaultDisplay();
		@SuppressWarnings("deprecation")
		int width = display.getWidth();
		//获取滚动条的偏移量
		int offset = width/fun_tabs.length;
		//使用开源项目nineold设置滚动动画
		ObjectAnimator ofFloat = ObjectAnimator.ofFloat(mImgScrollbar_fun, "translationX", lastoffset_fun, position*offset);

		ofFloat.setInterpolator(new DecelerateInterpolator());
		ofFloat.setDuration(500).start();
		//前一次偏移的位置
		lastoffset_fun = position*offset;
	}

}

将此记录下来,也是为自己学习Android做个留念。希望对刚学习Android的朋友有点帮助。

如何在Fragment中使用tabhost,布布扣,bubuko.com

时间: 2024-08-08 13:57:37

如何在Fragment中使用tabhost的相关文章

安卓开发_WebView如何在Fragment中使用

之前学习了如何在activity中使用WebView控件来显示网页. 在我的实际开发中,有需要在Fragment中用到WebView控件的,那么就百度学习了一下 其实很简单,但是当然不是和在Activity中使用的方法一样 具体看代码 1 package com.example.qunxiong; 2 3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 import android.view.Layout

如何在Fragment中获取FragmentActivity中的布局????

============问题描述============ 我是想在Fragment中往FragmentActivity里面的布局里添加控件,现在我怎么在Fragment中得到这个布局呢?? ============解决方案1============ 一种思路是定义一个接口,如 OnDecoratorSelectListener,然后Activity实现这个接口,当Fragment里面选择了一个脸型的时候,调用((OnDecoratorSelectListener)getActivity).onD

如何在Fragment中获取context

文章转载自http://blog.csdn.net/demonliuhui/article/details/51511136 这里仅供自己学习参考: Context,中文直译为"上下文",SDK中对其说明如下: 1.它描述的是一个应用程序环境的信息,即上下文. 2.该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类). 3.通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作,例如:启动一个Acti

如何在Fragment中使用findViewById呢?

版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp

使用Fragment实现类似TabHost标签栏的效果

在前几天,我写了篇<Android TabHost的使用>简单的介绍了其使用的方法,但是在实现的时候发现TabHost已经被官方遗弃了.虽然我觉得TabHost还是多好用的(可能因为我这种菜鸟不清楚吧)不知道为什么官方会遗弃,但应该还是有原因的吧. 首先,我们来简单的了解一下Fragment.Fragment是Android在3.0中引入的概念用来在一个Activity中描述一些行为或一部分用户界面,支持不同分辨率屏幕的动态和灵活的UI设计.Fragment必须总是被嵌入到一个Activity

Android应用开发:LoaderManager在Activity/Fragment中的使用分析

LoaderManager 外部接口initLoader:起始 public <D> Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) { if (mCreatingLoader) { throw new IllegalStateException("Called while creating a loader"); } Loade

如何在android中利用Theme设置application 的所有listview的style?~

今天看VLC的源代码,发现一个很神奇的东西 所有listview的点击效果背景色都是橘黄色 花了点时间找了一下看看怎么实现的. 首先,定义一个<selector> like this: <selector> <item android:state_pressed="true"> <shape android:shape="rectangle"> <solid android:color="@color/

如何在makfile中查看变量的值

在makefile中查看变量的取值是多少应该是一个比较麻烦的问题,但是本大神自己研究出一个十分方便的方法.这个方法十分简单.现在介绍如下 如果在一个十分复杂庞大的makefile文件中,有个地方用到一个变量SRC_FILE,你很想知道makefile运行到此处的时候这个变量的值为多少.那么你可以在这个变量的下面写两行东东: $(shell echo $(SRC_FILE) > readme.txt ) rrrrrrrrrrrrrrrrrrr 第一行其实是调用shell命令来将这个变量的值输入到r

如何在Angular2-cli中使用插件(不使用配置)

重要点: 要把插件放在src/assets文件中 如何在angular-cli中使用jquery插件 a.在index.html中引入 <script  src="./assets/jquery-1.8.3.min.js"></script> b. 在要使用jquery的组件中声明(XX.component.ts) declare var $ : any; c.导入onInit import { OnInit} from '@angular/core'; d.输