Use FragmentTabHost and ViewPager to slide the main interface and view pagertabhost
Recently, I have seen a lot of page pages that are similar. I decided to study and write them out, and I will use them directly later. I will not use the code on the wheel. I will summarize and learn more.
Let's talk nonsense. First
Introduce my code:
First, layout files:
<?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>
Of course, if you want to put the tab at the bottom on the top, you can replace viewPager and FragmentTabHost. The above is the main interface. It's easy to say. Please give me some tips, shift + ctrl + T you can find the desired package, such as entering viewPager and sticking android directly. support. v4.view. viewPager is enough for convenience.
Below is the MainActivity:
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 extends, 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 [] = {"work", "patient", "interaction", "personal Center "}; private List <Fragment> list = new ArrayList <Fragment> (); private ViewPager vp; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main_tab_layout); initView (); initPage ();}/*** control initialization */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) ;}}/*** initialize 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 (getsuppfrfragmentmanager (), 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 ;}@ Overridepublic 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 ;}@ Overridepublic void onPageScrollStateChanged (int arg0) {}@ Overridepublic void onPageScrolled (int arg0, float arg1, int arg2) {}@ Overridepublic void onPageSelected (int arg0) {TabWidget = 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);} @ Overridepublic void onTabChanged (String tabId) {int position = mTabHost. getCurrentTab (); vp. setCurrentItem (position );}}
The code is relatively simple.
Source code: https://github.com/zimoguo/FragmentTabHost-ViewPager
Combination of fragment and viewpager in android
If viewpager is used only, the page will be reloaded every time, and the speed will be relatively slow (it may not be visible when there is little content, such as an image). After fragment is loaded once, the data will be cached, the second time, I felt smoother, but it was also based on memory consumption, that is, I exchanged space for time, just like the viewholer Optimization Technology of listview. In addition, fragment is more advantageous than viewpager in navigation bar. So the combination of fragment and viewpager can be said to complement each other, but the memory consumption is more and the code is more complicated.
In Fragment, viewPager is used to embed three subFragment instances.
The data is being saved, and the adapter is created in the onAttach event. However, the view on the interface is rebuilt once. I have no data operation methods in onresume and onStart. Is empty. I load the onViewCreated event or restore it to the view.