【直接切入主题】:
相信这个时候的你一定建立好了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 在下面下载使用,有任何问题可以评论联系。