人生就像钟表,可以回到起点,却已不是昨天!
本讲内容:ViewPager+FragmentPagerAdapter+RadioGroup实现底部菜单栏
示例一:可滑动可点击并且由Fragment管理(布局文件和上一讲相同,这里就不贴了)
下面是res/layout/activity_main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/top" />
<android.support.v4.view.ViewPager
android:id="@+id/id_viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"/>
<include layout="@layout/bottom"/>
</LinearLayout>
下面是MyFragmentPagerAdapter.java文件:
/**
* 四个Fragment页卡设置适配器
*/
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyFragmentPagerAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments=fragments;
}
public Fragment getItem(int position) {
return fragments.get(position);
}
public int getCount() {
return fragments.size();
}
}
下面是MainActivity.java主界面文件:
public class MainActivity extends FragmentActivity {
private RadioGroup mRadioGroup;
private RadioButton mRadio01;
private RadioButton mRadio02;
private RadioButton mRadio03;
private RadioButton mRadio04;
private ViewPager mViewPager;
private MyFragmentPagerAdapter mAdapter;
private List<Fragment> mFragments=new ArrayList<Fragment>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initEvent();
}
private void initViews() {
mRadioGroup=(RadioGroup) findViewById(R.id.id_radioGroup);
mRadio01=(RadioButton) findViewById(R.id.id_tab1);
mRadio02=(RadioButton) findViewById(R.id.id_tab2);
mRadio03=(RadioButton) findViewById(R.id.id_tab3);
mRadio04=(RadioButton) findViewById(R.id.id_tab4);
mFragments.add(new Tab01Fragment());
mFragments.add(new Tab02Fragment());
mFragments.add(new Tab03Fragment());
mFragments.add(new Tab04Fragment());
mViewPager=(ViewPager) findViewById(R.id.id_viewPager);
mAdapter=new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments);
mViewPager.setAdapter(mAdapter);
}
private void initEvent(){
mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.id_tab1:
mViewPager.setCurrentItem(0);//选择某一页
break;
case R.id.id_tab2:
mViewPager.setCurrentItem(1);
break;
case R.id.id_tab3:
mViewPager.setCurrentItem(2);
break;
case R.id.id_tab4:
mViewPager.setCurrentItem(3);
break;
}
}
});
mViewPager.setOnPageChangeListener(new TabOnPageChangeListener());
}
/**
* 页卡滑动改变事件
*/
public class TabOnPageChangeListener implements OnPageChangeListener{
//当滑动状态改变时调用
public void onPageScrollStateChanged(int state) {
}
//当前页面被滑动时调用
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//当新的页面被选中时调用
public void onPageSelected(int position) {
switch (position) {
case 0:
mRadio01.setChecked(true);
break;
case 1:
mRadio02.setChecked(true);
break;
case 2:
mRadio03.setChecked(true);
break;
case 3:
mRadio04.setChecked(true);
break;
}
}
}
}
Take your time and enjoy it 要原码的、路过的、学习过的请留个言,顶个呗~~