How to Use tabhost in Fragment

Source: Internet
Author: User

Recently, I was working on a quasi-e-commerce app. because I used the fragment technology, I want to make the tabhost interface effect in a fragment. After searching for information, I found a solution. I 'd like to share it with you! (Do not spray for new users !)


The first control to be used is the control in 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>

This XML is the control found from support V4, And the custom dynamic scroll bar is displayed.

The code implementation is as follows:

Public class funfragment extends fragment implements extends {public funfragment () {// required empty public constructor} private fragmenttabhost mtabhost_fun; private string [] fun_tabs = new string [] {" ", "tag", "Follow"}; private imageview succeeded; private float lastoffset_fun; @ overridepublic 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. response); initactionbar (); // inittabfragment (); inittabhost (layout); return layout;} private void initactionbar () {fragmentmanager fm = getchildfragmentmanager (); fragmenttransaction Ft = FM. begintransaction (); actionfragment = new actionfragment (); ft. add (R. id. action_fun, actionfragment); actionfragment. setactionname ("fun"); ft. commit ();} // initialize fragmenttabhostpublic 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 = 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) ;}@ overridepublic void ontabchanged (string Tabid) {int position = mtabhost_fun.getcurrenttab (); // you can specify setscrollanimation (position) for the scrolling animation bar );} // set Private void setscrollanimation (INT position) {// obtain the screen width windowmanager mwindowmanager = (windowmanager) getactivity (). getsystemservice (context. window_service); display = mwindowmanager. getdefadisplay display (); @ suppresswarnings ("deprecation") int width = display. getwidth (); // get the offset int offset of the scroll bar = width/fun_tabs.length; // use the open-source project nineold to set the rolling animation objectanimator offloat = objectanimator. offloat (mimgscrollbar_fun, "translationx", lastoffset_fun, position * offset); offloat. setinterpolator (New decelerateinterpolator (); offloat. setduration (500 ). start (); // The Position of the previous offset lastoffset_fun = position * offset ;}}

Recording This is also a souvenir for learning android. I hope it will be helpful to my friends who have just learned android.

This article from the "android = phone + TV +..." blog, please be sure to keep this source http://wangwn.blog.51cto.com/8305590/1431390

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.