自定义首选项中的TextView无法正确更新

问题描述 投票:0回答:1

我有2个片段:

  • 扩展HomeFragmentFragment
  • 扩展SettingsFragmentPreferenceFragment

MainActivity,我有2个按钮:

  • switchButton允许在2个片段之间切换
  • updateButton允许更新SettingsFragment中的字符串(表示摘要)SettingsFragment中的摘要是TextView中用于CustomPreferenceSettingsFragment文本。

我想使用updateButton更新CustomPreference中的摘要。第一次将SettingsFragment添加到活动时,updateButton无法设置摘要。但是,当我使用switchButton放回SettingsFragment时,它工作正常。 我已经将日志放在每个功能的开头,希望能够追踪问题。 第一次放置SettingsFragment并点击updateButton,我得到以下日志

D/MainActivity: onCreate
D/MainActivity: Fragments button clicked
D/SettingsFragment: onCreate
D/CustomPreference: CustomPreference constructor
D/SettingsFragment: onViewCreated
D/CustomPreference: set summary : Started
D/CustomPreference: onBindView
    setting mSummaryTextView to Started
D/CustomPreference: onBindView
    setting mSummaryTextView to Started
D/CustomPreference: onBindView
    setting mSummaryTextView to Started
D/MainActivity: Update button clicked
D/SettingsFragment: setSummary : Hello Preference (0)
D/CustomPreference: set summary : Hello Preference (0)
    setting mSummaryTextView to Hello Preference (0)

现在,如果我第二次放回SettingsFragment并单击updateButton,我会得到以下日志

D/MainActivity: Fragments button clicked
D/SettingsFragment: onCreate
D/CustomPreference: CustomPreference constructor
D/SettingsFragment: onViewCreated
D/CustomPreference: set summary : Started
D/CustomPreference: onBindView
    setting mSummaryTextView to Started
D/CustomPreference: onBindView
    setting mSummaryTextView to Started
D/CustomPreference: onBindView
    setting mSummaryTextView to Started
D/CustomPreference: onBindView
    setting mSummaryTextView to Started
D/MainActivity: Update button clicked
D/SettingsFragment: setSummary : Hello Preference (1)
D/CustomPreference: set summary : Hello Preference (1)
    setting mSummaryTextView to Hello Preference (1)

(注意第二种情况再次调用onBindView

下面,我把我的代码

main activity.Java

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    HomeFragment homeFragment = new HomeFragment();
    SettingsFragment settingsFragment = new SettingsFragment();
    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set settingsFragment as the default fragment
        getFragmentManager().beginTransaction()
                .replace(R.id.fragmentContainer, homeFragment).commit();

        // A simple button to switch between the two fragments
        Button switchButton = findViewById(R.id.switchButton);
        switchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Fragments button clicked");
                if(homeFragment.isAdded()) {
                    getFragmentManager().beginTransaction()
                            .replace(R.id.fragmentContainer, settingsFragment).commit();
                } else {
                    getFragmentManager().beginTransaction()
                            .replace(R.id.fragmentContainer, homeFragment).commit();
                }
            }
        });

        // A simple button to change the summary of the preference
        Button updateButton = findViewById(R.id.updateButton);
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Update button clicked");
                if(settingsFragment.isAdded()) {
                    settingsFragment.setSummary("Hello Preference (" + counter + ")");
                    counter++;
                }
            }
        });
    }
}

home fragment.Java

public class HomeFragment extends Fragment {

    private static final String TAG = "HomeFragment";

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView textView = view.findViewById(R.id.homeText);
        if(textView != null) {
            textView.setText("Home fragment text (modified)");
        }
    }
}

settings fragment.Java

public class SettingsFragment extends PreferenceFragment {

    private static final String TAG = "SettingsFragment";

    CustomPreference customPreference;
    String mSummary;

    public SettingsFragment() {
        // Required empty public constructor
    }

    public void setSummary(String text) {
        Log.d(TAG, "setSummary : " + text);
        ((CustomPreference) findPreference("test_key")).setSummary(text);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.fragment_settings);

        mSummary = "Started";
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onViewCreated");
        super.onViewCreated(view, savedInstanceState);

        customPreference = (CustomPreference) findPreference("test_key");
        customPreference.setSummary(mSummary);
    }
}

custom preference.Java

public class CustomPreference extends Preference {

    private static final String TAG = "CustomPreference";

    private String mSummary;
    private TextView mSummaryTextView;

    public CustomPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG, "CustomPreference constructor");

        // Handle customized attributes
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs, R.styleable.CustomPreference, 0, 0);

        mSummary = a.getString(R.styleable.CustomPreference_summary);

        // TypedArray objects are shared and must be recycled
        a.recycle();
    }

    public void setSummary(String summary) {
        Log.d(TAG, "set summary : " + summary);
        this.mSummary = summary;

        if (mSummaryTextView != null) {
            Log.d(TAG, "setting mSummaryTextView to " + mSummary);
            mSummaryTextView.setText(mSummary);
            mSummaryTextView.invalidate();
        }
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        Log.d(TAG, "onBindView");

        mSummaryTextView = view.findViewById(R.id.summaryTextView);
        if (mSummaryTextView != null) {
            Log.d(TAG, "setting mSummaryTextView to " + mSummary);
            mSummaryTextView.setText(mSummary);
        }
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/activityText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:gravity="center_horizontal"
        android:text="MainActivity" />

    <Button
        android:id="@+id/updateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/activityText"
        android:text="Update"/>

    <Button
        android:id="@+id/switchButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/updateButton"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/activityText"
        android:text="Change fragment"/>

    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/switchButton"
        app:layout_constraintBottom_toBottomOf="parent">

    </FrameLayout>

</android.support.constraint.ConstraintLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".HomeFragment">

    <TextView
        android:id="@+id/homeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:text="Home fragment text" />

</android.support.constraint.ConstraintLayout>

fragment_settings.xml

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

    <com.gosense.myapplication.CustomPreference
        android:key="test_key"
        preference:summary="My custom preference"
        android:layout="@layout/preference_layout"/>

</PreferenceScreen>

preference_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textAllCaps="true"
        android:textColor="@color/colorAccent"
        android:text="Title"/>

    <TextView
        android:id="@+id/summaryTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/titleTextView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textAllCaps="true" />

</android.support.constraint.ConstraintLayout>
android android-fragments android-preferences
1个回答
0
投票

我找到了解决方案

事实上,在CustomPreference内部,每当我们对内部视图进行更改时,我们必须使用notifyChanged();通知这些更改。因此,当我通过mSummaryTextView.invalidate();更改CustomPreference.java中的行notifyChanged();时,它立即起作用。 希望有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.