Android_ViewPager+Fragment实现页面滑动和底部导航栏

1、Xml中底部导航栏由一个RadioGroup组成,其上是ViewPager

 Android_ViewPager+Fragment实现页面滑动和底部导航栏_第1张图片

 

 




    

        

        
    

    
    
    

        

        

        
    
    

  

2、编写几个页面的Fragment

3、编写FragmentPagerAdapter,重写getItemgetCount方法。

 

package com.example.didida_corder;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

public class MyFragPageAdapter extends FragmentPagerAdapter {
    private int PAGE_NUM = 3;
    private final int PAGE_CORD = 0;
    private final int PAGE_INOUT = 1;
    private final int PAGE_MY = 2;
    private CordFragment cordFragment = null;
    private MyItemFragment myItemFragment = null;
    private InOutFragment inOutFragment = null;

    public MyFragPageAdapter(@NonNull FragmentManager fm) {
        super(fm);
        cordFragment = new CordFragment();
        myItemFragment = new MyItemFragment();
        inOutFragment = new InOutFragment();
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case PAGE_CORD: {
                return cordFragment;
            }
            case PAGE_INOUT: {
                return inOutFragment;
            }
            case PAGE_MY: {
                return myItemFragment;
            }
            default:
                return null;
        }

    }

    @Override
    public int getCount() {
        return PAGE_NUM;
    }

}

 

  

 

4、在MainActivity中实现ViewPager.OnPageChangeListener和RadioGroup.OnCheckedChangeListener接口。

 

package com.example.didida_corder;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener{
    private MyFragPageAdapter myFragPageAdapter;
    private ViewPager viewPager;
    private TextView textView;
    private RadioGroup radioGroup;
    private RadioButton radioButton_crod;
    private RadioButton radioButton_inout;
    private RadioButton radioButton_myitem;
    private int PAGE_NUM = 3;
    private final int PAGE_CORD = 0;
    private final int PAGE_INOUT = 1;
    private final int PAGE_MY = 2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myFragPageAdapter=new MyFragPageAdapter(getSupportFragmentManager());
        bindViews();
        radioButton_crod.setChecked(true);
    }
    private void bindViews(){
        radioButton_crod=findViewById(R.id.main_rd_cord);
        radioButton_inout=findViewById(R.id.main_rd_inout);
        radioButton_myitem=findViewById(R.id.main_rd_my);
        radioGroup=findViewById(R.id.main_radio_group);
        radioGroup.setOnCheckedChangeListener(this);
        viewPager= findViewById(R.id.main_viewpager);
        viewPager.setAdapter(myFragPageAdapter);
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(0);
    }


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

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
       if (state==2){
           switch (viewPager.getCurrentItem()){
               case PAGE_CORD:
                   radioButton_crod.setChecked(true);
                   break;
               case PAGE_INOUT:
                   radioButton_inout.setChecked(true);
                   break;
               case PAGE_MY:
                   radioButton_myitem.setChecked(true);
                   break;
           }
       }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.main_rd_cord:{
                viewPager.setCurrentItem(PAGE_CORD);
                break;
            }
            case R.id.main_rd_inout:{
                viewPager.setCurrentItem(PAGE_INOUT);
                break;
            }
            case R.id.main_rd_my:{
                viewPager.setCurrentItem(PAGE_MY);
                break;
            }
        }
    }
}

  

你可能感兴趣的:(Android_ViewPager+Fragment实现页面滑动和底部导航栏)