這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)FragmentPagerAdapter適配器,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的上饒網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
1適配器FragmentPagerAdapter的實(shí)現(xiàn)
對(duì)于FragmentPagerAdapter的派生類,只需要重寫getItem(int)和getCount()就可以了。
public class MyFragmentPagerAdapter extends FragmentPagerAdapter { private List<Fragment> list; public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) { super(fm); this.list = list; } //返回了當(dāng)前要滑動(dòng)的View的個(gè)數(shù) @Override public int getCount() { return list.size(); } //在getItem(int arg0)中,根據(jù)傳來(lái)的參數(shù)arg0,來(lái)返回當(dāng)前要顯示的fragment @Override public Fragment getItem(int arg0) { return list.get(arg0); } }
2兩個(gè)Fragment類
OrderFragment.java
public class OrderFragment extends Fragment { //在onCreateView()中返回要顯示的View @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view= inflater.inflate(R.layout.fragment_order, container, false); //View中控件的操作方法 refresh = view.findViewById(R.id.order_count_refresh); refresh.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //操作邏輯 countStatus(); } }); return view; } }
fragment_order.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" > <Button android:id="@+id/fragment1_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="show toast" /> </LinearLayout>
MeFragment.java
public class MeFragment extends Fragment { //在onCreateView()中返回要顯示的View @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view= inflater.inflate(R.layout.fragment_me, container, false); //View中控件的操作方法 login = view.findViewById(R.id.me_login); login.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MeFragment.this.getActivity(), LoginActivity.class)); } }); return view; } }
fragment_me.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" android:orientation="vertical" > </LinearLayout>
3主activity實(shí)現(xiàn)
public class MainActivity extends FragmentActivity { private ViewPager mPager; private List<Fragment> fragmentList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPager = (ViewPager) findViewById(R.id.viewpager); //初始化fragment List<Fragment> fragmentList=new ArrayList<Fragment>(); fragmentList.add(new OrderFragment()); fragmentList.add(new MeFragment()); FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments); //給ViewPager設(shè)置適配器 mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList)); mPager.setCurrentItem(0);// 設(shè)置當(dāng)前顯示標(biāo)簽頁(yè)為第一頁(yè) //MyOnPageChangeListener()是頁(yè)面變化監(jiān)聽器 mPager.setOnPageChangeListener(new MyOnPageChangeListener()); radioGroup = (RadioGroup) findViewById(R.id.main_tab); //給radioGroup設(shè)置監(jiān)聽器 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(checkedId == R.id.tab_me){ mPager.setCurrentItem(INDEX_ME); }else if(checkedId == R.id.tab_shoujian){ mPager.setCurrentItem(INDEX_SHOUJIAN); } } }); } //實(shí)現(xiàn)頁(yè)面變化監(jiān)聽器OnPageChangeListener public class MyOnPageChangeListener implements OnPageChangeListener { @Override //當(dāng)頁(yè)面在滑動(dòng)的時(shí)候會(huì)調(diào)用此方法,在滑動(dòng)被停止之前,此方法會(huì)一直得到調(diào)用。 /** * arg0:當(dāng)前頁(yè)面,及你點(diǎn)擊滑動(dòng)的頁(yè)面 * arg1:當(dāng)前頁(yè)面偏移的百分比 *arg2:當(dāng)前頁(yè)面偏移的像素位置 */ public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override //當(dāng)頁(yè)面狀態(tài)改變的時(shí)候調(diào)用 /** * arg0 * 1:表示正在滑動(dòng) * 2:表示滑動(dòng)完畢 * 0:表示什么都沒做,就是停在那 */ public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override //頁(yè)面跳轉(zhuǎn)完后調(diào)用此方法 /** * arg0是頁(yè)面跳轉(zhuǎn)完后得到的頁(yè)面的Position(位置編號(hào))。 */ public void onPageSelected(int arg0) { // TODO Auto-generated method stub switch (arg0) { case INDEX_ME: radioGroup.check(R.id.tab_me); break; case INDEX_SHOUJIAN: radioGroup.check(R.id.tab_shoujian); break; } } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_alignParentTop="true" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginBottom="60dip" android:flipInterval="30" android:persistentDrawingCache="animation"/> <RadioGroup android:id="@+id/main_tab" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_alignParentBottom="true" android:layout_height="60dip"> <RadioButton android:id="@+id/tab_shoujian" <!--圖片在文字的上面--> android:drawableTop="@drawable/tab_shoujian_selector" android:layout_height="match_parent" android:checked="true" android:text="****" /> <RadioButton android:id="@+id/tab_me" android:drawableTop="@drawable/tab_me_selector" android:layout_height="match_parent" android:text="*" /> </RadioGroup> </RelativeLayout>
上述就是小編為大家分享的Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)FragmentPagerAdapter適配器了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:Android應(yīng)用中怎么實(shí)現(xiàn)一個(gè)FragmentPagerAdapter適配器
瀏覽路徑:http://jinyejixie.com/article4/gpgdie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、定制網(wǎng)站、虛擬主機(jī)、建站公司、企業(yè)建站、商城網(wǎ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)