关闭

ViewPager+Fragment+RadioGroup实现页面联动\点击切换

308人阅读 评论(0) 收藏 举报
分类:

废话不多说 上源码

MianActivity.java

public class MainActivity extends FragmentActivity {

/**
 * 声明viewpager
 */
@ViewInject(R.id.vp_veiwpager)
private ViewPager mViewPager;

/**
 * 声明list集合
 */
private List<Fragment> fragmentList;

/**
 * 声明radiogroup相关按键
 */
@ViewInject(R.id.rg_radiogroup)
private RadioGroup radioGroup;
@ViewInject(R.id.rb_bookstore)
private RadioButton rbStore;
@ViewInject(R.id.rb_bookcart)
private RadioButton rbCart;
@ViewInject(R.id.rb_lately)
private RadioButton rbLately;
@ViewInject(R.id.rb_mine)
private RadioButton rbMine;

/**
 * 声明viewpager 的适配器
 */
private MViewPagerAdapter mViewPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 初始化控件
    x.view().inject(this);
    // 设置adapter
    setAdapter();
    // 设置listener
    setListener();

}

/**
 * 为viewpager设置适配器
 */
private void setAdapter() {
    fragmentList = new ArrayList<Fragment>();
    fragmentList.add(new FragmentStore());
    fragmentList.add(new FragmentCart());
    fragmentList.add(new FragmentLately());
    fragmentList.add(new FragmentMine());
    mViewPagerAdapter = new MViewPagerAdapter(getSupportFragmentManager(),
            fragmentList);
    mViewPager.setAdapter(mViewPagerAdapter);
    // 启动程序直接加载4个界面的数据
    mViewPager.setOffscreenPageLimit(4);
}

/**
 * 设置viewpager跟radiogroup的监听跟联动
 */
private void setListener() {// radioGroup设置onCheckedChangeListener
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.rb_bookstore:
                mViewPager.setCurrentItem(0);// setCurrentItem 设置当前项目为0
                break;
            case R.id.rb_bookcart:
                mViewPager.setCurrentItem(1);// setCurrentItem 设置当前项目为1
                break;
            case R.id.rb_lately:
                mViewPager.setCurrentItem(2);// setCurrentItem 设置当前项目为2
                break;
            case R.id.rb_mine:
                mViewPager.setCurrentItem(3);// setCurrentItem 设置当前项目为3
                break;

            }
        }
    });
    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
        //为viewpager配置onPageChangeListener方法
        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
            case 0:
                rbStore.setChecked(true);
                break;
            case 1:
                rbCart.setChecked(true);
                break;
            case 2:
                rbLately.setChecked(true);
                break;
            case 3:
                rbMine.setChecked(true);
                break;

            }
        }

        @Override
        public void onPageScrolled(int arg0, float v, int arg2) {
            if(v!=0){
                //设置第四个fragment的透明度
                FragmentMine fragment = (FragmentMine) fragmentList.get(3);
                fragment.slide(v);
            }
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    });

}

}
ViewPagerAdapter.java

public class MViewPagerAdapter extends FragmentPagerAdapter{

private List<Fragment> fragmentList;


public MViewPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
    super(fm);
    this.fragmentList = fragmentList;
}

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    return fragmentList.get(arg0);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return fragmentList.size();
}

}
Fragment

public class FragmentCart extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cart, null);

return view;

}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:504次
    • 积分:26
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档