虽然网上有很多文章都讲解了fragment切换改如何保存其状态,但是很多篇幅个人感觉不够详细,所以
自己整理了一下。
有多种方法,这里介绍两种,一种是在fragment里判断是否已经加载过当前fragment,还有一种是在其
所属的Activity里进行判断。
首先介绍第一种:
//在onCreateView方法里,使用传参过来的view或自己写一个view都可以,我这里是自己写了一个
if (view == null) {
view = inflater.inflate(R.layout.fg_homepage, null);
}
else {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
}
所谓fragment就是我们Activity中将布局,如FramLayout、LinearLayout等布局容器放入Activity布局里,将相应fragment放入容器里,这里我们使用FramLayout
MainActivity 布局如下:
<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">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toRightOf="@+id/tv"
android:text="@string/hello_world" />
<FrameLayout
android:id="@+id/mFramLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tv" />
</RelativeLayout>
两个fragment分别为(PS: fragment1里面放入了ListView,这样方便我们看到切换后状态报错的效果)
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragmentOne" />
<ListView
android:id="@+id/mListview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragmentTwo" />
</LinearLayout>
ListView的适配器布局item_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/item_textview"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/hello_world" />
</LinearLayout>
两个Fragment类(过于简单,实在不是想贴出来的~~~)
FragmentOne
public class FragmentOne extends Fragment {
View view;
private ListView mListview;
private mAdapter mTestAdapter;
private List<String> lists;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment1, null);
mListview = (ListView) view.findViewById(R.id.mListview);
}
initView();
return view;
}
private void initView() {
getData();
mTestAdapter = new mAdapter(lists, getActivity());
mListview.setAdapter(mTestAdapter);
}
private void getData() {
lists = new ArrayList<String>();
for (int i = 0; i < 50; i++) {
lists.add("test");
}
}
}
FragmentTwo
public class FragmentTwo extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment2, null);
}
return view;
}
}
还有一个adapter。。。。。
package com.lee.testfragmentadd;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class FragmentOne extends Fragment {
View view;
private ListView mListview;
private mAdapter mTestAdapter;
private List<String> lists;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment1, null);
mListview = (ListView) view.findViewById(R.id.mListview);
}
initView();
return view;
}
private void initView() {
getData();
mTestAdapter = new mAdapter(lists, getActivity());
mListview.setAdapter(mTestAdapter);
}
private void getData() {
lists = new ArrayList<String>();
for (int i = 0; i < 50; i++) {
lists.add("test");
}
}
}
最后是我们的主要代码,MainActivity(PS:这里的Fragment之类的东西使用的时V4包,如果想使用新包可自行导入替换)。
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener {
private TextView tv, tv2;
private FrameLayout mFramLayout;
private FragmentOne fragmentOne;
private FragmentTwo fragmentTwo;
private Fragment mFragment;
FragmentTransaction fTransaction = getSupportFragmentManager().beginTransaction();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
initView();
// 实例化两个fragment
fragmentOne = new FragmentOne();
fragmentTwo = new FragmentTwo();
// 先获取FragmentManager,将activity_main.xml布局里的FramLayout放入fragment管理器内并提交
getSupportFragmentManager().beginTransaction().add(R.id.mFramLayout, fragmentOne).commit();
getSupportFragmentManager().beginTransaction().add(R.id.mFramLayout, fragmentTwo).commit();
// 初始化点击事件
initListener();
}
private void initListener() {
tv.setOnClickListener(this);
tv2.setOnClickListener(this);
}
private void initView() {
tv = (TextView) findViewById(R.id.tv);
tv2 = (TextView) findViewById(R.id.tv2);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv:
// 当用户点击后将其它Fragment隐藏,hide(fragment.class),显示当前点击对应的fragment,show(fragment.class)
// 并且commit
getSupportFragmentManager().beginTransaction().hide(fragmentTwo).show(fragmentOne).commit();
// 这里时为了防止用户点击多次当前选项卡,导致每次都调用getSupportFragmentManager().beginTransaction()方法
// 所以,如果改选项卡不需要切换时,使其不可点击
tv.setOnClickListener(null);
tv2.setOnClickListener(this);
Log.e("FUCK", "tv");
break;
case R.id.tv2:
// 同上
getSupportFragmentManager().beginTransaction().hide(fragmentOne).show(fragmentTwo).commit();
// 同上
tv2.setOnClickListener(null);
tv.setOnClickListener(this);
Log.e("FUCK", "tv2");
break;
default:
break;
}
}
}
总之,就是通过将fragment添加进fragmentmanager内,然后通过hide与show方法来隐藏与显示对应的
fragment。这里值得注意的是,getSupportFragmentManager().beginTransaction()是一个即时的获取当
前fragment管理器操作的对象,同Bean一样,不要直接定义一个FragmentTransaction fTransaction=
getSupportFragmentManager().beginTransaction()
代码注释已经讲得很清楚了,就不上传源项目了