Android應(yīng)用中怎么實現(xiàn)一個滑動切換頁面功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
創(chuàng)新互聯(lián)于2013年開始,先為河口等服務(wù)建站,河口等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為河口企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
TabLayou 主要實現(xiàn)的是標題頭的 滑動 這個 控件 類似于 ScrollView
XML中的布局
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <android.support.design.widget.TabLayout android:id="@+id/homefragment_tablelayouts" android:layout_width="0dp" android:layout_height="30dp" app:tabIndicatorHeight="2dp" android:layout_weight="1" app:tabIndicatorColor="@color/colorred" app:tabSelectedTextColor="#000000" app:tabMode="scrollable" > </android.support.design.widget.TabLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/add_channel_titlbar_new" /> </LinearLayout> <android.support.v4.view.ViewPager android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/homefragment_vp" ></android.support.v4.view.ViewPager>
以下是代碼的實現(xiàn)
//定義一個方法 public void getVpTitleData() { // 找到 TabLayou 控件 TabLayout mTabLayout= (TabLayout) getView().findViewById(R.id.homefragment_tablelayouts); // 找到 ViewPager 控件 ViewPager mViewPager= (ViewPager) getView().findViewById(R.id.homefragment_vp); //定義 title 集合 來存儲 解析的data數(shù)據(jù) listtitle = new ArrayList<>(); listtitle .add("頭條"); listtitle .add("社會"); listtitle .add("國內(nèi)"); listtitle .add("國際"); listtitle .add("明星"); listtitle .add("娛樂"); listtitle .add("軍事"); listtitle .add("財經(jīng)"); listtitle .add("時尚"); listtitle .add("八卦"); //遍歷 listtitle 集合 將title 添加經(jīng) TabLayou z中 for (int i = 0; i <listtitle.size() ; i++) { mTabLayout.addTab(mTabLayout.newTab().setText(listtitle.get(i))); } //創(chuàng)建集合 循環(huán)添加創(chuàng)建的Fragment listfragment = new ArrayList<>(); for (int i = 0; i <date.size() ; i++) { BeiJingFragment mjingFragment = new BeiJingFragment(); listfragment.add(mjingFragment); } HomeFragmentVPFragmentAdapter mAdapter = new HomeFragmentVPFragmentAdapter(getFragmentManager(),listfragment,listtitle); //給ViewPager設(shè)置適配器 mViewPager.setAdapter(mAdapter); //將TabLayout和ViewPager關(guān)聯(lián)起來。 mTabLayout.setupWithViewPager(mViewPager); //給TabLayout設(shè)置適配器 mTabLayout.setTabsFromPagerAdapter(mAdapter); }
ViewPafer與Fragment的適配器
public class HomeFragmentVPFragmentAdapter extends FragmentStatePagerAdapter { List<Fragment> listfragment; List<String> listtitle; public HomeFragmentVPFragmentAdapter(FragmentManager fm, List<Fragment> listfragment, List<String> listtitle) { super(fm); this.listfragment = listfragment; this.listtitle = listtitle; } @Override public CharSequence getPageTitle(int position) { return listtitle.get(position); } @Override public Fragment getItem(int position) { return listfragment.get(position); } @Override public int getCount() { return listfragment.size(); } }
看完上述內(nèi)容,你們掌握Android應(yīng)用中怎么實現(xiàn)一個滑動切換頁面功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享題目:Android應(yīng)用中怎么實現(xiàn)一個滑動切換頁面功能
文章位置:http://jinyejixie.com/article38/ppspsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、動態(tài)網(wǎng)站、建站公司、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)