:
Https://github.com/asijack/ViewPage-Fragment-indicator-TabhostDemo
First look at the effect:
My previous article http://www.cnblogs.com/asijack/p/4239445.html is also the effect of this slide, but that is not the effect I want, I want this slide when the cursor with the effect of sliding, rather than the end of the slide before moving.
Demo needs to introduce a libraries:listsamples-library
It is important to note that the Android-support-v4.jar needs of the libraries and demo are consistent (in order to eliminate unnecessary hassles, it is recommended that the same jar package)
The code is not too much
Look at the layout first.
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"android:orientation= "vertical"Android:layout_height= "Fill_parent"> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextViewAndroid:id= "@+id/tv_guid1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1.0"Android:layout_margintop= "5DP"Android:layout_marginbottom= "5DP"android:gravity= "Center"Android:text= "Fragment1"android:textsize= "18SP" /> <TextViewAndroid:id= "@+id/tv_guid2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1.0"Android:layout_margintop= "5DP"Android:layout_marginbottom= "5DP"android:gravity= "Center"Android:text= "Fragment2"android:textsize= "18SP" /> <TextViewAndroid:id= "@+id/tv_guid3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1.0"Android:layout_margintop= "5DP"Android:layout_marginbottom= "5DP"android:gravity= "Center"Android:text= "Fragment3"android:textsize= "18SP" /> <TextViewAndroid:id= "@+id/tv_guid4"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1.0"Android:layout_margintop= "5DP"Android:layout_marginbottom= "5DP"android:gravity= "Center"Android:text= "Fragment4"android:textsize= "18SP" /> </LinearLayout> <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <Android.support.v4.view.ViewPagerAndroid:id= "@+id/pager"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" /> <Com.viewpagerindicator.UnderlinePageIndicatorAndroid:id= "@+id/indicator"Android:layout_height= "5DP"Android:layout_width= "Fill_parent"Android:layout_alignparenttop= "true" /> </Relativelayout></LinearLayout>
V4 Self-equipped indicator
< Android.support.v4.view.PagerTabStrip Android:layout_width = "Wrap_content" android:layout_height= "Wrap_content" android:layout_gravity= "Top" />
This.
This is a custom indicator.
Com.viewpagerindicator.UnderlinePageIndicator
implemented within the libraries.
The code inside the mainactivity is also relatively simple.
@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Initialize TextViewInittextview (); Madapter=NewMyfragmentadapter (Getsupportfragmentmanager ()); Mpager=(Viewpager) Findviewbyid (R.id.pager); Mpager.setadapter (Madapter); //indicatorUnderlinepageindicator indicator =(Underlinepageindicator) Findviewbyid (r.id.indicator); Indicator.setviewpager (Mpager); //set not to disappear at restIndicator.setfades (false); } Private voidInittextview () {//each fragment (view) corresponds to the TextViewview1=(TextView) Findviewbyid (R.ID.TV_GUID1); View2=(TextView) Findviewbyid (R.ID.TV_GUID2); VIEW3=(TextView) Findviewbyid (R.ID.TV_GUID3); View4=(TextView) Findviewbyid (R.ID.TV_GUID4); //Setting up monitoringView1.setonclicklistener (NewTxlistener (0)); View2.setonclicklistener (NewTxlistener (1)); View3.setonclicklistener (NewTxlistener (2)); View4.setonclicklistener (NewTxlistener (3)); } Public classTxlistenerImplementsview.onclicklistener{Private intIndex=0; PublicTxlistener (inti) {Index=i; } @Override Public voidOnClick (view view) {Mpager.setcurrentitem (index); } }
// indicator Underlinepageindicator indicator = (underlinepageindicator) Findviewbyid (r.id.indicator); Indicator.setviewpager (Mpager); // set not to disappear at rest Indicator.setfades (false);
Here you can set the animated effect of the indicator, the background or something. It can also be set in the XML layout.
The color of the synchronized cursor is set here in the Underlinepageindicator class.
Final int defaultselectedcolor = Res.getcolor (R.color.default_underline_indicator_selected_color);
This libraries also supports cursor style changes such as round, line, custom, etc.
Interested can look at this source code.
:
Https://github.com/asijack/ViewPage-Fragment-indicator-TabhostDemo
Viewpage+fragment+indicator+tabhost effect