Fragment切换的时候,保存数据状态(执行onResume),不重新加载


关键代码来自:http://www.cnblogs.com/tiantianbyconan/p/3360938.html



import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

public class FixturesChildActivity extends Activity {

	private RadioGroup mRGSwitch;// 单选按钮组
	public List<Fragment> fragments = new ArrayList<Fragment>();
	private ArrayList<View> viewLines = new ArrayList<View>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//获取RadioButton下高亮的黄线
		View line1 = this.findViewById(R.id.fixtures_child_left_radio_button_line);
		View line2 = this.findViewById(R.id.fixtures_child_chatroom_button_line);
		View line3 = this.findViewById(R.id.fixtures_child_right_radio_button_line);
		View line4 = this.findViewById(R.id.fixtures_child_caipiao_radio_button_line);
		
		//加入集合
		viewLines.add(line1);
		viewLines.add(line2);
		viewLines.add(line3);
		viewLines.add(line4);

		fragments.add(new TabAFm());
		fragments.add(new TabBFm());
		fragments.add(new TabCFm());
		fragments.add(new TabDFm());
		fragments.add(new TabEFm());

		mRGSwitch = (RadioGroup) findViewById(R.id.fixtures_child_radio_group);
		new FragmentsAdapter(FixturesChildActivity.this, fragments,
				R.id.fixtures_child_view_child_view, mRGSwitch);
	}

	/**
	 * fragment适配器
	 */
	public class FragmentsAdapter implements RadioGroup.OnCheckedChangeListener {
		private List<Fragment> fragments; // 一个tab页面对应一个Fragment
		private RadioGroup rgs; // 用于切换tab
		private Activity mActivity; // Fragment所属的Activity
		private int fragmentContentId; // Activity中所要被替换的区域的id

		private int currentTab; // 当前Tab页面索引

		public FragmentsAdapter(Activity fragmentActivity,
				List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) {
			this.fragments = fragments;
			this.rgs = rgs;
			this.mActivity = fragmentActivity;
			this.fragmentContentId = fragmentContentId;

			// 默认显示第一页
			FragmentTransaction ft = fragmentActivity.getFragmentManager()
					.beginTransaction();
			ft.add(fragmentContentId, fragments.get(0));
			ft.commit();

			rgs.setOnCheckedChangeListener(this);

		}

		@Override
		public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
			for (int i = 0; i < rgs.getChildCount(); i++) {
				if (rgs.getChildAt(i).getId() == checkedId) {
					
					//把前一个RadioButton文本颜色设置为白色
					((RadioButton) rgs.getChildAt(getCurrentTab()))
							.setTextColor(getResources()
									.getColor(R.color.white));
					//把本次点击RadioButton文本颜色设置为蓝色
					((RadioButton) rgs.getChildAt(i))
							.setTextColor(getResources().getColor(R.color.blue));
					
					viewLines.get(getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.transparent));
					viewLines.get(i).setBackgroundColor(getResources().getColor(R.color.light_yellow));

					Fragment fragment = fragments.get(i);
					FragmentTransaction ft = mActivity.getFragmentManager()
							.beginTransaction();

					getCurrentFragment().onPause(); // 暂停当前tab

					if (fragment.isAdded()) {
						fragment.onResume(); // 启动目标tab的onResume()
					} else {
						ft.add(fragmentContentId, fragment);
					}
					showTab(i); // 显示目标tab
					ft.commit();
				}
			}
		}

		/**
		 * 切换tab
		 * 
		 * @param idx
		 */
		private void showTab(int idx) {
			for (int i = 0; i < fragments.size(); i++) {
				Fragment fragment = fragments.get(i);
				FragmentTransaction ft = mActivity.getFragmentManager()
						.beginTransaction();

				if (idx == i) {
					ft.show(fragment);
				} else {
					ft.hide(fragment);
				}
				ft.commit();
			}
			currentTab = idx; // 更新目标tab为当前tab
		}

		public int getCurrentTab() {
			return currentTab;
		}

		public Fragment getCurrentFragment() {
			return fragments.get(currentTab);
		}

	}
}



另一实现方式:http://www.cnblogs.com/over140/p/3362047.html

此外可以,将Fragment放置于ViewPager中,自动保存其状态。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值