Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)滑動(dòng)切換頁(yè)面功能?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
創(chuàng)新互聯(lián)于2013年開(kāi)始,先為河口等服務(wù)建站,河口等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為河口企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
TabLayou 主要實(shí)現(xiàn)的是標(biāo)題頭的 滑動(dòng) 這個(gè) 控件 類似于 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>
以下是代碼的實(shí)現(xiàn)
//定義一個(gè)方法 public void getVpTitleData() { // 找到 TabLayou 控件 TabLayout mTabLayout= (TabLayout) getView().findViewById(R.id.homefragment_tablelayouts); // 找到 ViewPager 控件 ViewPager mViewPager= (ViewPager) getView().findViewById(R.id.homefragment_vp); //定義 title 集合 來(lái)存儲(chǔ) 解析的data數(shù)據(jù) listtitle = new ArrayList<>(); listtitle .add("頭條"); listtitle .add("社會(huì)"); listtitle .add("國(guó)內(nèi)"); listtitle .add("國(guó)際"); listtitle .add("明星"); listtitle .add("娛樂(lè)"); listtitle .add("軍事"); listtitle .add("財(cái)經(jīng)"); listtitle .add("時(shí)尚"); 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)起來(lái)。 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)用中怎么實(shí)現(xiàn)一個(gè)滑動(dòng)切換頁(yè)面功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享題目:Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)滑動(dòng)切換頁(yè)面功能
文章位置:http://chinadenli.net/article38/ppspsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、建站公司、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)