我有2个片段:
HomeFragment
扩展了Fragment
SettingsFragment
扩展了PreferenceFragment
在MainActivity
中,我有2个按钮:
SettingsFragment
中的字符串(表示为 summary )
SettingsFragment
中的 summary 是TextView
中使用的CustomPreference
中SettingsFragment
的文本。我想使用 updateButton 更新CustomPreference
中的摘要。
第一次将SettingsFragment
添加到活动中时, updateButton 无法设置 summary 。但是,当我使用 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
)
下面,我放我的代码
MainActivity.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++;
}
}
});
}
}
HomeFragment.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)");
}
}
}
SettingsFragment.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);
}
}
CustomPreference.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>
答案 0 :(得分:0)
我找到了解决方法
实际上,在notifyChanged();
内,每次我们更改内部视图时,都必须使用mSummaryTextView.invalidate();
通知这些更改。因此,当我通过notifyChanged();
更改 CustomPreference.java 中的行forloop.counter
时,该行立即可用。
希望有帮助