0

可能是重复的问题。但我找不到我的问题的解决方案

我有一个带有 3 个标签的 TabHost。比方说tabA,,tabB。当我切换到另一个选项卡(即 tabA 或 tabB)时tabC,我做了很多视图更改,例如按钮文本更改、editText 更改,然后我失去了所有视图更改。我所有的视图值(按钮和editTexts),它将我的所有视图重置为默认的XML文件值。我不希望这种情况发生。我只想如何保存我的观点状态。我不能在标签更改时丢失这个。tabCtabC

我试过这个链接但失败了。

这是我的代码

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    Toast.makeText(getActivity(), "onCreateView", Toast.LENGTH_SHORT)
            .show();

    if (container == null) {

        return null;
    }

    View vi = inflater.inflate(R.layout.settings, container, false);

    btnInsert = (Button) vi.findViewById(R.id.btnInsert);
    btnInsert.setOnClickListener(this);
    btnPosition = (Button) vi.findViewById(R.id.btnPosition);
    btnPosition.setOnClickListener(this);
    txtPosition = (TextView) vi.findViewById(R.id.txtPosition);
    txtLogo = (TextView) vi.findViewById(R.id.txtLogo);
    imgLogoPreview = (ImageView) vi.findViewById(R.id.imgLogoPreview);
    imgLogoPreview.setOnClickListener(this);
    edTxtUserText = (EditText) vi.findViewById(R.id.edTxtPreview);
    relLogo = (RelativeLayout) vi.findViewById(R.id.RelLogo);
    relText = (RelativeLayout) vi.findViewById(R.id.RelText);

    logoWheel = (WheelView) vi.findViewById(R.id.wheelLogo);

    logoWheel.setAdapter(new ArrayWheelAdapter<String>(logoWheelList));
    logoWheel.setVisibleItems(4);
    logoWheel.setCurrentItem(1);
    positionWheel = (WheelView) vi.findViewById(R.id.wheelPosition);
    positionWheel.setAdapter(new ArrayWheelAdapter<String>(
            positionWheelTextList));

    // LogoWheel changed listener
    changedListenerLogo = new OnWheelChangedListener() {
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            if (!wheelScrolled) {

            }
        }
    };

    logoWheel.addChangingListener(changedListenerLogo);

    // Wheel scrolled listener
    scrolledListenerLogo = new OnWheelScrollListener() {

        public void onScrollStarts(WheelView wheel) {
            wheelScrolled = true;

        }

        public void onScrollEnds(WheelView wheel) {
            wheelScrolled = false;
            btnInsert.setText(logoWheelList[wheel.getCurrentItem()] + "");
            wheel.setVisibility(View.INVISIBLE);
            if (wheel.getCurrentItem() == 2) {

                txtPosition.setVisibility(View.INVISIBLE);
                btnPosition.setVisibility(View.INVISIBLE);
                relText.setVisibility(View.INVISIBLE);
                relLogo.setVisibility(View.INVISIBLE);

            } else if (wheel.getCurrentItem() == 1) {

                relText.setVisibility(View.VISIBLE);
                relLogo.setVisibility(View.INVISIBLE);
                txtPosition.setVisibility(View.VISIBLE);
                btnPosition.setVisibility(View.VISIBLE);
                btnPosition.setText("Top");
                positionWheel.setAdapter(new ArrayWheelAdapter<String>(
                        positionWheelTextList));
                positionWheel.setVisibleItems(4);
                positionWheel.setCurrentItem(1);

            } else if (wheel.getCurrentItem() == 0) {

                relLogo.setVisibility(View.VISIBLE);
                relText.setVisibility(View.INVISIBLE);
                txtPosition.setVisibility(View.VISIBLE);
                btnPosition.setVisibility(View.VISIBLE);
                btnPosition.setText("Top Left");
                positionWheel.setAdapter(new ArrayWheelAdapter<String>(
                        positionWheelLogoList));
                positionWheel.setVisibleItems(4);
                positionWheel.setCurrentItem(1);

            }
        }
    };

    logoWheel.addScrollingListener(scrolledListenerLogo);

    // /////////////////////Positon Wheel Listners///////////

    // LogoWheel changed listener
    changedListenerPosition = new OnWheelChangedListener() {
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            if (!wheelScrolled) {

            }
        }
    };

    positionWheel.addChangingListener(changedListenerPosition);

    // Wheel scrolled listener
    scrolledListenerPosition = new OnWheelScrollListener() {

        public void onScrollStarts(WheelView wheel) {
            wheelScrolled = true;

        }

        public void onScrollEnds(WheelView wheel) {
            wheelScrolled = false;

            String btnStatus = btnInsert.getText().toString();
            if (btnStatus.equals("Logo")) {
                btnPosition.setText(positionWheelLogoList[positionWheel
                        .getCurrentItem()] + "");

            } else if (btnStatus.equals("Text")) {
                btnPosition.setText(positionWheelTextList[positionWheel
                        .getCurrentItem()] + "");
            }

            wheel.setVisibility(View.INVISIBLE);

        }
    };

    positionWheel.addScrollingListener(scrolledListenerPosition);

    return vi;

}

