上次我们写的代码还有一个缺点,就是我们在点击返回键的时候是直接退出,而不是返回上一个view.我们可以使用栈,把fragment放到会退栈当中。
修改MyMenuFragment类中的 onItemClick()方法。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
final FragmentManager manager=getActivity().getSupportFragmentManager();
MyContentFragment old=(MyContentFragment) manager.findFragmentByTag("fragment"+position);
//怎么知道要退到哪个地方呢?那就要添加一个回退栈的事件监听器
manager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
//得到回退栈的数量
int count =manager.getBackStackEntryCount();
for(int i=0;i<count;i++){
Log.i("zhangxin", manager.getBackStackEntryAt(i).getName());
}
}
});
if(old==null){
MyContentFragment fragment=MyContentFragment.getInstance(position);
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.fl_content,fragment, "fragment"+position);
checkOldFragments(position, manager,
transaction);
//把当前的fragment放入会退栈,并取名字为"fragment"+position。
transaction.addToBackStack("fragment"+position);
transaction.commit();
}else{
FragmentTransaction transaction2=manager.beginTransaction();
checkOldFragments(position, manager, transaction2);
transaction2.commit();
}
}
但是你会发现每次回退都打印多个信息。
这是因为我们每次点击的时候都会为我们的fragment绑定一个监听,点击了三次我们就绑定了三个。现在修改如下,我们的监听机制不写在onItemClick()方法中,而写在onCreate()中,因为ListFragment只创建一次,所以onCreate()只执行一次,在该方法里只为他添加一个回退栈
修改 MyMenuFragment如下:
package com.tz.katefragement_repalce;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
// ListFragment :Fragment+ListView
//ontach---oncreat---onCreatView--onActivityCreate
public class MyMenuFragment extends ListFragment implements OnItemClickListener {
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday", };
private FragmentManager manager;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, days);
this.setListAdapter(adapter);
getListView().setOnItemClickListener(this);
super.onActivityCreated(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
manager = getActivity().getSupportFragmentManager();
// 怎么知道要退到哪个地方呢?那就要添加一个回退栈的事件监听器
// 因为ListFragment只创建一次,所以onCreate()只执行一次,在该方法里只为他添加一个回退栈
manager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
// 得到回退栈的数量
int count = manager.getBackStackEntryCount();
for (int i = 0; i < count; i++) {
Log.i("zhangxin", manager.getBackStackEntryAt(i).getName());
}
}
});
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
MyContentFragment old = (MyContentFragment) manager
.findFragmentByTag("fragment" + position);
if (old == null) {
MyContentFragment fragment = MyContentFragment
.getInstance(position);
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fl_content, fragment, "fragment" + position);
checkOldFragments(position, manager, transaction);
// 把当前的fragment放入会退栈,并取名字为"fragment"+position。
transaction.addToBackStack("fragment" + position);
transaction.commit();
} else {
FragmentTransaction transaction2 = manager.beginTransaction();
checkOldFragments(position, manager, transaction2);
transaction2.commit();
}
}
private void checkOldFragments(int position, FragmentManager manager,
FragmentTransaction transaction2) {
for (int i = 0; i < days.length; i++) {
MyContentFragment mcf2 = (MyContentFragment) manager
.findFragmentByTag("fragment" + i);
if (mcf2 != null) {
if (i == position) {
transaction2.show(mcf2);
} else {
transaction2.hide(mcf2);
}
}
}
}
}