Android首选项 - 在屏幕外滚动时重置开/关开关图标

时间:2013-11-29 16:48:35

标签: java android eclipse

我在eclipse中编写了一个Android应用程序。在我的首选项/设置屏幕中,我在顶部有开/关开关图标。如果我滚动到设置屏幕的底部并向上滚动到顶部再次看到开关,它们已从关闭更改为开启,或者开启或关闭。它们是随机的。并非所有开关都同时更改。有时它只是少数,有时都会这样做。

大多数情况下,它们似乎从关闭转为开启,但有时则相反。谁看过这个吗?有人可以帮忙解决这个问题吗?如果需要,当我到PC时,我会发布代码。感谢。

编辑:这是我的代码。这是我的主要活动,即选项菜单代码。此主要活动是具有多个片段的ViewPager。超过一半的时间你会看到我在哪里调用设置活动。

@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
    // Handle item selection

    switch (item.getItemId()) {

    case R.id.refresh:
        item.setActionView(R.layout.ab_loading);
        item.expandActionView();

        if (currentTab == 0 && mMarketFragment != null && mMarketFragment.isAdded()) {
            Log.v(TAG, "MarketFrag isnt null and its added");

            Loader loader = mMarketFragment.getLoaderManager().getLoader(2); 
            if ( loader != null && !loader.isReset() ) { 
                mMarketFragment.getLoaderManager().restartLoader(2, null, mMarketFragment); 
                mMarketFragment.getLoaderManager().restartLoader(3, null, mMarketFragment); 
                mMarketFragment.getLoaderManager().restartLoader(4, null, mMarketFragment);
            } else {
                mMarketFragment.getLoaderManager().initLoader(2, null, mMarketFragment);
                mMarketFragment.getLoaderManager().initLoader(3, null, mMarketFragment);
                mMarketFragment.getLoaderManager().initLoader(4, null, mMarketFragment);
            }
        }             
        else if (currentTab == 1 && mPortfolioFragment != null && mPortfolioFragment.isAdded()) {
            Log.v(TAG, "PortfolioFrag isnt null and is added");
            Loader loader2 = mPortfolioFragment.getLoaderManager().getLoader(-1); 
            if ( loader2 != null && !loader2.isReset() ) { 
                mPortfolioFragment.getLoaderManager().restartLoader(-1, null, mPortfolioFragment); 
            } else {
                mPortfolioFragment.getLoaderManager().initLoader(-1, null, mPortfolioFragment);
            }
            mPortfolioFragment.getLoaderManager().getLoader(-1).forceLoad();
        }
        else if (currentTab == 2 && mNewsFragment != null) {
            mNewsFragment.refresh();
        }
        else if (currentTab == 3 && mTwitterFragment != null) {
            mTwitterFragment.refresh();
        }

        return true;

    case R.id.dark:
        MainStocksICSActivity.THEME = R.style.DefaultTheme;
        saveTheme(R.style.DefaultTheme);
        return true;
    case R.id.light:
        MainStocksICSActivity.THEME = R.style.LightTheme;
        saveTheme(R.style.LightTheme);
        return true;
    case R.id.lightDark:
        MainStocksICSActivity.THEME = R.style.LightDarkTheme;
        saveTheme(R.style.LightDarkTheme);
        return true;
    case R.id.settings:
        Intent intent = new Intent();
        intent.setClass(MarketActivity.this, SettingsActivity.class);
        Log.v(TAG, "settings call");
        startActivityForResult(intent, ACTIVITY_SETTINGS);
        return true;        
    case android.R.id.home:
        // app icon in action bar clicked; go home
       super.onBackPressed();
       return true;
    case R.id.stocksearch:
        intent = new Intent();
        intent.setClass(MarketActivity.this, StockSearchActivity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        Log.v(TAG, "stock search call");
        startActivityForResult(intent, MENU_STOCK_ADDTICKER);
        return true;


    default:
        return true;
    }        
}

这是我的设置活动和片段。

public class SettingsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction().replace(android.R.id.content,
            new PrefsFragment()).commit();
}


public static class PrefsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.settings);
    }
}

}

这是我的设置XML。

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
android:key="using_categories_in_root_screen"
android:title="SETTINGS"
android:summary="Test">

<PreferenceCategory 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="settings_topic_title"
    android:title="GENERAL">

    <SwitchPreference
        android:key="settings_rss_feed_save"
        android:title="@string/settings_saving_policy_title"
        android:switchTextOn = "YES"
        android:switchTextOff = "NO"
        android:summary="@string/settings_saving_policy_summary" />

    <!--CheckBoxPreference
        android:key="settings_auto_sync"
        android:title="@string/settings_sync_title"
        android:summary="@string/settings_sync_summary" /-->


    <SwitchPreference
            android:key="settings_use_external_browser"
            android:title="Use external browser"
            android:summary="User external browser when displaying web content for stocks and news"
            android:switchTextOn = "YES"
            android:switchTextOff = "NO"
            android:defaultValue="false" />

    </PreferenceCategory>
<PreferenceCategory
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="invert_graph"
    android:title="Graph Color">

    <SwitchPreference
            android:key="settings_invert_graph"
            android:title="Invert Graph with Theme"
            android:summary="Whether to invert the graph color wiht the selected theme."
            android:switchTextOn = "Yes"
            android:switchTextOff = "No"
            android:defaultValue="true" />

</PreferenceCategory>

