我试图解决我的问题所以我改变了很多代码。我改变了帖子的标题。 我可以成功地在首选项ui中更改imageview背景的颜色。但是在离开片段并再次启动之后,ui无法以与以前相同的方式更新。
首先,我使用sherlockactionbar。有3个标签。当按下第3个条时,会加载包含按钮的片段。按下其中一个按钮时,将加载另一个首选项片段。
下面的代码显示了在按下其中一个按钮时如何调用首选项片段: SettingsMenuFragment.java
public class SettingsMenuFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button ICSbutton= (Button) view.findViewById(R.id.CallSearchSettingsButton);
ICSbutton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
clearFragmentStack();
SettingsIncomingSearchFragment removeSISF = (SettingsIncomingSearchFragment) getActivity().getSupportFragmentManager().findFragmentByTag("SISF");
if(removeSISF != null)
{
getActivity().getSupportFragmentManager().beginTransaction().remove(removeSISF).commit() ;
getActivity().getSupportFragmentManager().executePendingTransactions();
}
SettingsIncomingSearchFragment Prefrag = new SettingsIncomingSearchFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, Prefrag ,"SISF");
transaction.addToBackStack(null);
transaction.commit();
getActivity().getSupportFragmentManager().executePendingTransactions();
}
} );}}
,下面的代码显示了preferencefragment:
public class SettingsIncomingSearchFragment extends PreferenceListFragment implements SharedPreferences.OnSharedPreferenceChangeListener,PreferenceListFragment.OnPreferenceAttachedListener {
Context ctx ;
public static final String SHARED_PREFS_NAME = "settings";
LinearLayout mainlayout ;
LinearLayout sublayout ;
View view ;
Preference myPref ;
ImageView img ;
SharedPreferences sp ;
@Override
public void onCreate(Bundle icicle) {
ctx = getActivity() ;
super.onCreate(icicle);
addPreferencesFromResource(R.xml.pref_incomingsearchsettings);
myPref = (Preference) findPreference("incomingsearchbackgroundcolor");
setColor() ;
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
HSVColorPickerDialog cpd = new HSVColorPickerDialog( ctx, 0xFF4488CC, new OnColorSelectedListener() {
@Override
public void colorSelected(Integer color)
{
sp.edit().putString("incomingsearchbackgroundcolor", String.valueOf(color)).commit();
}
});
cpd.setTitle( "Pick a color" );
cpd.show();
return true ;
}
});
}
private void setColor() {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.rectangle_layout, null);
mainlayout = (LinearLayout)view.findViewById(R.id.rectangle_main_layout_ll);
sublayout = (LinearLayout)mainlayout.findViewById(R.id.rectangle_layout_ll);
sp = ctx.getSharedPreferences(SHARED_PREFS_NAME, ctx.MODE_PRIVATE);
String defValue = null ;
defValue = sp.getString("incomingsearchbackgroundcolor", null);
img = (ImageView)sublayout.findViewById(R.id.iv_priority);
img.setBackgroundColor(Integer.parseInt(defValue));
}
@Override
public void onPreferenceAttached(PreferenceScreen root, int xmlId) {
// TODO Auto-generated method stub
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
if(key.equals("incomingsearchbackgroundcolor"))
{
sp = ctx.getSharedPreferences(SHARED_PREFS_NAME, ctx.MODE_PRIVATE);
String defValue = null ;
defValue = sp.getString("incomingsearchbackgroundcolor", null);
Log.d("Debug", defValue);
int iColor = Integer.parseInt(defValue);
img.setBackgroundColor(iColor);
img.invalidate();
if(this.isAdded())
{
getActivity().getSupportFragmentManager().beginTransaction().detach(this).commit() ;
getActivity().getSupportFragmentManager().executePendingTransactions();
getActivity().getSupportFragmentManager().beginTransaction().attach(this).commit();
getActivity().getSupportFragmentManager().executePendingTransactions();
}
}
}
@Override
public void onResume()
{
super.onResume();
sp.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
sp.unregisterOnSharedPreferenceChangeListener(this);
}
}
这是首选项xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:summary = "Options" android:title = "OPTIONS" android:key="options">
<CheckBoxPreference android:key="IncomingCallSearch" android:summary="On/Off" android:title="Enable Incoming Call Search" android:defaultValue="true"/>
</PreferenceCategory>
<PreferenceCategory android:summary = "Screen Settings" android:title = "Screen settings" android:key="ScreenSettings" >
<ListPreference
android:entries="@array/screenLocOptions"
android:entryValues="@array/screenLocValues"
android:key="incomingcallsearch_screenlocation"
android:title="Location"
android:defaultValue="Top"/>
<Preference
android:defaultValue="0xFF000000"
android:key="incomingsearchbackgroundcolor"
android:title="Background Color"
android:layout="@layout/rectangle_layout" />
</PreferenceCategory>
这是rectangle_layout xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="@+id/rectangle_main_layout_ll">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Background color"
android:layout_weight= "0.9"/>
<LinearLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:orientation="vertical"
android:layout_weight= "0.1"
android:id="@+id/rectangle_layout_ll">
<ImageView
android:id="@+id/iv_priority"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
/>
</LinearLayout>
正如我写的这篇文章的前一版本,上面的代码在第一个加载的片段中成功运行,imageview背景颜色在首选项ui中更新。我尝试逐步编写失败条件:
1。我从操作栏中选择第3个栏并加载包含按钮的片段。我按下加载首选项片段的按钮(你可以看到上面的代码)
3。在此首选项片段中,首选项包括textview和imageview(您可以在上面看到详细信息)
4。当我点击这个偏好时,一个颜色选择器运行。(你可以看到上面的细节)
5。我从颜色选择器中选择一种颜色并将其保存到共享偏好。(您可以看到上面的详细信息)
6。 onsharedpreferencechanged事件已触发,我成功更改了imageview的背景颜色(您可以看到上面的详细信息)。
7。我留下片段,从操作栏中选择另一个选项卡或使用背压按钮
8。我在第3栏中按下相同的按钮启动相同的片段
9。我再次使用颜色选择器并触发onsharedpreferencechanged事件
10。我可以通过调试看到真正的颜色代码来自共享首选项,它被设置为imageview背景颜色,并运行以下代码:
getActivity()。getSupportFragmentManager()。beginTransaction()。detach(this).commit();
。getActivity()getSupportFragmentManager()executePendingTransactions();
。getActivity()getSupportFragmentManager()的BeginTransaction()连接(本).commit()。;
。getActivity()getSupportFragmentManager()executePendingTransactions();
11。但是在这个时候,偏好不会更新。 在imageview中可以看到旧的颜色或黑色。
非常感谢
答案 0 :(得分:0)
getSupportFragmentmanager().detach(this).attach(this).commit()
提交后,所有更改仅仅 。因此,如果您在中间没有提交时调用detach(this)
和attach(this)
,则您什么都没做。
尝试做这样的事情:
getSupportFragmentmanager().detach(this).commit();
getSupportFragmentmanager().attach(this).commit();
这就是提交理念背后的原因。
P.S
我没有在API中找到FragmentManager.detach()方法......
答案 1 :(得分:0)
最后我解决了这个问题,这很简单。我希望这篇文章可以帮助任何有动态偏好更新问题的人。
public class SettingsIncomingSearchFragment extends PreferenceListFragment
implements SharedPreferences.OnSharedPreferenceChangeListener,
PreferenceListFragment.OnPreferenceAttachedListener {
Context ctx ;
public static final String SHARED_PREFS_NAME = "settings";
<-- Begin : i delete these global variables and define them in methods locally and
set the values in methods -->
LinearLayout mainlayout ;
LinearLayout sublayout ;
View view ;
Preference myPref ;
ImageView img ;
SharedPreferences sp ;
<-- End : i delete these global variables and define them in methods locally and
set the values in methods -->