码迷,mamicode.com
首页 > 移动开发 > 详细

22.Android 十分方便的滑动标签页

时间:2015-10-18 23:16:35      阅读:483      评论:0      收藏:0      [点我收藏+]

标签:android   github   bug   标签   扩展   

22.Android 十分方便的滑动标签页


前言

其实滑动标签页是很常见的,网上搜也是一大堆。但是好用、简单、少bug、可扩展的库实在不多。很多想在做滑动标签页的时候也是经常想到各种不靠谱的库,虽然不难,但是容易坑自己。

原三星底层App大神JiangEcho提供技术支持


EasySlidingTabs属性

这些属性的名字都是很简单的英文,不难!

    <declare-styleable name="EasySlidingTabs">
        <attr name="easyIndicatorColor" format="color" />
        <attr name="easyUnderlineColor" format="color" />
        <attr name="easyTabTextColor" format="color" />
        <attr name="easySelectedTagTextColor" format="color" />
        <attr name="easyDividerColor" format="color" />
        <attr name="easyIndicatorHeight" format="dimension" />
        <attr name="easyUnderlineHeight" format="dimension" />
        <attr name="easyDividerPadding" format="dimension" />
        <attr name="easyTabPaddingLeftRight" format="dimension" />
        <attr name="easyTabBackground" format="reference" />
        <attr name="easyScrollOffset" format="dimension" />
        <attr name="easyShouldExpand" format="boolean" />
        <attr name="easyTextAllCaps" format="boolean" />
        <attr name="easyIndicatorDrawable" format="reference" />
    </declare-styleable>

EasySlidingTabs布局

真的十分简单 > . < 。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.camnter.easyslidingtabs.widget.EasySlidingTabs
        android:id="@+id/easy_sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:easyIndicatorColor="#ff57C1CC"
        app:easyUnderlineColor="#ffdddddd"
        app:easyTabTextColor="#ffFF4081"
        app:easySelectedTagTextColor="#ff57C1CC"
        android:paddingBottom="16dp"
        android:paddingTop="16dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/easy_vp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

FragmentPagerAdapter

public class TabsFragmentAdapter extends FragmentPagerAdapter implements EasySlidingTabs.TabsTitleInterface {

    private String[] titles;
    private List<Fragment> fragments;
    public TabsFragmentAdapter(FragmentManager fm, String[] titles, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
        this.titles = titles;
    }

    @Override
    public SpannableString getTabTitle(int position) {
        CharSequence title = this.getPageTitle(position);
        if (TextUtils.isEmpty(title)) return new SpannableString("");
        SpannableString spannableString = new SpannableString(title);
        return spannableString;
    }

    /**
     * This method may be called by the ViewPager to obtain a title string
     * to describe the specified page. This method may return null
     * indicating no title for this page. The default implementation returns
     * null.
     *
     * @param position The position of the title requested
     * @return A title for the requested page
     */
    @Override
    public CharSequence getPageTitle(int position) {
        if (position < titles.length) {
            return titles[position];
        } else {
            return "";
        }
    }

    /**
     * Return the Fragment associated with a specified position.
     *
     * @param position
     */
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = this.fragments.get(position);
        if (fragment != null) {
            return this.fragments.get(position);
        } else {
            return null;
        }
    }

    @Override
    public int getTabDrawableBottom(int position) {
        return 0;
    }

    @Override
    public int getTabDrawableLeft(int position) {
        return 0;
    }

    @Override
    public int getTabDrawableRight(int position) {
        return 0;
    }

    @Override
    public int getTabDrawableTop(int position) {
        return 0;
    }


    /**
     * Return the number of views available.
     */
    @Override
    public int getCount() {
        return this.fragments.size();
    }

}

EasySlidingTabs设置Tab背景

easyslidingtabs模块下的bg_easy_sliding_tabs.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">
    <item android:state_pressed="true" android:drawable="@color/bg_easy_sliding_tabs_pressed" />
    <item android:state_focused="true" android:drawable="@color/bg_easy_sliding_tabs_pressed" />
    <item android:drawable="@android:color/transparent" />
</selector>

在这修改背景色!!


Github传送门

EasySlidingTabs


效果图

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

22.Android 十分方便的滑动标签页

标签:android   github   bug   标签   扩展   

原文地址:http://blog.csdn.net/qq_16430735/article/details/49229189

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!