大家都知道可以使用TabHost来实现, 不过这种实现已经是被deprecated, 其实是可以通过Fragment来实现, 不过如果是Fragment的话好像只能每次new一个fragment,这样感觉不太好, 按常理如果是以前有创建过fragment,下一次应该还是显示那个fragment实例。 通过google得知可以通过FragmentTransaction的attach和detach来实现。 下面贴下代码。
先detach所有可能的fragment,然后add或attach对应的fragment。
package com.kissbb.activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import com.androidquery.AQuery;
import com.androidquery.util.AQUtility;
import com.kissbb.R;
import com.kissbb.api.GoodsAPI;
import com.kissbb.fragment.CartFragment;
import com.kissbb.fragment.KuaigouListFragment;
import com.kissbb.fragment.MoreFragment;
import com.kissbb.fragment.MyOrderFragment;
import com.kissbb.model.KuaigouGroup;
import de.greenrobot.event.util.AsyncExecutor;
import java.util.List;
public class MainActivity extends FragmentActivity implements CompoundButton.OnCheckedChangeListener {
private RadioButton rbMenuIndex;
private RadioButton rbMenuCart;
private RadioButton rbMenuMyOrder;
private RadioButton rbMenuMore;
private View rlMenuIndex;
private View rlMenuCart;
private View rlMenuMyOrder;
private View rlMenuMore;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AQuery aq = new AQuery(this);
rbMenuIndex = (RadioButton) findViewById(R.id.rbMenuIndex);
rbMenuIndex.setOnCheckedChangeListener(this);
rbMenuCart = (RadioButton) findViewById(R.id.rbMenuCart);
rbMenuCart.setOnCheckedChangeListener(this);
rbMenuMyOrder = (RadioButton) findViewById(R.id.rbMenuMyOrder);
rbMenuMyOrder.setOnCheckedChangeListener(this);
rbMenuMore = (RadioButton) findViewById(R.id.rbMenuMore);
rbMenuMore.setOnCheckedChangeListener(this);
rlMenuCart = findViewById(R.id.rlMenuCart);
rlMenuIndex = findViewById(R.id.rlMenuIndex);
rlMenuMore = findViewById(R.id.rlMenuMore);
rlMenuMyOrder = findViewById(R.id.rlMenuMyOrder);
//
rbMenuIndex.setChecked(true);
// CheckBox cbIndex = (CheckBox) findViewById(R.id.rbMenuIndex);
// aq.id(R.id.rbMenuIndex).
// aQuery.id(R.id.button).clicked(this, "buttonClicked");
}
public void buttonClicked(View view) {
AsyncExecutor.create().execute(new AsyncExecutor.RunnableEx() {
@Override
public void run() throws Exception {
List<KuaigouGroup> kuaigouList = new GoodsAPI(MainActivity.this).getKuaigouList();
AQUtility.debug("List<KuaigouGroup> is not null: " + kuaigouList.size());
// UserLoginResult userLoginResult = new GoodsAPI(MainActivity.this).userLogin("[email protected]", "87200795");
// AQUtility.debug("UserLoginResult is not null: " + (userLoginResult != null));
// new UserAPI(MainActivity.this).userRegister("[email protected]", "12345678", 111, 111, 11);
}
});
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
resetAllRadioButtons();
buttonView.setChecked(true);
FragmentManager fragmentManager = getSupportFragmentManager();
KuaigouListFragment kuaigouListFragment = (KuaigouListFragment) fragmentManager.findFragmentByTag(String.valueOf(R.id.rbMenuIndex));
CartFragment cartFragment = (CartFragment) fragmentManager.findFragmentByTag(String.valueOf(R.id.rbMenuCart));
MyOrderFragment myOrderFragment = (MyOrderFragment) fragmentManager.findFragmentByTag(String.valueOf(R.id.rbMenuMyOrder));
MoreFragment moreFragment = (MoreFragment) fragmentManager.findFragmentByTag(String.valueOf(R.id.rbMenuMore));
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
/** Detaches all fragments if exists */
if (kuaigouListFragment != null) {
fragmentTransaction.detach(kuaigouListFragment);
}
if (cartFragment != null) {
fragmentTransaction.detach(cartFragment);
}
if (myOrderFragment != null) {
fragmentTransaction.detach(myOrderFragment);
}
if (moreFragment != null) {
fragmentTransaction.detach(moreFragment);
}
int buttonId = buttonView.getId();
switch (buttonId) {
case R.id.rbMenuIndex:
rlMenuIndex.setBackgroundResource(R.drawable.footover);
if (kuaigouListFragment == null) {
/** Create AndroidFragment and adding to fragmenttransaction */
fragmentTransaction.add(R.id.flContent, new KuaigouListFragment(), String.valueOf(R.id.rbMenuIndex));
} else {
/** Bring to the front, if already exists in the fragmenttransaction */
fragmentTransaction.attach(kuaigouListFragment);
}
break;
case R.id.rbMenuCart:
rlMenuCart.setBackgroundResource(R.drawable.footover);
if (cartFragment == null) {
/** Create AndroidFragment and adding to fragmenttransaction */
fragmentTransaction.add(R.id.flContent, new CartFragment(), String.valueOf(R.id.rbMenuCart));
} else {
/** Bring to the front, if already exists in the fragmenttransaction */
fragmentTransaction.attach(cartFragment);
}
break;
case R.id.rbMenuMore:
rlMenuMore.setBackgroundResource(R.drawable.footover);
if (moreFragment == null) {
/** Create AndroidFragment and adding to fragmenttransaction */
fragmentTransaction.add(R.id.flContent, new MoreFragment(), String.valueOf(R.id.rbMenuMore));
} else {
/** Bring to the front, if already exists in the fragmenttransaction */
fragmentTransaction.attach(moreFragment);
}
break;
case R.id.rbMenuMyOrder:
rlMenuMyOrder.setBackgroundResource(R.drawable.footover);
if (myOrderFragment == null) {
/** Create AndroidFragment and adding to fragmenttransaction */
fragmentTransaction.add(R.id.flContent, new MyOrderFragment(), String.valueOf(R.id.rbMenuMyOrder));
} else {
/** Bring to the front, if already exists in the fragmenttransaction */
fragmentTransaction.attach(myOrderFragment);
}
break;
}
fragmentTransaction.commit();
}
}
private void resetAllRadioButtons() {
rbMenuCart.setChecked(false);
rbMenuIndex.setChecked(false);
rbMenuMore.setChecked(false);
rbMenuMyOrder.setChecked(false);
rlMenuMore.setBackgroundResource(R.drawable.footline);
rlMenuIndex.setBackgroundResource(R.drawable.footline);
rlMenuMyOrder.setBackgroundResource(R.drawable.footline);
rlMenuCart.setBackgroundResource(R.drawable.footline);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(isTaskRoot()){
//clean the file cache with advance option
long triggerSize = 3000000; //starts cleaning when cache size is larger than 3M
long targetSize = 2000000; //remove the least recently used files until cache size is less than 2M
AQUtility.cleanCacheAsync(this, triggerSize, targetSize);
}
}
}
但是如果attach已经存在的fragment时 还是会调用Fragment的onCreateView方法, 这样的话会让整个fragment重新绘制UI, 如果是里面是个ListView的话,因为需要重新所以ListView滚动那个位置就丢失了,如何能避免这个问题呢? 那就要保存Fragment onCreateView中生成的view, 代码如下。
package com.kissbb.fragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ListView;
import com.androidquery.util.AQUtility;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.kissbb.R;
import com.kissbb.adapter.KuaigouListAdapter;
import com.kissbb.api.GoodsAPI;
import com.kissbb.model.KuaigouGroup;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.util.AsyncExecutor;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: Denny
* Date: 13-7-17
* Time: 下午11:00
* To change this template use File | Settings | File Templates.
*/
public class KuaigouListFragment extends Fragment {
private GoodsAPI goodsAPI;
private PullToRefreshListView ptrlvKuaigou;
private int imgHeight;
private List<KuaigouGroup> kuaigouGroupList;
private View rootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
goodsAPI = new GoodsAPI(getActivity());
DisplayMetrics dm = getResources().getDisplayMetrics();
int widthPixels = dm.widthPixels;
imgHeight = (int) (182f/713*widthPixels);
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
AQUtility.debug("onCreateView", "KuaigouListFragment");
if (rootView == null) {
rootView = inflater.inflate(R.layout.kuaigoulist_fragment, container, false);
ptrlvKuaigou = (PullToRefreshListView) rootView.findViewById(R.id.ptrlvKuaigou);
ptrlvKuaigou.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
startToPrepareData();
}
});
if (kuaigouGroupList != null) {
}else{
ptrlvKuaigou.setVisibility(View.INVISIBLE);
startToPrepareData();
}
}
//缓存的rootView需要判断是否已经被加过parent, 如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null) {
parent.removeView(rootView);
}
return rootView;
}
private void startToPrepareData() {
AsyncExecutor.create().execute(new AsyncExecutor.RunnableEx() {
@Override
public void run() throws Exception {
getKuaigouListData();
}
});
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); //To change body of overridden methods use File | Settings | File Templates.
AQUtility.debug("onActivityCreated", "KuaigouListFragment");
}
public void onEventMainThread(KuaigouDataEvent event) {
ptrlvKuaigou.setVisibility(View.VISIBLE);
kuaigouGroupList = event.kuaigouList;
ptrlvKuaigou.setAdapter(new KuaigouListAdapter(kuaigouGroupList, getActivity(), imgHeight));
ptrlvKuaigou.onRefreshComplete();
}
private void getKuaigouListData() {
List<KuaigouGroup> kuaigouList = goodsAPI.getKuaigouList();
EventBus.getDefault().post(new KuaigouDataEvent(kuaigouList));
}
public class KuaigouDataEvent{
public List<KuaigouGroup> kuaigouList;
public KuaigouDataEvent(List<KuaigouGroup> kuaigouList) {
this.kuaigouList = kuaigouList;
}
}
}
特别注意这段代码, 需要先把view从parent中删掉 才能加回去。
//缓存的rootView需要判断是否已经被加过parent, 如果有parent需要从parent删除,要不然会发生这个rootview已经有parent的错误。
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null) {
parent.removeView(rootView);
}