日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用FragmentTabHost和ViewPager实现仿微信主界面侧滑

發布時間:2025/6/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用FragmentTabHost和ViewPager实现仿微信主界面侧滑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近看到很多界面主頁都差不多,決定研究研究寫出來,以后直接拿來用,不做代碼的輪子,多總結,多學習

還是廢話少說,先上圖


介紹一下我的代碼:

首先是布局文件:

[html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????android:orientation="vertical"?>??
  • ??
  • ?????<android.support.v4.view.ViewPager??
  • ????????android:id="@+id/pager"??
  • ????????android:layout_width="match_parent"??
  • ????????android:layout_height="0dp"??
  • ????????android:layout_weight="1"?/>??
  • ??
  • ????<android.support.v4.app.FragmentTabHost??
  • ????????android:id="@android:id/tabhost"??
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:background="@android:color/black"?>??
  • ??
  • ????????<FrameLayout??
  • ????????????android:id="@android:id/tabcontent"??
  • ????????????android:layout_width="0dp"??
  • ????????????android:layout_height="0dp"??
  • ????????????android:layout_weight="0"?/>??
  • ????</android.support.v4.app.FragmentTabHost>??
  • ??
  • </LinearLayout>??

  • 當然如果你想讓底部的tab放在上面的話,可以把viewPager和FragmentTabHost位置換一下,以上就是主界面了,比較簡單沒什么好說的,給個小技巧吧,shift+ctrl+T可以查找你想要的包,例如輸入viewPager,直接粘android.support.v4.view.ViewPager就可以了,比較方便

    下面就是MainActivity了:

    [java]?view plaincopy
  • package?com.sdufe.thea.framework;??
  • ??
  • import?java.util.ArrayList;??
  • import?java.util.List;??
  • import?java.util.zip.Inflater;??
  • ??
  • import?android.os.Bundle;??
  • import?android.R.integer;??
  • import?android.annotation.SuppressLint;??
  • import?android.app.Activity;??
  • import?android.support.v4.app.Fragment;??
  • import?android.support.v4.app.FragmentActivity;??
  • import?android.support.v4.app.FragmentTabHost;??
  • import?android.support.v4.view.ViewPager;??
  • import?android.support.v4.view.ViewPager.OnPageChangeListener;??
  • import?android.view.LayoutInflater;??
  • import?android.view.Menu;??
  • import?android.view.View;??
  • import?android.view.ViewGroup;??
  • import?android.widget.ImageView;??
  • import?android.widget.TabHost.OnTabChangeListener;??
  • import?android.widget.TabHost.TabSpec;??
  • import?android.widget.TabWidget;??
  • import?android.widget.TextView;??
  • ??
  • public?class?MainActivity?extends?FragmentActivity?implements??
  • ????????OnPageChangeListener,?OnTabChangeListener?{??
  • ??
  • ????private?FragmentTabHost?mTabHost;??
  • ????private?LayoutInflater?layoutInflater;??
  • ????private?Class?fragmentArray[]?=?{?Fragment1.class,?Fragment.class,??
  • ????????????Fragment3.class,?Fragment4.class?};??
  • ????private?int?imageViewArray[]?=?{?R.drawable.mywork,?R.drawable.mypatient,??
  • ????????????R.drawable.infusion,?R.drawable.personal?};??
  • ????private?String?textViewArray[]?=?{?"工作",?"病人",?"互動",?"個人中心"?};??
  • ????private?List<Fragment>?list?=?new?ArrayList<Fragment>();??
  • ????private?ViewPager?vp;??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main_tab_layout);??
  • ??
  • ????????initView();??
  • ????????initPage();??
  • ????}??
  • ??
  • ????/**?
  • ?????*?控件初始化?
  • ?????*/??
  • ????private?void?initView()?{??
  • ????????vp?=?(ViewPager)?findViewById(R.id.pager);??
  • ????????vp.setOnPageChangeListener(this);??
  • ????????layoutInflater?=?LayoutInflater.from(this);??
  • ????????mTabHost?=?(FragmentTabHost)?findViewById(android.R.id.tabhost);??
  • ????????mTabHost.setup(this,?getSupportFragmentManager(),?R.id.pager);??
  • ????????mTabHost.setOnTabChangedListener(this);??
  • ??
  • ????????int?count?=?textViewArray.length;??
  • ??
  • ????????for?(int?i?=?0;?i?<?count;?i++)?{??
  • ????????????TabSpec?tabSpec?=?mTabHost.newTabSpec(textViewArray[i])??
  • ????????????????????.setIndicator(getTabItemView(i));??
  • ????????????mTabHost.addTab(tabSpec,?fragmentArray[i],?null);??
  • ????????????mTabHost.setTag(i);??
  • ????????}??
  • ????}??
  • ??
  • ????/**?
  • ?????*?初始化Fragment?
  • ?????*/??
  • ????private?void?initPage()?{??
  • ????????Fragment1?fragment1?=?new?Fragment1();??
  • ????????Fragment2?fragment2?=?new?Fragment2();??
  • ????????Fragment3?fragment3?=?new?Fragment3();??
  • ????????Fragment4?fragment4?=?new?Fragment4();??
  • ????????list.add(fragment1);??
  • ????????list.add(fragment2);??
  • ????????list.add(fragment3);??
  • ????????list.add(fragment4);??
  • ????????vp.setAdapter(new?MyFragmentAdapter(getSupportFragmentManager(),?list));??
  • ????}??
  • ??
  • ????private?View?getTabItemView(int?i)?{??
  • ????????View?view?=?layoutInflater.inflate(R.layout.tab_content,?null);??
  • ????????ImageView?mImageView?=?(ImageView)?view??
  • ????????????????.findViewById(R.id.tab_imageview);??
  • ????????TextView?mTextView?=?(TextView)?view.findViewById(R.id.tab_textview);??
  • ????????mImageView.setBackgroundResource(imageViewArray[i]);??
  • ????????mTextView.setText(textViewArray[i]);??
  • ????????return?view;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onCreateOptionsMenu(Menu?menu)?{??
  • ????????//?Inflate?the?menu;?this?adds?items?to?the?action?bar?if?it?is?present.??
  • ????????getMenuInflater().inflate(R.menu.main,?menu);??
  • ????????return?true;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onPageScrollStateChanged(int?arg0)?{??
  • ??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onPageScrolled(int?arg0,?float?arg1,?int?arg2)?{??
  • ??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onPageSelected(int?arg0)?{??
  • ????????TabWidget?widget?=?mTabHost.getTabWidget();??
  • ????????int?oldFocusability?=?widget.getDescendantFocusability();??
  • ????????widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);??
  • ????????mTabHost.setCurrentTab(arg0);??
  • ????????widget.setDescendantFocusability(oldFocusability);??
  • ????????mTabHost.getTabWidget().getChildAt(arg0)???
  • ????????????????.setBackgroundResource(R.drawable.selector_tab_background);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?onTabChanged(String?tabId)?{??
  • ????????int?position?=?mTabHost.getCurrentTab();??
  • ????????vp.setCurrentItem(position);??
  • ????}??
  • ??
  • }??

  • 代碼相對來說比較簡單,下面的就不貼了


    源碼:https://github.com/zimoguo/FragmentTabHost-ViewPager

    總結

    以上是生活随笔為你收集整理的使用FragmentTabHost和ViewPager实现仿微信主界面侧滑的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。