<PreferenceCategory
        android:title="Quotes Source">

    <EditTextPreference
            android:key="edittext_preference"
            android:title="@string/title_edittext_preference"
            android:summary="@string/summary_edittext_preference"
            android:dialogTitle="@string/dialog_title_edittext_preference" />

    <ListPreference
            android:key="stock_data_source"
            android:title="Quote Data Source"
            android:summary="Select Yahoo or Google for quotes"
            android:entries="@array/stock_data_source_options"
            android:entryValues="@array/stock_data_source"
            android:defaultValue="yahoo_data"
            android:dialogTitle="Select the quote source" />
    <ListPreference
            android:key="chart_data_source"
            android:title="Chart Data Source"
            android:summary="Set the Small Chart (non-trendline is better for clearer small text)"
            android:entries="@array/chart_data_source_options"
            android:entryValues="@array/chart_data_source"
            android:defaultValue="yahoo_chart_with_trendlines"
            android:dialogTitle="Select the chart source" />

</PreferenceCategory>

<PreferenceCategory
        android:title="News Options">

    <!-- This PreferenceScreen tag serves as a screen break (similar to page break
         in word processing). Like for other preference types, we assign a key
         here so it is able to save and restore its instance state. -->
    <PreferenceScreen
            android:key="subscriptions_screen"
            android:title="Yahoo News Categories"
            android:summary="Select which categories to display in news general screen">

        <!-- You can place more preferences here that will be shown on the next screen. -->

        <PreferenceCategory 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:key="subscriptions_news"
            android:title="@string/title_newstopic">
            <SwitchPreference
                    android:key="usmarkets"
                    android:title="@string/title_usmarkets"
                    android:summary="Something"
                    android:switchTextOn = "YES"
                    android:switchTextOff = "NO"
                     />
            <SwitchPreference
                    android:key="mostpopular"
                    android:title="@string/title_mostpopular"
                    android:summary="Test"
                    android:switchTextOn = "YES"
                    android:switchTextOff = "NO"
                     />
            <SwitchPreference
                    android:key="investingstrategies"
                    android:title="@string/title_investingstrategies"
                    android:summary="four"
                    android:switchTextOn = "YES"
                    android:switchTextOff = "NO"
                     />
            <SwitchPreference
                    android:key="taxes"
                    android:title="@string/title_taxes"
                    android:summary="funtimes"
                    android:switchTextOn = "YES"
                    android:switchTextOff = "NO"
                     />
        </PreferenceCategory>
        <PreferenceCategory 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:key="subscription_basicmaterials"
            android:title="@string/title_basicmaterials">

            <SwitchPreference
                android:key="OilGasRefiningMarketing"
                android:title="@string/title_OilGasRefiningMarketing"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="Gold"
                android:title="@string/title_Gold"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="Silver"
                android:title="@string/title_Silver"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="SteelIron"
                android:title="@string/title_SteelIron"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="Synthetics"
                android:title="@string/title_Synthetics"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
        </PreferenceCategory>
        <PreferenceCategory 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:key="subscription_financial"
            android:title="@string/title_financial">
            <SwitchPreference
                android:key="AssetManagement"
                android:title="@string/title_AssetManagement"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="RealEstateDevelopment"
                android:title="@string/title_RealEstateDevelopment"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="CreditServices"
                android:title="@string/title_CreditServices"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="LifeInsurance"
                android:title="@string/title_LifeInsurance"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="SavingsLoans"
                android:title="@string/title_SavingsLoans"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
        </PreferenceCategory>
        <PreferenceCategory 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:key="subscription_healthcare"
            android:title="@string/title_healthcare">
            <SwitchPreference
                android:key="HealthCarePlans"
                android:title="@string/title_HealthCarePlans"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="Hospitals"
                android:title="@string/title_Hospitals"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="MedicalInstrumentsSupplies"
                android:title="@string/title_MedicalInstrumentsSupplies"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
        </PreferenceCategory>
        <PreferenceCategory 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:key="subscription_technology"
            android:title="@string/title_technology">
            <SwitchPreference
                android:key="ApplicationSoftware"
                android:title="@string/title_ApplicationSoftware"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="HealthcareInformationServices"
                android:title="@string/title_HealthcareInformationServices"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="InternetSoftwareServices"
                android:title="@string/title_InternetSoftwareServices"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
            <SwitchPreference
                android:key="WirelessCommunications"
                android:title="@string/title_WirelessCommunications"
                android:switchTextOn = "YES"
                android:switchTextOff = "NO"
                 />
        </PreferenceCategory>

    </PreferenceScreen>
    <SwitchPreference
            android:key="google_news"
            android:title="Google News"
            android:summary="Whether to include Google News"
            android:switchTextOn = "YES"
            android:switchTextOff = "NO"
            android:defaultValue="false" />     
    <SwitchPreference
            android:key="foxbusiness_news"
            android:title="Fox Business News"
            android:summary="Whether to include Fox Business News"
            android:switchTextOn = "YES"
            android:switchTextOff = "NO"
            android:defaultValue="true" />
    <SwitchPreference
            android:key="bloggingstocks_news"
            android:title="Blogging Stocks News"
            android:summary="Whether to include BloggingStocks News"
            android:switchTextOn = "YES"
            android:switchTextOff = "NO"
            android:defaultValue="false" /> 
    <SwitchPreference
            android:key="wsj_news"
            android:title="Wall St Journal Market News"
            android:summary="Whether to include WSJ News"
            android:switchTextOn = "YES"
            android:switchTextOff = "NO"
            android:defaultValue="false" /> 
</PreferenceCategory>    

所有开关在离开屏幕时都不会粘住并重置。请注意,我的设置有几个屏幕,一个使用列表首选项,而顶部的主屏幕没有。然而,所有交换机都不会坚持下去。我错过了一些愚蠢的东西吗?如果我忽视某些事情,请帮忙。谢谢!

0 个答案:

没有答案