Reprinted from: http://blog.csdn.net/zhaokaiqiang1992/article/details/40378285
In the previous article, we used the Pagertabstrip in the support package to implement the Viewpager effect of sliding labels, and today we'll introduce another open source project to achieve similar results.
In this article, we will use the third-party open source project Viewpagerextensions implementation.
First look at the effect
Viewpagerextensions's github address: https://github.com/astuetz/ViewPagerExtensions
First give the directory structure of the entire project
In this demo, I directly put the resource files in the project, easy to use.
First, let's take a look at the layout file activity_main.xml.
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="Http://schemas.android.com/tools"
- xmlns:app="Http://schemas.android.com/com.heli17.tradeshowcloud"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Com.astuetz.viewpager.extensions.ScrollingTabsView
- android:id="@+id/scrolling_tabs"
- android:layout_width="fill_parent"
- android:layout_height="38DP"
- android:background="@drawable/tab_unselected_holo"
- app:dividerdrawable="@android: Color/white" />
- <Android.support.v4.view.ViewPager
- android:id="@+id/pager"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- </linearlayout>
In the layout file, we added a Scrollingtabsview control, which is the custom control for the indicator above.
Once the layout is written, we can set up the adapter in the code.
The code is as follows:
[Java]View Plaincopy
- Package Com.example.scrollingtabsdemo;
- Import java.util.ArrayList;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.support.v4.app.Fragment;
- Import android.support.v4.app.FragmentActivity;
- Import Android.support.v4.app.FragmentManager;
- Import Android.support.v4.app.FragmentStatePagerAdapter;
- Import Android.support.v4.view.ViewPager;
- Import Android.view.View;
- Import Android.widget.Button;
- Import Com.astuetz.viewpager.extensions.ScrollingTabsView;
- Import Com.astuetz.viewpager.extensions.TabsAdapter;
- Public class Mainactivity extends Fragmentactivity {
- private Viewpager Viewpager;
- private Scrollingtabsadapter Scrollingtabsadapter;
- private Scrollingtabsview Scrollingtabs;
- private Fragsadapter Pageradapter;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Viewpager = (Viewpager) Findviewbyid (R.id.pager);
- //Set adapter
- Pageradapter = New Fragsadapter (Getsupportfragmentmanager ());
- Viewpager.setadapter (Pageradapter);
- //Set the number of cache fragment
- Viewpager.setoffscreenpagelimit (2);
- Viewpager.setcurrentitem (0);
- Viewpager.setpagemargin (4);
- Scrollingtabsadapter = New Scrollingtabsadapter (this);
- //Set the adapter and host Viewpager for sliding labels
- Scrollingtabs = (Scrollingtabsview) Findviewbyid (r.id.scrolling_tabs);
- Scrollingtabs.setadapter (Scrollingtabsadapter);
- Scrollingtabs.setviewpager (Viewpager);
- }
- /**
- * Viewpager Adapter
- *
- * @author Zhaokaiqiang
- *
- */
- private class Fragsadapter extends Fragmentstatepageradapter {
- private arraylist<fragment> fragments;
- Public Fragsadapter (fragmentmanager FM) {
- super (FM);
- Fragments = new arraylist<fragment> ();
- For (int i = 0; i < scrollingTabsAdapter.mTitles.length; i++) {
- Fragments.add (new Myfragment (i));
- }
- }
- @Override
- public int GetCount () {
- return fragments.size ();
- }
- @Override
- Public Fragment GetItem (int position) {
- return Fragments.get (position);
- }
- }
- /**
- * Slide Label Adapter
- *
- * @author Zhaokaiqiang
- *
- */
- private class Scrollingtabsadapter implements Tabsadapter {
- private Activity Mcontext;
- Public string[] mtitles = { "home", "recommended", "latest", "entertainment", "settings"};
- Public Scrollingtabsadapter (Activity ctx) {
- this.mcontext = CTX;
- }
- @Override
- Public View GetView (int position) {
- button tab = (Button) mcontext.getlayoutinflater (). Inflate (
- R.layout.tab_scrolling, null);
- Tab.settext (Mtitles[position]);
- return tab;
- }
- }
- }
In the code, we need to set up two adapters, one is Viewpager, to replace the display of the fragment, and the other is the Sliding tab layout, to control the layout of each tab display, returned in the GetView method.
Here r.layout.tab_scrolling is a custom layout, the code is as follows:
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <button xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="0DP"
- android:layout_height="wrap_content"
- android:background="@drawable/tab_holo"
- android:gravity="center"
- android:paddingbottom="8DP"
- android:paddingleft="30dip"
- android:paddingright="30dip"
- android:paddingtop="8DP"
- android:textcolor="@android: Color/holo_blue_light"
- android:textsize="16sp" />
You can make changes according to your needs.
This sliding tab is suitable for many situations, so the choice of VIEWPAGR adapter is fragmentpagerstateadapter, if the sliding interface is below 3 or 3, it is recommended to use Fixedtabsview, Using the same method as this, in addition, Viewpager adapter for fragmentpageradapter more appropriate.
Demo GitHub address for this project: Https://github.com/ZhaoKaiQiang/ScrollingTabsDemo
"Android Interface Implementation" uses Scrollingtabsview to achieve viewpager effects with sliding tabs