android Fragment FragmentActivity 选项卡 replace add hide show
来源:互联网 发布:手机淘宝寄快递在哪里 编辑:程序博客网 时间:2024/04/28 03:37
android Fragment FragmentActivity 选项卡 replace add hide show
1、获取FragmentManage的方式:
getFragmentManager() // v4中,getSupportFragmentManager
2、主要的操作都是FragmentTransaction的方法
FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务
transaction.add()
往Activity中添加一个Fragment
transaction.remove()
从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),
这个Fragment实例将会被销毁。
transaction.replace()
使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transaction.hide()
隐藏当前的Fragment,仅仅是设为不可见,并不会销毁
transaction.show()
显示之前隐藏的Fragment
detach()
会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
Activity.java
import java.util.List;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.Spinner;import android.widget.TextView;import com.example.R;/** * 俩个Fragment切换 AFragment BFragment * */public class MainActivity extends FragmentActivity implements OnClickListener {private static final int TYPE_201 = 0;private static final int TYPE_202 = 1;private FragmentManager fragmentManager;private FragmentTransaction transaction;private Fragment fragment;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bizData(TYPE_201);// 设置默认的Fragment}public void bizData(int type) {switch (type) {case TYPE_201:fragment = new AFragment();break;case TYPE_202:fragment = new BFragment();break;default:break;}fragmentManager = getSupportFragmentManager();transaction = fragmentManager.beginTransaction().setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);transaction.(R.id.container, fragment);transaction.commit();}@Overridepublic void onClick(View arg0) {switch (arg0.getId()) {case R.id.tv_continuing:bizData(TYPE_201);break;case R.id.tv_exam:bizData(TYPE_202);break;default:break;}}}
activity_main.xml 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:background="#ffffff" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#F39801" android:orientation="horizontal" > <TextView android:id="@+id/tv_continuing" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:text="继续教育" android:textColor="#ffffff" android:textSize="13sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#ffffff" /> <TextView android:id="@+id/tv_exam" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:text="考试培训" android:textColor="#ffffff" android:textSize="13sp" /> </LinearLayout> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class AFragment extends Fragment {private View view;public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_a, container, false);return view;}}
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class BFragment extends Fragment {private View view;public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_b, container, false);return view;}}
0 0
- android Fragment FragmentActivity 选项卡 replace add hide show
- [Android]Fragment加载替换add,show,hide,replace方法
- Fragment add、show、hide、replace、remove、
- Fragment show hide,replace
- Fragment加载替换add,show,hide,replace方法
- Fragment加载轮换add,show,hide,replace方法
- Fragment生命周期及add、replace、remove、hide&show方法分析
- Fragment加载替换add,show,hide,replace方法
- Fragment加载替换add,show,hide,replace方法
- Android Fragment 真正的完全解析(上) add remove replace detach attach hide show
- Android Fragment 真正的完全解析(上) add remove replace detach attach hide show
- android碎片的replace()和add()、hide()、show()的区别
- Fragment add hide show方式
- Fragment切换 保存状态(add show hide)
- Fragment的show,add,hide使用
- 一道面试题:两个Fragment在replace/add 和 hide/show不同情况下来回跳转生命周期的变化
- FragmentTransition replace add show hide方法的使用和差异
- 【Fragment】fragment之间的切换,add、show、hide
- 《第一行代码》笔记
- VBA教程初级(三):变量的声明与作用域
- QGis二次开发基础 -- 根据属性查询要素
- Android框架之网络通信Volley
- Wiseteam 12核工作站GE180 仅29900元
- android Fragment FragmentActivity 选项卡 replace add hide show
- Android中如何做到Service被关闭后又自动启动
- EventBus使用教程
- Java内部类总结
- c++第一次实验
- php中的curl使用入门教程和常见用法实例
- 利用gradle修改打包或者运行时App工程生成的app文件名
- 第一次c++上机实验
- 记录一下快排的思想