您的位置:首页 > 移动开发 > Android开发

Android Fragment的使用 二 事务函数详解

2017-04-16 12:58 489 查看
我先把需要将的函数摆列出来

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维护。

attach()

重建view视图,附加到UI上并显示。

transatcion.commit()//提交一个事务

使用例子如下:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.add(R.id.id_content,new FragmentOne(),"ONE");
tx.commit();

}
}

public class FragmentOne extends Fragment implements View.OnClickListener {

private Button button;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_one,container,false);
button = (Button) view.findViewById(R.id.id_fragment_one_btn);
button.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v){
FragmentTwo fTwo = new FragmentTwo();
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.replace(R.id.id_content,fTwo,"TWO");
tx.addToBackStack(null);
tx.commit();

}

}

public class FragmentTwo extends Fragment implements View.OnClickListener {

private Button button;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_two,container,false);
button = (Button) view.findViewById(R.id.id_fragment_two_btn);
button.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v){
FragmentThree fThree = new FragmentThree();
FragmentManager fm = getFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.hide(this);
tx.add(R.id.id_content,fThree,"THREE");
tx.addToBackStack(null);
tx.commit();

}

}

public class FragmentThree extends Fragment implements View.OnClickListener {

private Button button;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_three,container,false);
button = (Button) view.findViewById(R.id.id_fragment_three_btn);
button.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v){
Toast.makeText(getActivity(),"three",Toast.LENGTH_SHORT).show();

}

}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="android.com.myapplication.MainActivity">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/id_content"></FrameLayout>

</RelativeLayout>

fragment_one

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:id="@+id/id_fragment_one_btn"
android:layout_gravity="center"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

fragment_two

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:id="@+id/id_fragment_two_btn"
android:layout_gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

fragment_three

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:id="@+id/id_fragment_three_btn"
android:layout_gravity="center"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>


我们在三个Fragment的输入栏都写入了数据,当我们从FragmentThree按下back键,回退到FragmentTwo,发现这输入栏还存在之前的数据,但再按下back键,FragmentOne的输入栏没有数据。

这是因为FragmentOne的replace将之前FragmentOne销毁了,然后添加了FragmenTwo,但在FragmentTwo使用了hide将FragmentTwo隐藏,将FragmentThree添加下来。

下期将Fragment与Activity的交互。

再见
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: