2017-01-31 3 views
3

Ich versuche, die Sichtbarkeit von 1 meiner Shapes in einem Fragment umzuschalten, aber es funktioniert nicht, wenn ich auf das SwitchPreferenceCompat-Steuerelement & zurück zu meinem geladenen Fragment klicke. Keine Fehler oder Warnungen waren vor & während ich die App entweder lief. Hier ist mein Code:Fragmentelement wird nicht minimiert

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> 

Seite Einstellungen Screenshot 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); 
     } 
    } 
} 

Aktuelles Ergebnis enter image description here

Erwartetes Ergebnis enter image description here

+0

Können Sie die Layoutdatei 'fragment_blueshapes.xml' veröffentlichen? – Johann67

+0

poste deine 'res/layout/activity_settings.xml' – Saurabh

Antwort

2

Nun erste Vorschlag bitte android Coding Standard folgen, weil, dass Ihr eine Menge Probleme lösen würde und für Sharedpreferences https://medium.com/@ali.muzaffar/android-sharedpreferences-singleton-to-make-life-easier-f1d802b6cd8e versuchen Singletonmuster verwenden und nie hart codieren Ihre Konstanten eine Klasse Constants.class erstellen und setzen all deine Konstanten, die viel einfacher zu verwalten wären.

Edit 1:

Sobald Sie den Wert SwitchPreferenceCompat ändern Sie es überall in der Anwendung zugreifen können Ihre loadPreferences Methode wie folgt ändern.

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

Habe es einfach ausprobiert und macht immer noch keinen Unterschied. Was du erwähnt hast, war ein Fehler, den ich bemerkt und korrigiert habe, bevor du deine Antwort geschrieben hast. – MacaronLover

+0

Ich kopiere nur deinen Code und habe diese Demo erstellt, bitte schau sie dir an. https://github.com/reyanshmishra/stackoverflow-demo –

+0

Ich habe meinen Code geändert, indem ich [dieses Tutorial] (https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part -1-5959aa49337c), weil es den Android-Richtlinien entspricht, während der Code, den ich vorher verwendet habe, nicht funktioniert. Bitte sehen Sie sich meinen aktualisierten Code an (insbesondere ** EinstellungenFragment.java **, ** EinstellungenAktivität.java **, ** xml/app_preferences.xml **, ** preference_keys.xml **, ** strings.xml * *) und editiere deine Antwort nach Bedarf, damit sie meine aktualisierte Frage beantwortet und löst. – MacaronLover

Verwandte Themen