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

Source: Internet
Author: User

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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="Http://schemas.android.com/tools"
  4. xmlns:app="Http://schemas.android.com/com.heli17.tradeshowcloud"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical" >
  8. <Com.astuetz.viewpager.extensions.ScrollingTabsView
  9. android:id="@+id/scrolling_tabs"
  10. android:layout_width="fill_parent"
  11. android:layout_height="38DP"
  12. android:background="@drawable/tab_unselected_holo"
  13. app:dividerdrawable="@android: Color/white" />
  14. <Android.support.v4.view.ViewPager
  15. android:id="@+id/pager"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent" />
  18. </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
  1. Package Com.example.scrollingtabsdemo;
  2. Import java.util.ArrayList;
  3. Import android.app.Activity;
  4. Import Android.os.Bundle;
  5. Import android.support.v4.app.Fragment;
  6. Import android.support.v4.app.FragmentActivity;
  7. Import Android.support.v4.app.FragmentManager;
  8. Import Android.support.v4.app.FragmentStatePagerAdapter;
  9. Import Android.support.v4.view.ViewPager;
  10. Import Android.view.View;
  11. Import Android.widget.Button;
  12. Import Com.astuetz.viewpager.extensions.ScrollingTabsView;
  13. Import Com.astuetz.viewpager.extensions.TabsAdapter;
  14. Public class Mainactivity extends Fragmentactivity {
  15. private Viewpager Viewpager;
  16. private Scrollingtabsadapter Scrollingtabsadapter;
  17. private Scrollingtabsview Scrollingtabs;
  18. private Fragsadapter Pageradapter;
  19. @Override
  20. protected void OnCreate (Bundle savedinstancestate) {
  21. super.oncreate (savedinstancestate);
  22. Setcontentview (R.layout.activity_main);
  23. Viewpager = (Viewpager) Findviewbyid (R.id.pager);
  24. //Set adapter
  25. Pageradapter = New Fragsadapter (Getsupportfragmentmanager ());
  26. Viewpager.setadapter (Pageradapter);
  27. //Set the number of cache fragment
  28. Viewpager.setoffscreenpagelimit (2);
  29. Viewpager.setcurrentitem (0);
  30. Viewpager.setpagemargin (4);
  31. Scrollingtabsadapter = New Scrollingtabsadapter (this);
  32. //Set the adapter and host Viewpager for sliding labels
  33. Scrollingtabs = (Scrollingtabsview) Findviewbyid (r.id.scrolling_tabs);
  34. Scrollingtabs.setadapter (Scrollingtabsadapter);
  35. Scrollingtabs.setviewpager (Viewpager);
  36. }
  37. /** 
  38. * Viewpager Adapter
  39. *
  40. * @author Zhaokaiqiang
  41. *
  42. */
  43. private class Fragsadapter extends Fragmentstatepageradapter {
  44. private arraylist<fragment> fragments;
  45. Public Fragsadapter (fragmentmanager FM) {
  46. super (FM);
  47. Fragments = new arraylist<fragment> ();
  48. For (int i = 0; i < scrollingTabsAdapter.mTitles.length; i++) {
  49. Fragments.add (new Myfragment (i));
  50. }
  51. }
  52. @Override
  53. public int GetCount () {
  54. return fragments.size ();
  55. }
  56. @Override
  57. Public Fragment GetItem (int position) {
  58. return Fragments.get (position);
  59. }
  60. }
  61. /** 
  62. * Slide Label Adapter
  63. *
  64. * @author Zhaokaiqiang
  65. *
  66. */
  67. private class Scrollingtabsadapter implements Tabsadapter {
  68. private Activity Mcontext;
  69. Public string[] mtitles = { "home", "recommended", "latest", "entertainment", "settings"};
  70. Public Scrollingtabsadapter (Activity ctx) {
  71. this.mcontext = CTX;
  72. }
  73. @Override
  74. Public View GetView (int position) {
  75. button tab = (Button) mcontext.getlayoutinflater (). Inflate (
  76. R.layout.tab_scrolling, null);
  77. Tab.settext (Mtitles[position]);
  78. return tab;
  79. }
  80. }
  81. }

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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <button xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="0DP"
  4. android:layout_height="wrap_content"
  5. android:background="@drawable/tab_holo"
  6. android:gravity="center"
  7. android:paddingbottom="8DP"
  8. android:paddingleft="30dip"
  9. android:paddingright="30dip"
  10. android:paddingtop="8DP"
  11. android:textcolor="@android: Color/holo_blue_light"
  12. 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

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.