标签:
开源代码viewpagerindicator里面没有实现tab下划线切换过程中的移动动画,都是很突兀的多个fragement之间的切换,导致用户体验略差,google了下相关问题,发现一片博文:
public class UnderlinePageIndicatorEx extends UnderlinePageIndicator{ public UnderlinePageIndicatorEx(Context context) { super(context, null); } public UnderlinePageIndicatorEx(Context context, AttributeSet attrs) { super(context, attrs, R.attr.vpiUnderlinePageIndicatorStyle); } public UnderlinePageIndicatorEx(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setViewPager(ViewPager viewPager) { if (mViewPager == viewPager) { return; } // if (mViewPager != null) { // //Clear us from the old pager. // mViewPager.setOnPageChangeListener(null); // } if (viewPager.getAdapter() == null) { throw new IllegalStateException("ViewPager does not have adapter instance."); } mViewPager = viewPager; // mViewPager.setOnPageChangeListener(this); invalidate(); post(new Runnable() { @Override public void run() { if (mFades) { post(mFadeRunnable); } } }); } }
/** * Draws a line for each page. The current page line is colored differently * than the unselected page lines. */
android:theme="@style/StyledIndicators",对应styles文件下面:
<!-- 下面带横线tab的style --> <style name="StyledIndicators" parent="@android:style/Theme.Light"> <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item> </style> <style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator"> <!-- <item name="android:background">@drawable/tab_indicator</item> --> <item name="android:background">#00000000</item> <item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item> <item name="android:textSize">14sp</item> <item name="android:textColor">@drawable/viewpager_title_textcolor</item> <item name="android:dividerPadding">8dp</item> <item name="android:showDividers">middle</item> <item name="android:paddingLeft">10dp</item> <item name="android:paddingRight">10dp</item> <item name="android:fadingEdge">horizontal</item> <item name="android:fadingEdgeLength">8dp</item> </style>
<item name="android:background">@drawable/tab_indicator</item>
对应的drawable中切换的xml代码:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" /> <item android:state_selected="false" android:state_pressed="true" android:drawable="@android:color/transparent" /> <item android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/base_tabpager_indicator_selected" /> <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/base_tabpager_indicator_selected" /> </selector>
<itemname="android:background">#00000000</item>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.soyoungboy.view.viewpagerindicator.TabPageIndicator android:id="@+id/indicator" android:layout_width="fill_parent" android:layout_height="45dp" android:background="@color/white" /> <com.soyoungboy.view.viewpagerindicator.UnderlinePageIndicatorEx android:id="@+id/underline_indicator" android:layout_width="fill_parent" android:layout_height="3dp" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
//1,声明组件 private UnderlinePageIndicatorEx mUnderlinePageIndicator; //2,代码实现部分 mUnderlinePageIndicator = (UnderlinePageIndicatorEx) findViewById(R.id.underline_indicator); mUnderlinePageIndicator.setViewPager(viewpager); mUnderlinePageIndicator.setFades(false); indicator.setOnPageChangeListener(mUnderlinePageIndicator);
【界面优化】使用viewpagerindicator添加下划线滑动动画
标签:
原文地址:http://www.cnblogs.com/androidsuperman/p/bd5832871bb60f25d1237b84fedb971f.html