Fragment页面的切换
2016-04-16 12:03
393 查看
在android日常开发中,经常会用到点击底部导航栏,然后多个Fragment互相切换的功能。之前做毕设的时候用到了这个功能,今天有空整理了一下,并且做了androidStudio的demo,供大家一起讨论学习。闲话不多说,直接上图,大约就是这个样子(欢迎转载,请说明来处)Demo下载地址:http://download.csdn.net/detail/sinat_27681957/9492952
主文件用来控制Ftagment的切换:
主布局文件里面放置3个fragment:
MainFragment:
HomeFragment:
UserFragment:
fragment_main.xml:
Fragment_home.xml:
Fragment_user.xml:
好了差不多了,Demo下载地址:http://download.csdn.net/detail/sinat_27681957/9492952
主文件用来控制Ftagment的切换:
public class MainActivity extends AppCompatActivity { private Fragment[] mFragments; private RadioGroup bottomRg; private FragmentManager fragmentManager; private FragmentTransaction fragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init(){ mFragments = new Fragment[3]; fragmentManager = getSupportFragmentManager(); mFragments[0] = fragmentManager.findFragmentById(R.id.fragment_main); mFragments[1] = fragmentManager.findFragmentById(R.id.fragment_bbs); mFragments[2] = fragmentManager.findFragmentById(R.id.fragment_user); fragmentTransaction = fragmentManager.beginTransaction() .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]); fragmentTransaction.show(mFragments[0]).commit(); setFragmentIndicator(); } //切换fragment private void setFragmentIndicator() { bottomRg = (RadioGroup) findViewById(R.id.bottomRg); bottomRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { fragmentTransaction = fragmentManager.beginTransaction() .hide(mFragments[0]).hide(mFragments[1]) .hide(mFragments[2]); switch (checkedId) { case R.id.rbOne: fragmentTransaction.show(mFragments[0]).commit(); Log.e("1", "1"); break; case R.id.rbTwo: fragmentTransaction.show(mFragments[1]).commit(); Log.e("2", "2"); break; case R.id.rbThree: fragmentTransaction.show(mFragments[2]).commit(); Log.e("3", "3"); break; default: break; } } }); } }
主布局文件里面放置3个fragment:
<?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" > <!-- 上边主页面 --> <fragment android:id="@+id/fragment_main" android:name="com.lei.doctorofhand.fragment.MainFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="20" /> <fragment android:id="@+id/fragment_bbs" android:name="com.lei.doctorofhand.fragment.HomeFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="20" /> <fragment android:id="@+id/fragment_user" android:name="com.lei.doctorofhand.fragment.UserFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="20" /> <!-- 分割线 --> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/bgcolor"/> <!-- 底部菜单页面 --> <RadioGroup android:id="@+id/bottomRg" android:layout_width="fill_parent" android:layout_height="55dp" android:background="@color/footer_bg_normal" android:orientation="horizontal" > <RadioButton android:id="@+id/rbOne" style="@style/rg_btn_style" android:checked="true" android:text="首页" android:textColor="@color/black" android:drawableTop="@drawable/rb_one_btn_selector"/> <RadioButton android:id="@+id/rbTwo" style="@style/rg_btn_style" android:text="房间" android:textColor="@color/black" android:drawableTop="@drawable/rb_two_btn_selector"/> <RadioButton android:id="@+id/rbThree" style="@style/rg_btn_style" android:text="设置" android:textColor="@color/black" android:drawableTop="@drawable/rb_three_btn_selector"/> </RadioGroup> </LinearLayout>
MainFragment:
public class MainFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view=inflater.inflate(R.layout.fragment_main,container,false); return view; }
HomeFragment:
public class HomeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_home, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initView(); } private void initView() { } }
UserFragment:
public class UserFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_user, container, false); } }
fragment_main.xml:
<?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:background="@color/bgwhite" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="首页"/> </LinearLayout>
Fragment_home.xml:
<?xml version="1.0" encoding="utf-8"?> <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:background="@color/bgwhite" android:orientation="vertical" tools:context="com.askdoctor.ui.FragmentBBS" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="房间"/> </LinearLayout>
Fragment_user.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/bgwhite" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="用户"/> </RelativeLayout>
好了差不多了,Demo下载地址:http://download.csdn.net/detail/sinat_27681957/9492952
相关文章推荐
- JAVA修改组件
- 【模拟】HDU4509湫湫系列故事——减肥记II
- Windows Docker的有趣事实 推荐
- Linux用户管理:addgroup、adduser、usermod
- android 调用远程service时报java.lang.SecurityException: Binder invocation to an incorrect interface异常
- position、opacity与z-index的组合
- 复利计算再升级
- 关于stm32定时器的理解
- SeimiAgent——基于QtWebkit的通用网页渲染代理服务
- 关于stm32定时器的理解
- Android 学习记录-零散知识点
- 11. Container With Most Water
- jQuery实现漂亮实用的商品图片tips提示框效果(无图片箭头+阴影)
- 什么是P问题、NP问题和NPC问题
- spring mvc中的@PathVariable
- CentOS安装软件<一> MYSQL
- c++(1)
- 单例模式的五种写法
- 织梦后台jpeg格式图片没法上传,图片浏览器中没法查看预览
- OpenStack 安装教程(使用Fuel )