您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android中怎么使用viewpager滑動(dòng)指示器”,在日常操作中,相信很多人在Android中怎么使用viewpager滑動(dòng)指示器問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android中怎么使用viewpager滑動(dòng)指示器”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
工程目錄:
1. MainActivity.java
public class MainActivity extends FragmentActivity { private ViewPagerFrameAdapter adapter; //適配器(標(biāo)題和內(nèi)容) private ViewPager mPager; private TabPageIndicator tabbPageIndicator; // private UnderlinePageIndicatorEx underlinePageIndicator; private int COUNT = 0; private List<String> titles = new ArrayList<String>(); //標(biāo)題 @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //標(biāo)題,這里自己造的假數(shù)據(jù) for (int j = 0; j < 3 ; j++){ titles.add( "第"+ j +"標(biāo)簽卡"); } mPager = (ViewPager)findViewById(R.id.pager); adapter = new ViewPagerFrameAdapter(getSupportFragmentManager(),titles); mPager.setAdapter(adapter); //得到指示器 tabbPageIndicator = (TabPageIndicator) findViewById(R.id.tab_indicator); tabbPageIndicator.setViewPager(mPager); //下標(biāo) underlinePageIndicator = (UnderlinePageIndicatorEx)findViewById(R.id.underline_indicator); underlinePageIndicator.setViewPager(mPager); underlinePageIndicator.setFades(false); //設(shè)置指示器 tabbPageIndicator.setOnPageChangeListener(underlinePageIndicator); } //adapter 沒(méi)啥好說(shuō)的 跟前面的一樣 private class ViewPagerFrameAdapter extends FragmentStatePagerAdapter{ private List<String> title; //頁(yè)面標(biāo)題頭部數(shù)據(jù) public ViewPagerFrameAdapter(FragmentManager fm, List<String> title) { super(fm); this.title = title; } //根據(jù)position 返回 fragment @Override public Fragment getItem(int position) { return ArrayFragment.newInstance(position); } //size @Override public int getCount() { return title.size(); } //頂部的title @Override public CharSequence getPageTitle(int position) { if (titles != null){ return titles.get(position).toString(); } return super.getPageTitle(position); } } }
UnderlinePageIndicatorEx.java
//繼承這個(gè)然后重寫 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); } //自身的 ViewPage傳遞過(guò)去 @Override public void setViewPager(ViewPager viewPager) { if (mViewPager == viewPager){ return; } // if (mViewPager != null) { // mViewPager.setOnPageChangeListener(null); // } if (viewPager.getAdapter() == null) { throw new IllegalStateException(" pager 沒(méi)有 加入 adapter"); } mViewPager = viewPager; // mViewPager.setOnPageChangeListener(this); invalidate(); post(new Runnable(){ @Override public void run() { if (mFades) { post(mFadeRunnable); } } }); } }
ArrayFragment.java
public class ArrayFragment extends Fragment{ private int position; public ArrayFragment(int position){ this.position = position; } // 返回 viwePager 下面滑動(dòng)的頁(yè)面 public static ArrayFragment newInstance(int position){ return new ArrayFragment(position); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onDestroy() { super.onDestroy(); } // 根據(jù)position 返回不同的view @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layoutaa, null); return view; } }
好了接下來(lái)是 布局xml了。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 標(biāo)題 和 標(biāo)題下面的 指示器 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="35dp" android:orientation="vertical" android:background="@drawable/main_header_background"> <com.viewpagerindicator.TabPageIndicator android:id="@+id/tab_indicator" android:layout_height="wrap_content" android:layout_width="fill_parent"/> <com.test.linepagerindictor.widget.UnderlinePageIndicatorEx android:id="@+id/underline_indicator" android:layout_height="3dip" android:layout_width="fill_parent" android:layout_alignParentBottom="true"/> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
接下來(lái)設(shè)置 滑動(dòng)指示器的style
<!-- 總 style 直接引用 這個(gè) 由 tab style 和 Indicator style 組成 --> <style name="StyledAllIndicators" parent="@android:style/Theme.Light"> <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item> <item name="vpiUnderlinePageIndicatorStyle">@style/CustomUnderlinePageIndicator</item> </style> <!-- tab的 style --> <style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator"> <item name="android:background">#00000000</item> <item name="android:textAppearance">@style/CustomTabPageIndicator_text</item> <item name="android:textColor">@color/selector_tab</item> <item name="android:textSize">20sp</item> <!-- 分割線,可有可無(wú)的 --> <item name="android:divider">@drawable/custom_tab_indicator_divider</item> <item name="android:dividerPadding">10dip</item> <item name="android:showDividers">middle</item> <item name="android:paddingTop">4dp</item> <item name="android:paddingLeft">8dp</item> <item name="android:paddingBottom">4dp</item> <item name="android:paddingRight">8dp</item> <item name="android:fadingEdge">horizontal</item> <item name="android:fadingEdgeLength">8dp</item> </style> <style name="CustomTabPageIndicator_text" parent="android:TextAppearance.Medium"> <item name="android:typeface">monospace</item> </style> <!-- 下標(biāo)的指示器的顏色 --> <style name="CustomUnderlinePageIndicator"> <item name="selectedColor">#EB5548</item> <item name="android:background">#00000000</item> <item name="fadeLength">1000</item> <item name="fadeDelay">1000</item> </style>
selector_tab.xml 這個(gè)是 選擇器 在style中引用的,是用來(lái)切換 被選中的背景色的
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:color="#91D228" /> <item android:state_focused="true" android:color="#91D228" /> <item android:color="#dddddd" /> </selector>
到此,關(guān)于“Android中怎么使用viewpager滑動(dòng)指示器”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。