22.Android 十分方便的滑动标签页
- Android 十分方便的滑动标签页
- 前言
- EasySlidingTabs属性
- EasySlidingTabs布局
- FragmentPagerAdapter
- EasySlidingTabs设置Tab背景
- Github传送门
- 效果图
前言
其实滑动标签页是很常见的,网上搜也是一大堆。但是好用、简单、少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传送门
效果图
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-27 21:59:12