片段项不会崩溃 [英] Fragment item won't collapse

查看:220
本文介绍了片段项不会崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试切换片段中我的1个形状的可见性但是每当我点击SwitchPreferenceCompat控件&返回我加载的片段。之前没有出现任何错误或警告。在我运行应用程序期间。这是我的代码:

I'm trying to toggle the visibility of 1 of my shapes within a fragment but it does not work whenever I click the SwitchPreferenceCompat control & return to my loaded fragment. No errors or warnings had appeared before & during I ran the app either. Here is my code:

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>

设置页面截图

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);
        }
    }
}

当前结果

预期结果

推荐答案

首先建议请遵循android编码标准,因为这样可以解决很多问题问题和共享偏好 https:// medium .com / @ ali.muzaffar / android-sharedpreferences-singleton-to-make-life-ease-f1d802b6cd8e 尝试使用单例模式,永远不要硬编码你的常量创建一个类Constants.class并将所有常量放在那里更容易维护。

Well first suggestion please follow android coding standard because that would solve your a lot of problem and for Sharedpreferences https://medium.com/@ali.muzaffar/android-sharedpreferences-singleton-to-make-life-easier-f1d802b6cd8e try using singleton pattern and never hardcode your constants create a class Constants.class and put all your constants there that would be lot easier to maintain.

编辑1:

一旦改变你可以在应用程序的任何地方访问它的SwitchPreferenceCompat的值改变你的loadPreferences方法。

Once you change the value of SwitchPreferenceCompat you can access it anywhere in the application change your loadPreferences method like this.

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

这篇关于片段项不会崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