"Android Interface Implementation" uses Scrollingtabsview to achieve viewpager effects with sliding tabs

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

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.

<?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:

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; @Overrideprotected 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 sliding label adapter and host Viewpagerscrollingtabs = (Scrollingtabsview) Findviewbyid (R.id.scrolling_tabs); Scrollingtabs.setadapter (Scrollingtabsadapter); ScrollingTabs.setViewPager ( Viewpager);} /** * Viewpager Adapter * * @author Zhaokaiqiang * */private class Fragsadapter extends Fragmentstatepageradapter {private Ar Raylist<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));}} @Overridepublic int GetCount () {return fragments.size ();} @Overridepublic Fragment getItem (int position) {return fragments.get (position);}} /** * Sliding Label Adapter * * @author Zhaokaiqiang * */private class Scrollingtabsadapter implements Tabsadapter {private Activity M Context;public string[] Mtitles = {"Home","Recommended", "latest", "Entertainment", "Settings"};p ublic Scrollingtabsadapter (Activity ctx) {this.mcontext = CTX;} @Overridepublic 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:

<?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 Fixedtabsview, using the same method as this, in addition, Viewpager adapter replaced 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.