Fragment 实现tab页卡切换并保存界面状态,动态添加Fragment

    
Fragment实现tab页卡切换,切换后并保存原先的界面状态,比如我在第一个页面输入了某些字符,突然忘了要切换到其他页面看看在回来继续输入,这时就需要对先前输入的字符进行保存,当从其他界面界面回到这里时信息就还会存在。在这个Demo里用的其实是在切换时把其他界面隐藏起来,而不是去销毁它,这样数据就不会丢失。效果图如下:

就像上图那样,我在订单界面输入字符,突然要切换到其他界面然后再切换回来,数据要依然存在,代码如下:

package com.example.myandroid.Fragment;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.myandroid.R;
import com.example.myandroid.Fragment.ListViewFragment.MyListFragment;

public class TabFragmentActivity extends Activity {
	private RadioGroup mRadioGroup;
	TitleFragment titlefragment;
	ListFragment listFragment;
	ConentFragment contentfragment;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tabfragment_layout);

		mRadioGroup = (RadioGroup) findViewById(R.id.tab_radiogroup);
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				FragmentTransaction fragmentTransaction = getFragmentManager()
						.beginTransaction();
				hideFragment(fragmentTransaction);//隐藏所有
				switch (checkedId) {
				case R.id.tab_radiobutton1:
					if(titlefragment==null){
						titlefragment=new TitleFragment();
						fragmentTransaction.add(R.id.tabframent_conent, titlefragment);
					}
					else{
						fragmentTransaction.show(titlefragment);
					}
					break;
				case R.id.tab_radiobutton2:
					if(listFragment==null){
						listFragment=new ListFragment();
						fragmentTransaction.add(R.id.tabframent_conent, listFragment);
					}
					else{
						fragmentTransaction.show(listFragment);
					}
					break;
				case R.id.tab_radiobutton3:
					if(contentfragment==null){
						
						contentfragment=new ConentFragment();
						fragmentTransaction.add(R.id.tabframent_conent, contentfragment);
					}
					else{
						fragmentTransaction.show(contentfragment);
					}
					break;
				}
				fragmentTransaction.commit();
			}
		});
		RadioButton radio = (RadioButton) mRadioGroup.getChildAt(2);
		radio.toggle();// 默认选中第三项

	}

	/**
	 * 将所有的fragment设置为隐藏
	 * @param transaction
	 */
	private void hideFragment(FragmentTransaction transaction) {
		if (titlefragment!= null) {
			transaction.hide(titlefragment);
		}
		if (listFragment != null) {
			transaction.hide(listFragment);
		}
		if (contentfragment != null) {
			transaction.hide(contentfragment);
		}
	}
}
activity 的布局xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RadioGroup
        android:id="@+id/tab_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="#ffffff"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/tab_radiobutton1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_tab"
            android:gravity="center"
            android:paddingLeft="0dp"
            android:text="首页"
            android:textColor="@drawable/selector_color" />

        <RadioButton
            android:id="@+id/tab_radiobutton2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_tab"
            android:gravity="center"
            android:paddingLeft="0dp"
            android:text="客服"
            android:textColor="@drawable/selector_color" />

        <RadioButton
            android:id="@+id/tab_radiobutton3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawablePadding="5dp"
            android:drawableTop="@drawable/selector_tab"
            android:gravity="center"
            android:paddingLeft="0dp"
            android:text="订单"
            android:textColor="@drawable/selector_color"
            android:textSize="15sp" />
    </RadioGroup>

    <FrameLayout
        android:id="@+id/tabframent_conent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/tab_radiogroup" />

</RelativeLayout>

然后下面依次是三个Fragment:

TitleFragment:

package com.example.myandroid.Fragment;

import com.example.myandroid.R;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TitleFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
        		Bundle savedInstanceState) {
        	 // 第一个参数是这个Fragment将要显示的界面布局,第二个参数是这个Fragment所属的Activity,第三个参数是决定此fragment是否附属于Activity   
			return inflater.inflate(R.layout.titlefragment_layout, container,false);
        }
        public static Fragment newInstance(){
    		Fragment titleFragment = new TitleFragment();
    		
//    		Bundle bundle = new Bundle();
//    		bundle.putString("MESSAGE", message);
//    		receiverFragment.setArguments(bundle);
    		
    		return titleFragment;
    	}
}
<?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:background="@android:color/holo_orange_dark"
    android:orientation="vertical" >

    <Button
        android:id="@+id/previous_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/pre_but" />

    <Button
        android:id="@+id/next_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/next_but" />

    <Button
        android:id="@+id/exit_button"

ConentFragment:

package com.example.myandroid.Fragment;

import com.example.myandroid.R;
import com.example.myandroid.Fragment.Interaction2.ReceiverFragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ConentFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
    	
    	
    	return inflater.inflate(R.layout.conent_layout,container, false);
    }
    public static Fragment newInstance(){
		Fragment containerFragment = new ConentFragment();
		
//		Bundle bundle = new Bundle();
//		bundle.putString("MESSAGE", message);
//		receiverFragment.setArguments(bundle);
		
		return containerFragment;
	}
}
<?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/show_message"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:background="@android:color/holo_blue_dark"
        android:text="show_message" 
        android:gravity="center"/>
    <EditText  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

ListFragment:

package com.example.myandroid.Fragment;

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

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.myandroid.R;

public class ListFragment extends Fragment {
      private ListView mListView;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
    	View v=inflater.inflate(R.layout.listfragment_layout,container,false);
    	
    	mListView=(ListView) v.findViewById(R.id.listfragment_listview);
    	

		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> item1 = new HashMap<String, Object>();
		item1.put("title", "何记花甲");
		item1.put("content", "【成都等】新石器烤肉100元新石器烤肉现金抵用券1张,全场通");
		item1.put("icon", R.drawable.list1);
		list.add(item1);

		HashMap<String, Object> item2 = new HashMap<String, Object>();
		item2.put("title", "新石器烤肉");
		item2.put("content", "【成都等】新石器烤肉100元新石器烤肉现金抵用券1张,全场通");
		item2.put("icon", R.drawable.list2);
		list.add(item2);

		HashMap<String, Object> item3 = new HashMap<String, Object>();
		item3.put("title", "九里烤肉");
		item3.put("content", "【成都等】新石器烤肉100元新石器烤肉现金抵用券1张,全场通");
		item3.put("icon", R.drawable.list3);
		list.add(item3);

		String[] from = new String[] { "title", "content", "icon" };// 参数from,是一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称
		int[] to = new int[] { R.id.textview0010, R.id.textview0011,
				R.id.imageview001 };// 参数to,是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应。
		SimpleAdapter adapters = new SimpleAdapter(getActivity(), list,
				R.layout.activity_hashmap_listview, from, to);
		mListView.setAdapter(adapters);
    	
    	
    	return v;
    }
    public static Fragment newInstance(){
		Fragment listFragment = new ListFragment();
		
//		Bundle bundle = new Bundle();
//		bundle.putString("MESSAGE", message);
//		receiverFragment.setArguments(bundle);
		
		return listFragment;
	}
}
<?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" >
    
  <ListView  
      android:id="@+id/listfragment_listview"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
</LinearLayout>

动态添加Fragment 是在代码里面实现的,而静态添加直接在布局里面写死,一般在项目里面的话都是采用动态的方法。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值