如何在Fragment中使用tabhost

來源:互聯網
上載者:User

標籤:android   技術   fragment   資料   fragmenttabhost   

   最近在做一個仿電商的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;@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.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();}//初始化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 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();//設定滾動動畫條    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的朋友有點協助。

本文出自 “Android=phone+tv+....” 部落格,請務必保留此出處http://wangwn.blog.51cto.com/8305590/1431390

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.