2017-01-31 100 views
3

我想切換一個片段內的我的形狀的可見性,但它不工作,只要我點擊SwitchPreferenceCompat控件&返回到我加載的片段。在我運行應用程序期間,在&之前沒有出現任何錯誤或警告。這裏是我的代碼:片段項目不會崩潰

fragment_blueshapes.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <View 
     android:id="@+id/blue_square" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="20dp" 
     android:background="@drawable/shape_blue_square" /> 

    <View 
     android:id="@+id/blue_circle" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/shape_blue_circle" 
     android:layout_marginBottom="20dp" 
     android:layout_below="@id/blue_square"/> 

    <View 
     android:id="@+id/blue_rectangle" 
     android:layout_width="300dp" 
     android:layout_height="100dp" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/shape_blue_rectangle" 
     android:layout_below="@id/blue_circle"/> 
</RelativeLayout> 

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     PreferencesFragment newFragment = new PreferencesFragment(); 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.master_container, newFragment); 
     transaction.commit(); 
    } 
} 

PreferencesFragment.java

public class PreferencesFragment extends PreferenceFragmentCompat { 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public void onCreatePreferences(Bundle bundle, String s) { 
     addPreferencesFromResource(R.xml.app_preferences); 
    } 
} 

XML/app_preferences.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.preference.PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <android.support.v7.preference.SwitchPreferenceCompat 
     android:defaultValue="true" 
     android:key="pref_pref1" 
     android:title="@string/preferences_switch_title" 
     android:summary="@string/preferences_switch_summary" 
     android:layout="@layout/preference_multiline"/> 

</android.support.v7.preference.PreferenceScreen> 

preference_keys.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="pref_pref1" translatable="false">pref_pref1</string> 
</resources> 

的strings.xml

<resources> 
    <string name="app_name">Shapes</string> 

    <string name="preferences_switch_title">Show squares</string> 
    <string name="preferences_switch_summary">This preference applies to all square shapes</string> 
</resources> 

設置頁面的屏幕截圖 enter image description here

BlueShapesActivity.java

public class BlueShapesActivity extends AppCompatActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_shapes); 

     if (savedInstanceState == null) { 
      FragmentBlueShapes newFragment = new FragmentBlueShapes(); 
      FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction(); 
      transaction.replace(R.id.detail_container, newFragment); 
      transaction.commit(); 
     } 
    } 
} 

FragmentBlueShapes.java

public class FragmentBlueShapes extends android.support.v4.app.Fragment { 

    public FragmentBlueShapes() { 
    } 

    boolean squareState; 

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

      return inflater.inflate(R.layout.fragment_blueshapes, container, false); 
    } 

    @Override 
    public void onResume(){ 
     super.onResume(); 
     loadPreferences(); 
     displaySettings(getView()); 
    } 

    public void loadPreferences(){ 
     SharedPreferences pref = this.getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE); 
     squareState = pref.getBoolean("square_state", true); 
    } 

    public void displaySettings(View rootView) { 
     if (squareState) { 
      rootView.findViewById(R.id.blue_square).setVisibility(View.VISIBLE); 
     } else { 
      rootView.findViewById(R.id.blue_square).setVisibility(View.GONE); 
     } 
    } 
} 

當前結果 enter image description here

預期結果 enter image description here

+0

你可以發佈佈局文件'fragment_blueshapes.xml'嗎? – Johann67

+0

發佈你的'res/layout/activity_settings.xml' – Saurabh

回答

2

那麼首先建議請按照Android的編碼標準,因爲這將解決你的很多Sharedpreferences https://medium.com/@ali.muzaffar/android-sharedpreferences-singleton-to-make-life-easier-f1d802b6cd8e的問題和嘗試使用Singleton模式,從來沒有硬編碼的常數,創建一個類Constants.class放那裏的所有常量將更容易維護。

編輯1:

一旦更改SwitchPreferenceCompat的價值,你可以在任何地方訪問它的應用程序更改loadPreferences方法是這樣的。

private void loadPreferences(){ 
     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); 
     squareState = sharedPreferences.getBoolean("pref_pref1", false); 
    } 
+0

剛試過,仍然沒有任何區別。你提到的是我在發佈答案之前發現並糾正的一個錯誤。 – MacaronLover

+0

我複製粘貼你的代碼,並創建這個演示,請看看。 https://github.com/reyanshmishra/stackoverflow-demo –

+0

我通過以下[本教程]更改了我的代碼(https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part -1-5959aa49337c),因爲它正確地遵循Android指南,而之前使用的代碼沒有。請查看我的更新代碼(尤其是** PreferencesFragment.java **,** SettingsActivity.java **,** xml/app_preferences.xml **,preference_keys.xml **,** strings.xml * *)並根據需要編輯您的答案,以便答案並解決我更新的問題。 – MacaronLover