Actionbar moving tab indicator

2019-07-15 09:31发布

I'm using ActionBar of AppCompact with tabs and ViewPager.
How can I implement moving (with the fragment) tabs indicator like in Onavo app?
I tried to use PagerTitleStrip but didn't succeed.

enter image description here

1条回答
Luminary・发光体
2楼-- · 2019-07-15 09:48

There is a library called "PagerSlidingTabStrip" that does exactly what you want: https://github.com/astuetz/PagerSlidingTabStrip

Interactive paging indicator widget, compatible with the ViewPager from the Android Support Library.

Usage

Include the PagerSlidingTabStrip widget in your view. This should usually be placed adjacent to the ViewPager it represents.

<com.astuetz.viewpager.extensions.PagerSlidingTabStrip
   android:id="@+id/tabs"
   android:layout_width="match_parent"
    android:layout_height="48dip" />

In your onCreate method (or onCreateView for a fragment), bind the widget to the ViewPager.

 // Set the pager with an adapter
 ViewPager pager = (ViewPager) findViewById(R.id.pager);
 pager.setAdapter(new TestAdapter(getSupportFragmentManager()));

 // Bind the widget to the adapter
 PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
 tabs.setViewPager(pager);

(Optional) If you use an OnPageChangeListener with your view pager you should set it in the widget rather than on the pager directly.

 // continued from above
 tabs.setOnPageChangeListener(mPageChangeListener);
查看更多
登录 后发表回答