ViewPager单独使用的场景 ------------------------------ 软件更新的引导界面、广告界面等等非常简单的View对象的显示,即没有复杂的界面嵌套、程序的逻辑。 ViewPager+Fragment ------------------------------ 【定位】 ViewPager:作为Fragment的容器,提供ViewPager自身的特性功能,例如支持手势、默认动画等 Fragment:初始化、显示各个View,对用户的操作进行响应 Activity:初始化ViewPager,使用ViewPager的Adapter对多个Fragment进行管理 【实现步骤】 1. 在res\layout下创建各个View对应的布局 2. 创建匹配数量的Fragment,继承自v4包中的Fragment,在每一个Fragment中都重写onCreateView()方法,加载布局、初始化控件等,并返回View对象 3. 在Activity中初始化ViewPager控件,并为其配置FragmentPagerAdapter 4. 将Activity的父类修改为FragmentActivity,并通过getSupportFragmentManager()方法获取FragmentManager对象,作为FragmentPagerAdapter构造方法的参数 xml界面文件 frag: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="联系人" android:textSize="15sp"/> </LinearLayout> main: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/vp_view_page" android:layout_width="match_parent" android:layout_height="350dp" /> <RadioGroup android:id="@+id/rg_main_menu" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_contact" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:background="@drawable/menu_contact" android:button="@null"/> <RadioButton android:id="@+id/rb_message" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:background="@drawable/menu_message" android:button="@null" /> <RadioButton android:id="@+id/rb_nearby" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:background="@drawable/menu_nearby" android:button="@null" /> <RadioButton android:id="@+id/rb_my" android:layout_width="0dp" android:layout_height="65dp" android:layout_weight="1" android:background="@drawable/menu_my" android:button="@null" /> </RadioGroup> </LinearLayout> java文件 frag: package com.example.fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Page01Fragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.frag_contact, null); return view; } } main: public class MainActivity extends FragmentActivity implements OnPageChangeListener { private ViewPager vpViewPager; private FragmentPagerAdapter adapter; private RadioGroup rg_main_menu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化Fragment容器 vpViewPager = (ViewPager) findViewById(R.id.vp_view_page); // 初始化按钮控件 rg_main_menu=(RadioGroup)findViewById(R.id.rg_main_menu); InnerOnCheckedChangeListener listener = new InnerOnCheckedChangeListener(); rg_main_menu.setOnCheckedChangeListener(listener); adapter = new InnerPagerAdapter(getSupportFragmentManager()); vpViewPager.setAdapter(adapter); vpViewPager.setOnPageChangeListener(this); } private class InnerPagerAdapter extends FragmentPagerAdapter{ public InnerPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { Fragment frag = null; switch (position) { case 0: frag=new Page01Fragment(); break; case 1: frag=new Page02Fragment(); break; case 2: frag=new Page03Fragment(); break; case 3: frag=new Page04Fragment(); break; } return frag; } @Override public int getCount() { return 4; } } private class InnerOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener{ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { int itemIndex = 0; switch (checkedId) { case R.id.rb_contact: itemIndex = 0; break; case R.id.rb_message: itemIndex = 1; break; case R.id.rb_nearby: itemIndex = 2; break; case R.id.rb_my: itemIndex = 3; break; } vpViewPager.setCurrentItem(itemIndex); } } public void onPageSelected(int position) { int checkId=R.id.rb_contact; switch (position) { case 1: checkId = R.id.rb_message; break; case 2: checkId = R.id.rb_nearby; break; case 3: checkId = R.id.rb_my; break; } rg_main_menu.check(checkId); } public void onPageScrollStateChanged(int arg0) { } public void onPageScrolled(int arg0, float arg1,int arg2){ } }
ViewPager与底部菜单的关联
最新推荐文章于 2021-05-26 23:35:20 发布
本文介绍了如何将ViewPager与底部菜单结合使用。在软件更新引导界面或广告界面等简单场景,ViewPager可以单独工作。当配合Fragment时,ViewPager作为Fragment容器,负责手势滑动和动画效果,Fragment则承载具体视图并响应用户操作。实现步骤包括创建Fragment布局、初始化ViewPager、配置FragmentPagerAdapter,以及在Activity中管理菜单和页面切换。
摘要由CSDN通过智能技术生成