【直接切入主题】:
相信这个时候的你一定建立好了1个以上的fragment,只是苦于切换操作怎么走?
希望做到的是,在fragmentA点击了什么可以切换到fragmentB
但是所有的Fragment都是托管于该绑定的Activity
想要自己切换,没门!想要切换,必须先问过老大Activity的同意,然后老大来切换!
这块看的不懂得建议看我博客“Fragment”里面的章节,老大与小弟的故事。
那接下来我们的逻辑是,每个需要切换的Fragment写个报告书给老大,报告切换,这个似乎是比较简单的方法。
也是正确的,只是每个fagment写的报告书异曲同工,如何缩减工作量呢?
【抽象类和什么事情都交给老大去做就好了】
首先看下Fragment要做的事情
view=View.inflate(main, R.layout.create_frame, null);
所有的fragment都要做的事情,于是我们做个抽象类,写抽象方法,去替代这个重复的方法。
代码如下:
package com.example.testdrawerlayoutleft; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public abstract class BaseFragment extends Fragment { public Main main; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { main = (Main) getActivity(); View view = initView(); return view; } public abstract View initView(); }
然后让每个fagment去继承这个抽象类:
package com.example.testdrawerlayoutleft; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class fragment_content extends BaseFragment { private View view; private Button create; @Override public View initView() { // TODO Auto-generated method stub if(view==null){ view=View.inflate(main, R.layout.fragment_content, null); create=(Button)view.findViewById(R.id.creat_bt); create.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // Toast.makeText(main, "sdsd", 100).show(); main.switchFragment("content", "create"); } }); } return view; } }
这里设置点击,此按钮跳转到另外一个Fragment
那么我们的Activity可以这样写:
public void switchFragment(String fromTag, String toTag) { Fragment from = fragmentManager.findFragmentByTag(fromTag); Fragment to = fragmentManager.findFragmentByTag(toTag); if (mCurrentFragment != to) { mCurrentFragment = to; FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); if (!to.isAdded()) {//判断是否被添加到了Activity里面去了 fragmentTransaction.hide(from).add(R.id.content_frame, to).commit(); } else { fragmentTransaction.hide(from).show(to).commit(); } } } /**
【注意的坑】:
FragmentManager 第一次创建后就可以一直使用fragmentManager对象
但是!!!!!!FragmentTransaction fragmentTransaction每次在切换添加的时候必须重新声明一次,要不
会出现加载不出来崩溃的想象,看了很多博客,使用的时候必须每次用,每次声明,具体原理不知,待找到原理再来告知,但是这个坑!!务必慎重踏
到这里基本全部结束
最后只要在Main Activity里面加入显示代码:
fragmentManager = getFragmentManager(); fragmentTransaction = fragmentManager .beginTransaction(); fragmentTransaction.add(R.id.content_frame,fragment_content, "content").add(R.id.content_frame,Create_frame, "create").hide(Create_frame).commit();
main的xml文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.widget.DrawerLayout android:id="@+id/drawlayout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 中间的主题显示区域 --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <FrameLayout android:id="@+id/content_frame1" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <!-- 左边的侧滑菜单 --> <ListView android:id="@+id/lv_leftmenu" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#ffc" > </ListView> </android.support.v4.widget.DrawerLayout> </LinearLayout>
最后:
就是使用DrawLayout的时候,存在的Framelayout冲突,虽然不知道为什么DrawLayout一定需要FrameLayout但是Fragment需要的显示contianer 不可以是DrawLayout 发现一旦使用一样的直接崩溃,解决的方法简单就是再写一个FrameLayout作为显示布局,看起来解决方便,但是当时发现时这个问题的时候已经过去了两天,也算是一个大坑级别了。
示例代码demo 在下面下载使用,有任何问题可以评论联系。
相关推荐
Fragment的嵌套和侧滑菜单DrawLayout加标签页SlidingTabLayout或PagerTabStrip的使用demo. Android studio项目
Fragment的四种跳转方式: 1、从同一个Activiy的一个Fragment跳转到另外一个Fragment 2、从一个Activity的Fragment跳转到另外一个Activity 3、从一个Activity跳转到另外一个Activity的Fragment上 4、从一个Activity...
通过接口的方式实现fragment之间的跳转操作
android平板电脑开发,使用Fragment实现页面的局部跳转,像管理系统一样,点击左边的连接实现右边页面的跳转
自己项目中用到fragment搭建框架,从网上找得一个demo,实现activity与fragment相互传值;fragment与fragment跳转。跟大家分享
fragment回退,fragment之间相互跳转
简单实例菜单导航(Fragment+Button),新手可参考。
fragment间的跳转例子,博文地址:http://blog.csdn.net/wei18359100306/article/details/41174155
ViewPager+Fragment导航栏支持滑动和点击切换fragment,点击某个按钮触发替换某个tab的fragment。如有tab1,tab2两个导航按钮,分别对应tab1fragment和tab2fragment,点击某个按钮触发,将tab2的tab2fragment替换tab3...
ViewPage + RadioGroup + Fragment + FrameLayout搭配解析,RadioGroup 里面有四个RadioButton切换,上面用ViewPage滑动,两边关联,可以相互的切换。也做了用FrameLayout可切换和不可切换的。
activity和fragment的生命周期以及intent跳转
为了实现从Fragment跳转到其他Activity,下面需要创建以下文件: 第一步:简单编写布局文件fragment_activity.xml和抽象类TemplateFragmentActivity.java代码如下: fragment_activity.xml <?xml version=1.0 ...
一个纯代码实现的android底部菜单栏,动态加载,简单实用
Demo结构 fragment_first.xml fragment_second.xml fragment_main.xml 效果图: 原创文章 2获赞 1访问量 119 关注 私信 展开阅读全文 作者:河边啃母鸡
Android通过按钮实现Fragment之间的跳转,代码可以运行.zip
实现Activity 之间以及 fragment 之间 共享元素 平滑动画过渡跳转,实现界面跳转动画
Fragment嵌套fragmen。对应的博客链接:http://blog.csdn.net/r_ine,其中有一篇是对其的介绍。这是一个 eclipse资源。
Android应用中的fragement跳转demo,跳转是从一个子fragement上的按钮跳转到其他子fragement上,不是通过导航栏上的按钮
就是简单明了的android简单闪频+登陆+fragment+跳转,页面少,代码简洁易懂有效率