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

Android-Fragment(2)-在Activity中动态添加删除

2017-05-03 11:42 267 查看

1. 前言

接着上一篇

2. 开发步骤

1. 取得FragmentTransaction实例

FragmentManager manager=getFragmentManager();
FragmentTransaction           transaction=manager.beginTransaction();


2. 调用add方法添加fragment到布局

transaction.add(R.id.main, first);


这里R.id.main是布局的地址,自己可以在activity_main.xml设置

android:id="@+id/main"


first是所创建的Fragment类的实例

Fragment first;
first =new FirstFragment();


3. 调用commit()方法

transaction.commit();


为了给Activity提交改变事物,必须调用commit方法

到这里就可以正常的添加fragment了。

注意:每个事务(提交给Activity的每个变化称为一个事务)都需要一个取得一次fragmenttransaction对象

当然,我们的每次变化都可以用transaction.addToBackStack(null);方法保存到Activity的back stack,这是一个栈,可以返回你之前存入其中的状态

当fragment移除时,fragment会被销毁,而在commit()提交之前使用addToBackStack,他只会停止,如果导航回来,fragment就会恢复。

3. 代码;

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
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="com.example.second.MainActivity" >
</RelativeLayout>


firstfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FF0000" >
<TextView
android:layout_width="match_parent"
android:layout_height="146dp"
android:text="this is first fragment" />
</FrameLayout>


secondfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/a
b8df
ndroid"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFF11"
android:layout_marginTop="300dp"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="this is second fragment" />
</FrameLayout>


FirstFragment.java

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


SecondFragment.java

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


MainActivity.java

public class MainActivity extends ActionBarActivity {
FirstFragment first;
SecondFragment second;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first =new FirstFragment();
second =new SecondFragment();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
FragmentManager manager=getFragmentManager();
transaction=manager.beginTransaction();

int id = item.getItemId();
if (id == R.id.add_first) {
transaction.add(R.id.main, first);
transaction.addToBackStack(null);

}
else if(id==R.id.add_second){
transaction.add(R.id.main, second);
transaction.addToBackStack(null);//Log下显示BackStack元素个数

}
else if(id==R.id.remove_first){
//transaction.replace(R.id.main, first);
transaction.remove(first);
transaction.addToBackStack(null);

}
else if(id==R.id.remove_second){
transaction.remove(second);
transaction.addToBackStack(null);

}
transaction.commit();
int TAG = getFragmentManager().getBackStackEntryCount();
Log.d("TAG",String.valueOf(TAG));
if(TAG>0)
Log.d("TAG",getFragmentManager().getBackStackEntryAt(TAG-1).toString());
return true;
}
}


效果图:





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