一.Fragment使用:

要在你的activity中管理Fragment,需要使用FragmentManager,可以通过getFragmentManager(),这里注意要是在v4包要用getSupportFragmentManager()方法。

FragmentManager可以开一个事物调用beginTransaction()方法返回FragmentTransaction对象,这是我们可以用FragmentTransaction去执行删除增加Fragment的一些操作:

(1).add():往activity添加一个Fragment


(2).remove():从Activity移除一个Fragment,当这个Fragment没有添加进回退栈,则会被销毁。当添加进入回退栈之后则会销毁视图层,在显示这个Fragment则会调用onDestoryView和onCreateView。


(3).replace():使用另一个Fragment替换当前的Fragment,也就是remove操作和add合体执行。


(4).hide():隐藏当前的Fragment,仅仅是设为不可见,并不会销毁。


(5).show():显示之前隐藏的Fragment。


(6).detach():会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。


(7).attach():重建view视图,附加到UI上并显示。


(8).addToBackStack():添加Fragment事务到回退栈中。传null表示当前Fragment。


(9).commit():提交事务。


注意当以使用了add添加Fragment之后,你仅仅是想隐藏Fragment,而且保持当前Fragment输入的数据,你只需要用hide(),如果你不想用户在看到数据,直接使用replace()比使用add()在remove()方便的多。

二.如何使用回退栈保持数据:

实现效果,当进入第一个Fragment后点击改变文字按钮,会把显示在屏幕中央的TextView内容改变,然后点击进入第二个Fragment,在点击返回按钮,屏幕中央的TextView内容会还原成未改变前的,这就是说明了退回栈把Fragment的视图销毁了,但是实例并没有销毁,如果你想要不把数据也销毁,则就像上面说的使用hide()和show().

图片就不上传了,大家可是自行测试源码(要源码留下邮箱,这个是在上篇文章基础上修改的):

主Activity:

public class MainActivity extends Activity  {RelativeLayout r1;RelativeLayout r2;RelativeLayout r3;RelativeLayout view = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.bottom_layout);r1 = (RelativeLayout) findViewById(R.id.layout1);r2 = (RelativeLayout) findViewById(R.id.layout2);r3 = (RelativeLayout) findViewById(R.id.layout3);setDefaultFragment();}private void setDefaultFragment() {FragmentManager fm = getFragmentManager();FragmentTransaction transaction = fm.beginTransaction();MyFragment my = new MyFragment();transaction.add(R.id.frame_layout1, my,"ONE");transaction.addToBackStack(null);transaction.commit();}}

Fragment1:

public class MyFragment extends Fragment implements OnClickListener {private Button button1;private Button button2;private TextView textView1;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {System.out.println("onCreateView");View view = inflater.inflate(R.layout.fragment_1, container, false);button1 = (Button) view.findViewById(R.id.button1);button1.setOnClickListener(this);button2 = (Button) view.findViewById(R.id.button2);button2.setOnClickListener(this);textView1 = (TextView) view.findViewById(R.id.textView1);return view;}@Overridepublic void onClick(View arg0) {switch (arg0.getId()) {case R.id.button1:textView1.append("哈");break;case R.id.button2:MyFragment2 f2 = new MyFragment2();FragmentManager fm = getFragmentManager();FragmentTransaction tx = fm.beginTransaction();tx.replace(R.id.frame_layout1, f2, "TWO");tx.addToBackStack(null);tx.commit();break;}}}

Fragment2:

public class MyFragment2 extends Fragment implements OnClickListener {private Button button1;private Button button2;private TextView textView1;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {System.out.println("onCreateView");View view = inflater.inflate(R.layout.fragment_2, container, false);button1 = (Button) view.findViewById(R.id.button11);button1.setOnClickListener(this);button2 = (Button) view.findViewById(R.id.button21);button2.setOnClickListener(this);textView1 = (TextView) view.findViewById(R.id.textView2);return view;}@Overridepublic void onClick(View arg0) {switch (arg0.getId()) {case R.id.button11:textView1.append("哈");break;case R.id.button21:MyFragment3 f3 = new MyFragment3();FragmentManager fm = getFragmentManager();FragmentTransaction tx = fm.beginTransaction();tx.hide(this);tx.add(R.id.frame_layout1, f3, "THREE");// tx.replace(R.id.id_content, fThree, "THREE");tx.addToBackStack(null);tx.commit();break;}}}
Fragment1布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="第一个页面" />       <Button        android:id="@+id/button1"        android:layout_width="fill_parent"        android:layout_height="40dp"        android:text="改变文字" />        <Button        android:id="@+id/button2"        android:layout_width="fill_parent"        android:layout_height="40dp"        android:layout_below="@id/button1"        android:text="跳转第二个界面" />  </RelativeLayout>

Fragment3:

public class MyFragment3 extends Fragment{    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {    return inflater.inflate(R.layout.fragment_3, container, false);    }}

这样就ok了。


三.Activity与Fragment之间的回调:

因为要考虑Fragment的重复使用,所以必须降低Fragment与Activity的耦合,而且Fragment更不应该直接操作别的Fragment,毕竟Fragment操作应该由它的管理者Activity来决定。


就用上边的例子,第一种回调:

public class MyFragment extends Fragment implements OnClickListener {private Button button1;private Button button2;private TextView textView1;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {System.out.println("onCreateView");View view = inflater.inflate(R.layout.fragment_1, container, false);button1 = (Button) view.findViewById(R.id.button1);button1.setOnClickListener(this);button2 = (Button) view.findViewById(R.id.button2);button2.setOnClickListener(this);textView1 = (TextView) view.findViewById(R.id.textView1);return view;}public interface MyFragmentOneClick{void onMyOneBtnClick();  }@Overridepublic void onClick(View arg0) {switch (arg0.getId()) {case R.id.button1:textView1.append("哈");break;case R.id.button2://第一种回调方式if (getActivity() instanceof MyFragmentOneClick)          {              ((MyFragmentOneClick) getActivity()).onMyOneBtnClick();          }  break;}}}


第二种回调:

public class MyFragment2 extends Fragment implements OnClickListener {private Button button1;private Button button2;private TextView textView1;private MyFragmentTwoClick myFragmentTwoClick ;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {System.out.println("onCreateView");View view = inflater.inflate(R.layout.fragment_2, container, false);button1 = (Button) view.findViewById(R.id.button11);button1.setOnClickListener(this);button2 = (Button) view.findViewById(R.id.button21);button2.setOnClickListener(this);textView1 = (TextView) view.findViewById(R.id.textView2);return view;}public interface MyFragmentTwoClick{void onMyTwoBtnClick();  }//第二种回调方式,设置回调接口      public void setMyTwoBtnClickListener(MyFragmentTwoClick myFragmentTwoClick)      {          this.myFragmentTwoClick = myFragmentTwoClick;      }  @Overridepublic void onClick(View arg0) {switch (arg0.getId()) {case R.id.button11:textView1.append("哈");break;case R.id.button21:if(myFragmentTwoClick != null)          {  myFragmentTwoClick.onMyTwoBtnClick();          }  break;}}}

主Activity实现:


public class MainActivity extends Activity implements MyFragmentOneClick,MyFragmentTwoClick {RelativeLayout r1;RelativeLayout r2;RelativeLayout r3;RelativeLayout view = null;MyFragment f1; MyFragment2 f2 ;MyFragment3 f3; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.bottom_layout);r1 = (RelativeLayout) findViewById(R.id.layout1);r2 = (RelativeLayout) findViewById(R.id.layout2);r3 = (RelativeLayout) findViewById(R.id.layout3);setDefaultFragment();}private void setDefaultFragment() {FragmentManager fm = getFragmentManager();FragmentTransaction transaction = fm.beginTransaction();f1 = new MyFragment();transaction.add(R.id.frame_layout1, f1,"ONE");transaction.addToBackStack(null);transaction.commit();}@Overridepublic void onMyOneBtnClick() {if (f2 == null)          {  f2 = new MyFragment2();  f2.setMyTwoBtnClickListener(this);          }  FragmentManager fm = getFragmentManager();FragmentTransaction tx = fm.beginTransaction();tx.replace(R.id.frame_layout1, f2, "TWO");tx.addToBackStack(null);tx.commit();}@Overridepublic void onMyTwoBtnClick() {if (f3 == null)          {  f3 = new MyFragment3();            }  FragmentManager fm = getFragmentManager();FragmentTransaction tx = fm.beginTransaction();tx.hide(f2);tx.add(R.id.frame_layout1, f3, "THREE");tx.addToBackStack(null);tx.commit();}}
这样也实现了上述的功能。





更多相关文章

  1. “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
  2. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  3. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  4. ExpandableListView 和 ExpandableListActivity的使用及数据更新
  5. Android实现下拉放大图片松手自动反弹效果
  6. Android如何解析json数组对象
  7. Android用RecyclerView实现动态添加本地图片
  8. android页面间传输数据
  9. Anroid camera + mediacodec

随机推荐

  1. 面试PHP中高级工程师的时候总是会问到这
  2. U盘显示操作无法完成,因为磁盘管理控制台
  3. 磁盘出现“文件系统变RAW”的解决方法
  4. 快速开发对项目有价值
  5. laravel 本地集成开发环境,路由访问 Not F
  6. 数据库中间件 MyCat1.6 安装使用(docker
  7. PE装到移动硬盘的数据找到办法
  8. 中了exe病毒文件夹变exe应用程序解决方法
  9. SQL查询语句
  10. 后台一 搭建项目