标签:android fragment 底部导航栏 viewpager
一、初始化各项组件
private void initViews() { viewPager = (ViewPager) findViewById(R.id.view_pager); imgDynamic = (ImageView) findViewById(R.id.tab_dynamic_get); imgMsg = (ImageView) findViewById(R.id.tab_msg_get); imgExam = (ImageView) findViewById(R.id.tab_exam_get); imgMe = (ImageView) findViewById(R.id.tab_me); viewPager.setOffscreenPageLimit(6); imgExam.setOnClickListener(new ImgOnClickListener(0)); imgDynamic.setOnClickListener(new ImgOnClickListener(1)); imgMsg.setOnClickListener(new ImgOnClickListener(2)); imgMe.setOnClickListener(new ImgOnClickListener(3)); }
二、在list里添加各个fragment(每个标签页的界面)
private void initAllFragments() { // 将Fragment添加到List中 fragments = new ArrayList<Fragment>(); examFragment = new ExamFragment(); fragments.add(examFragment); similarFragment=new SimilarPeopleFragment(); fragments.add(similarFragment); faxianFragment=new DiscoverFragment(); fragments.add(faxianFragment); messageFragment = new MessageFragment(); fragments.add(messageFragment); friendListFragment = new FriendListFragment(); fragments.add(friendListFragment); meFragment = new MeFragment(); fragments.add(meFragment); // 设置Adapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments); viewPager.setAdapter(myFragmentPagerAdapter); // 设置初始页面,并设置tab效果 viewPager.setCurrentItem(0); imgExam.setImageResource(R.drawable.tab_exam_press); // 设置切换的监听器 viewPager.setOnPageChangeListener(new MyPagerChangeListener()); }/** * 点击底部tab时的切换监听器 */ private class ImgOnClickListener implements View.OnClickListener { private int index = -1; // private ViewPager viewPagerGet; public ImgOnClickListener(int index) { this.index = index; // this.viewPagerGet = viewPagerGet; } @Override public void onClick(View v) { if (index==0) { viewPager.setCurrentItem(index, false); } if(index==1||index==2){ viewPager.setCurrentItem(index + 1, false); } if(index==3){ viewPager.setCurrentItem(index+2,false); } } }/** * tab切换时的监听器 */ private class MyPagerChangeListener implements ViewPager.OnPageChangeListener { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: /* if (currentIndex == 0) { imgDynamic.setImageResource(R.drawable.tab_dynamic); } else if (currentIndex == 1 || currentIndex == 2) { imgMsg.setImageResource(R.drawable.tab_message); } else if (currentIndex == 4) { imgMe.setImageResource(R.drawable.tab_me); }*/ imgDynamic.setImageResource(R.drawable.tab_dynamic); imgMsg.setImageResource(R.drawable.tab_message); imgMe.setImageResource(R.drawable.tab_me); imgExam.setImageResource(R.drawable.tab_exam_press); setExamActionBar(); setExamExam(); break; case 1: /* if (currentIndex == 0) { imgDynamic.setImageResource(R.drawable.tab_dynamic); } else if (currentIndex == 1 || currentIndex == 2) { imgMsg.setImageResource(R.drawable.tab_message); } else if (currentIndex == 4) { imgMe.setImageResource(R.drawable.tab_me); }*/ imgDynamic.setImageResource(R.drawable.tab_dynamic); imgMsg.setImageResource(R.drawable.tab_message); imgMe.setImageResource(R.drawable.tab_me); imgExam.setImageResource(R.drawable.tab_exam_press); setExamActionBar(); setExamSimilar(); break; case 2: imgMsg.setImageResource(R.drawable.tab_message); imgExam.setImageResource(R.drawable.tab_exam); imgMe.setImageResource(R.drawable.tab_me); imgDynamic.setImageResource(R.drawable.tab_dynamic_press); setDynamicActionBar(); break; case 3: /* if (currentIndex == 0) { imgDynamic.setImageResource(R.drawable.tab_dynamic); } else if (currentIndex == 3) { imgExam.setImageResource(R.drawable.tab_exam); } else if (currentIndex == 4) { imgMe.setImageResource(R.drawable.tab_me); } else { }*/ imgDynamic.setImageResource(R.drawable.tab_dynamic); imgExam.setImageResource(R.drawable.tab_exam); imgMe.setImageResource(R.drawable.tab_me); imgMsg.setImageResource(R.drawable.tab_message_press); setMesActionBar(); setMes(); break; case 4: /* if (currentIndex == 0) { imgDynamic.setImageResource(R.drawable.tab_dynamic); } else if (currentIndex == 3) { imgExam.setImageResource(R.drawable.tab_exam); } else if (currentIndex == 4) { imgMe.setImageResource(R.drawable.tab_me); } else { }*/ imgDynamic.setImageResource(R.drawable.tab_dynamic); imgExam.setImageResource(R.drawable.tab_exam); imgMe.setImageResource(R.drawable.tab_me); imgMsg.setImageResource(R.drawable.tab_message_press); setMesActionBar(); setFriend(); break; case 5: /*if (currentIndex == 0) { imgDynamic.setImageResource(R.drawable.tab_dynamic); } else if (currentIndex == 1 || currentIndex == 2) { imgMsg.setImageResource(R.drawable.tab_message); } else if (currentIndex == 3) { imgExam.setImageResource(R.drawable.tab_exam); }*/ imgDynamic.setImageResource(R.drawable.tab_dynamic); imgMsg.setImageResource(R.drawable.tab_message); imgExam.setImageResource(R.drawable.tab_exam); imgMe.setImageResource(R.drawable.tab_me_press); setSelfMesActionBar(); break; } currentIndex = position; } @Override public void onPageScrollStateChanged(int state) { } }/** * 点击底部tab时的切换监听器 */ private class ImgOnClickListener implements View.OnClickListener { private int index = -1; // private ViewPager viewPagerGet; public ImgOnClickListener(int index) { this.index = index; // this.viewPagerGet = viewPagerGet; } @Override public void onClick(View v) { if (index==0) { viewPager.setCurrentItem(index, false); } if(index==1||index==2){ viewPager.setCurrentItem(index + 1, false); } if(index==3){ viewPager.setCurrentItem(index+2,false); } } }** * 设置Fragment显示的Adapter * Created by lsy on 15-2-13. */ public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter { private ArrayList<Fragment> list; /* private Context context;*/ public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) { super(fm); this.list = list; } /* public MyFragmentPagerAdapter(ArrayList<Fragment> list,Context context){ this.list = list; this.context = context; }*/ @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } @Override public int getItemPosition(Object object) { return super.getItemPosition(object); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:android fragment 底部导航栏 viewpager
原文地址:http://blog.csdn.net/u010184245/article/details/47327629