我是靠谱客的博主 落寞玫瑰,最近开发中收集的这篇文章主要介绍Android 底部导航栏的使用 fragment,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、初始化各项组件

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);
    }
}

转载于:https://my.oschina.net/nandecanghai123/blog/1529295

最后

以上就是落寞玫瑰为你收集整理的Android 底部导航栏的使用 fragment的全部内容,希望文章能够帮你解决Android 底部导航栏的使用 fragment所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部