viewpager+fragment+radiobutton 底部导航栏实现

res布局资源文件:

values

color.xml:文件


    #00FFFFFF
    #FFFFFF
    #E5E5E5
    #8D7A71
    #FFFFFF
    #FFC800
    #FFC800
    #694E42
    #3F51B5
    #303F9F
    #ffffffff
    #FF4081
    
    #00000000

strings.xml:文件



      ......

    提醒
    信息
    我的
    设置

styles.xml:文件



        ......

    

mipmap-xhdpi:

图片资源放入mipmap-xhdpi:文件下

viewpager+fragment+radiobutton 底部导航栏实现_第1张图片

drawable:

tab_menu_better.xml文件:


    
    

tab_menu_channel.xml文件


    
    

 tab_menu_message.xml文件


    
    

  tab_menu_setting.xml文件


    
    

 tab_menu_text.xml文件


    
    

 tab_menu_bg.xml文件


    
        
            
        
    
    
        
            
        
    

layout

viewpager.xml文件



    
        
        
        
        
    
    
    

 myfragment1.xml文件:



    
        
        
    





 myfragment2.xml文件: 



    
    

注:myfragment3.xml和myfragment4.xml与myfragment1.xml相似,根据自己的需求随意设置布局。这里不再给出。

Java资源文件:

 

adapter

新建adapter包,包里新建MyFragmentPagerAdapter类

MyFragmentPagerAdapter.java
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    private final int PAGER_COUNT = 4;
    private MyFragment1 myFragment1 = null;
    private MyFragment2 myFragment2 = null;
    private MyFragment3 myFragment3 = null;
    private MyFragment4 myFragment4 = null;
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        myFragment1 = new MyFragment1();
        myFragment2 = new MyFragment2();
        myFragment3 = new MyFragment3();
        myFragment4 = new MyFragment4();
    }
    @Override
    public int getCount() {
        return PAGER_COUNT;
    }
    @Override
    public Object instantiateItem(ViewGroup vg, int position) {
        return super.instantiateItem(vg, position);
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        System.out.println("position Destory" + position);
        super.destroyItem(container, position, object);
    }
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position) {
            case MainActivity.PAGE_ONE:
                fragment = myFragment1;
                break;
            case MainActivity.PAGE_TWO:
                fragment = myFragment2;
                break;
            case MainActivity.PAGE_THREE:
                fragment = myFragment3;
                break;
            case MainActivity.PAGE_FOUR:
                fragment = myFragment4;
                break;
        }
        return fragment;
    }
}

fragment

新建fragment包,包里新建MyFragment1类

MyFragment1.java
public class MyFragment1 extends Fragment {
    public MyFragment1() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myfragment1, container, false);
        return view;
    }
}

注:MyFragment2.java,MyFragment3.java,MyFragment4.java和MyFragment1.java相似,只需将View view = inflater.inflate(R.layout.myfragment1, container, false); 换成对应的布局文件。然后根据自己的需求在MyFragment1.java文件中添加功能。

MainActivity.java
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener {
    @BindView(R.id.rg_tab_bar)
    RadioGroup rg_tab_bar;
    @BindView(R.id.rb_channel)
    RadioButton rbChannel;
    @BindView(R.id.rb_message)
    RadioButton rbMessage;
    @BindView(R.id.rb_better)
    RadioButton rbBetter;
    @BindView(R.id.rb_setting)
    RadioButton rbSetting;
    @BindView(R.id.vpager)
    ViewPager vpager;
    //UI Objects
    //几个代表页面的常量
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;

    private MyFragmentPagerAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.viewpager);
        ButterKnife.bind(this);
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        bindViews();
        rbChannel.setChecked(true);
    }

    //UI组件初始化与事件绑定
    private void bindViews() {
        rg_tab_bar.setOnCheckedChangeListener((RadioGroup.OnCheckedChangeListener) this);
        vpager.setAdapter(mAdapter);
        vpager.setCurrentItem(0);  //初始设置ViewPager选中第一帧
        vpager.addOnPageChangeListener(this);
        vpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        break;
                    case 1:
                        //StatusBarUtil.setTranslucentStatus(MainActivity.this);//设置状态栏透明
                        break;
                    case 2:
                    case 3:
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_channel:
                vpager.setCurrentItem(PAGE_ONE);
                break;
            case R.id.rb_message:
                vpager.setCurrentItem(PAGE_TWO);
                break;
            case R.id.rb_better:
                vpager.setCurrentItem(PAGE_THREE);
                break;
            case R.id.rb_setting:
                vpager.setCurrentItem(PAGE_FOUR);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        /*此方法在页面被选中时调用*/

    }
    
    @Override
    public void onPageScrollStateChanged(int state) {
        //state的状态有三个,0表示什么都没做,1正在滑动,2滑动完毕
        if (state == 2) {
            switch (vpager.getCurrentItem()) {
                case PAGE_ONE:
                    rbChannel.setChecked(true);
                    break;
                case PAGE_TWO:
                    rbMessage.setChecked(true);
                    break;
                case PAGE_THREE:
                    rbBetter.setChecked(true);
                    break;
                case PAGE_FOUR:
                    rbSetting.setChecked(true);
                    break;
            }
        }
    }
}

注:该代码里引入了ButterKnife ,可以省去写很多findViewById来找View对象。

butterknife AndroidStudio3.1.2 配置及使用

 

你可能感兴趣的:(viewpager+fragment+radiobutton 底部导航栏实现)