编辑 这是我的 FragmentActivity 的代码

public class MainActivity extends FragmentActivity implements
    TabHost.OnTabChangeListener {

private TabHost mTabHost;
private HashMap mapTabInfo = new HashMap();
private TabInfo mLastTab = null;

private class TabInfo {
    private String tag;
    private Class clss;
    private Bundle args;
    private Fragment fragment;

    TabInfo(String tag, Class clazz, Bundle args) {
        this.tag = tag;
        this.clss = clazz;
        this.args = args;
    }

}

class TabFactory implements TabContentFactory {

    private final Context mContext;

    /**
     * @param context
     */
    public TabFactory(Context context) {
        mContext = context;
    }

    /**
     * (non-Javadoc)
     * 
     * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
     */
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }

}

/**
 * (non-Javadoc)
 * 
 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
 */
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Step 1: Inflate layout
    setContentView(R.layout.tabs_layout);
    // Step 2: Setup TabHost
    initialiseTabHost(savedInstanceState);
    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); // set
    }
}

/**
 * (non-Javadoc)
 * 
 * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
 */
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
                                                            // selected
    super.onSaveInstanceState(outState);
}

/**
 * Step 2: Setup TabHost
 */
private void initialiseTabHost(Bundle args) {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInfo tabInfo = null;
    MainActivity.addTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec("Tab1").setIndicator("Home",
                    getResources().getDrawable(R.drawable.instructions)),
            (tabInfo = new TabInfo("Tab1", Instructions.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    MainActivity.addTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec("Tab2").setIndicator("Capture",
                    getResources().getDrawable(R.drawable.capture)),
            (tabInfo = new TabInfo("Tab2", Capture.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    MainActivity.addTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec("Tab3").setIndicator("Settings",
                    getResources().getDrawable(R.drawable.settings)),
            (tabInfo = new TabInfo("Tab3", Settings.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    // Default to first tab
    this.onTabChanged("Tab1");
    //
    mTabHost.setOnTabChangedListener(this);
}

/**
 * @param activity
 * @param tabHost
 * @param tabSpec
 * @param clss
 * @param args
 */
private static void addTab(MainActivity activity, TabHost tabHost,
        TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    // Attach a Tab view factory to the spec
    tabSpec.setContent(activity.new TabFactory(activity));
    String tag = tabSpec.getTag();

    // Check to see if we already have a fragment for this tab, probably
    // from a previously saved state. If so, deactivate it, because our
    // initial state is that a tab isn't shown.
    tabInfo.fragment = activity.getSupportFragmentManager()
            .findFragmentByTag(tag);
    if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
        FragmentTransaction ft = activity.getSupportFragmentManager()
                .beginTransaction();
        ft.detach(tabInfo.fragment);
        ft.commit();
        activity.getSupportFragmentManager().executePendingTransactions();
    }

    tabHost.addTab(tabSpec);
}

/**
 * (non-Javadoc)
 * 
 * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
 */
public void onTabChanged(String tag) {
    TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);
    if (mLastTab != newTab) {
        FragmentTransaction ft = this.getSupportFragmentManager()
                .beginTransaction();
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(this,
                        newTab.clss.getName(), newTab.args);
                ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
        ft.commit();
        this.getSupportFragmentManager().executePendingTransactions();
    }
}

}

我正在关注教程。

4

2 回答 2

1

这是您的问题的解决方案,我对您有帮助...我发布了一段代码:

 TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                android.support.v4.app.FragmentManager fm =   getSupportFragmentManager();
                AndroidFragment androidFragment = (AndroidFragment) fm.findFragmentByTag("android");
                AppleFragment appleFragment = (AppleFragment) fm.findFragmentByTag("apple");
                android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();

                /** Detaches the androidfragment if exists */


                /** Detaches the applefragment if exists */


                /** If current tab is android */
                if(tabId.equalsIgnoreCase("android")){

                    if(androidFragment==null){      
                        /** Create AndroidFragment and adding to fragmenttransaction */
                        ft.add(R.id.realtabcontent,new AndroidFragment(), "android");                       
                    }else{
                        /** Bring to the front, if already exists in the fragmenttransaction */
                        if(appleFragment!=null)
                        ft.hide(appleFragment);

                        ft.show(androidFragment);                       
                    }

                }else{  /** If current tab is apple */
                    if(appleFragment==null){
                        /** Create AppleFragment and adding to fragmenttransaction */
                        ft.add(R.id.realtabcontent,new AppleFragment(), "apple");                       
                    }else{
                        /** Bring to the front, if already exists in the fragmenttransaction */
                        if(androidFragment!=null)
                            ft.hide(androidFragment);

                        ft.show(appleFragment);                     
                    }
                }
                ft.commit();                
            }
        };


        /** Setting tabchangelistener for the tab */
        tHost.setOnTabChangedListener(tabChangeListener);

只需执行此操作,我就可以很好地保存视图状态....

于 2013-02-22T14:02:47.063 回答
0

对于少量的碎片,你可以试试这个

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

在 onCreate() 设置为 true

于 2013-02-22T13:55:50.490 回答