Today you need to do a function, tab switching, but not to viewpager like sliding, only through the Tab tab at the top, is similar to the Actionbar tab-like switch.
Then I went to find examples, in the Apidemos fragmenttabs (extends Activity), fragmenttabsfragment (extends Fragment), two examples, specifically said Fragmenttabsfragment, this class of Tabmanager is to rewrite the fragmenttabhost, their own implementation of the State to save and so on, it is worth reference, of course, this class also refer to the implementation of Fragmenttabhost.
In Support4demos, there are fragmenttabs (extends fragmentactivity), Fragmenttabsfragmentsupport (extends Fragment);
My main reference is Fragmenttabsfragmentsupport, because I want to use fragment to achieve my needs.
A few problems encountered:
The top tab style of the 1.FragmentTabHost is systematic and does not meet my requirements, so how do I customize this style?
You must be using this:
Mtabhost.addtab (Mtabhost.newtabspec ("simple"). Setindicator ("simple"), arraylistfragment.class, NULL);
If you want to change the tab style, you can do this:
Button simple = New button (getactivity ());
Simple.settext ("xxx");
Simple.settextcolor (This.getresources (). GetColor (R.color.green));
Simple.setbackgroundcolor (r.color.indicate);
Simple.setbackgroundresource (r.drawable.ic_star_p);
Set padding
Simple.setpadding (150, 150, 0, 0);
Set margin
Linearlayout.layoutparams LP = new Linearlayout.layoutparams (
0, LinearLayout.LayoutParams.MATCH_PARENT, 1); 1 is an optional write.
Lp.setmargins (150, 50, 0, 50);
SIMPLE.SETLAYOUTPARAMS (LP);
See, you can set padding, margin, background, etc...
Then-"Setindicator", see, there is a way to support custom view, so we can customize a view, for example, the above-defined button simple to pass in it.
You can also set the height and background of the Tabwidget:
Mtabhost.gettabwidget (). Setbackgroundresource (r.drawable.ic_new_tab_p);
Mtabhost.gettabwidget (). Setminimumheight (300);
Mtabhost.gettabwidget (). setdividerdrawable (NULL);
2. Multiple tab switch, each time will be new execution: Oncreateview,ondestroyview, resulting in such as the ListView can not save the browse location?
That's how I solved it:
if (Rootview = = null) {
Rootview = Inflater.inflate (R.layout.fragment_pager_list, container, false);
}
ViewGroup parent = (ViewGroup) rootview.getparent ();
if (parent! = NULL) {
Parent.removeview (Rootview);
}
View TV = Rootview.findviewbyid (R.id.text);
((TEXTVIEW) TV). SetText ("Fragment #" + mnum);
return rootview;
Test code:
http://download.csdn.net/detail/song_shi_chao/7168045
Reference:
Http://www.cnblogs.com/asion/archive/2013/09/25/3339313.html
Http://ar.newsmth.net/thread-ffd9fb821607d1.html
Http://www.eoeandroid.com/thread-322096-1-1.html
Http://www.byywee.com/page/M0/S910/910755.html
http://blog.csdn.net/renpengben/article/details/12615487 (I used this method)
Http://www.2cto.com/kf/201309/242225.html
Http://www.eoeandroid.com/thread-153696-1-1.html (How to fix Viewpager with the sub-view sliding conflict )
Android Fragment fragmenttabhost Issues