標籤: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