Java 写入SharedReferences后是否更新布局?
我的第一个线程在这里-抱歉,如果文本格式是坏的 我使用安卓Studio 3.1.3 API27,并为智能手机开发应用程序 该应用程序目前由1个活动(分成3个片段)、第二个活动和5个xml文件组成 通过使用ViewPager,我可以浏览3个片段 第二个片段(中间片段)包含两个按钮,每个按钮打开第二个活动,其中包含许多颜色按钮 单击颜色按钮时,我可以更改第一个片段的背景颜色 选择颜色后,第二个活动关闭,我回到活动1->fragment2 它可以工作,但问题是我总是要刷到第三个片段, 然后回到第二个位置,再回到第一个位置。 如果我不这样做,片段1的颜色将保持原来的颜色。 现在我正在寻找一种方法,只要我按下活动2的颜色按钮,就可以更新片段1的布局 我已经试过了:Java 写入SharedReferences后是否更新布局?,java,android,android-fragments,layout,refresh,Java,Android,Android Fragments,Layout,Refresh,我的第一个线程在这里-抱歉,如果文本格式是坏的 我使用安卓Studio 3.1.3 API27,并为智能手机开发应用程序 该应用程序目前由1个活动(分成3个片段)、第二个活动和5个xml文件组成 通过使用ViewPager,我可以浏览3个片段 第二个片段(中间片段)包含两个按钮,每个按钮打开第二个活动,其中包含许多颜色按钮 单击颜色按钮时,我可以更改第一个片段的背景颜色 选择颜色后,第二个活动关闭,我回到活动1->fragment2 它可以工作,但问题是我总是要刷到第三个片段, 然后回到第二个位
- 在编写SharedReferences(Activity2)时,我使用editor.apply()而不是editor.commit()
- 在读取SharedReferences(Activity1->Fragment1)时,我使用Context.MODE\u MULTI\u进程而不是Context.MODE\u PRIVATE
- 使用viewpage.setOffscreenPageLimit(0);在my public void SetUpViewPager(ViewPager viewpage)方法内的MainActivity中
onStop()
和onStart()
并将其用作post值
EventBus.getDefault().post(new MessageEvent("Change Color"));
此函数将处理MessageEvent
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
//change the color here
//add this function in Page_1
}
当您更新颜色的值时。放入MessageEvent当片段变为可见时(即,ViewPager中的选定页面),将调用其setUserVisibleHint()方法。您可以在片段中重写该方法并使用它触发刷新
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
//you can check if the color is changed then refresh the fragment if not then don't do anything
//here you should refresh your fragment , this will called every time you
//view this fragment in all cases even if you didn't move to the
//third tab
}
}
如何刷新片段
Fragment currentFragment = getFragmentManager().findFragmentByTag("YourFragmentTag");
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.detach(currentFragment);
fragmentTransaction.attach(currentFragment);
fragmentTransaction.commit();
所以你想在更改颜色时直接更新颜色?在你在onActivityCreated cut paste in onResume()中写的第一个片段中,对不起@VishalThakkar,我在写答案时没有读你的回复。哦,哇,这实际上解决了问题!谢谢我甚至需要创建onActivityCreated吗?这是可行的,但是当viewpager完成滚动时将调用该方法。因此,他将看到旧颜色,直到滚动完成,然后更改颜色:)每次他转到片段时都会调用此方法,即使他在第二个选项卡中,然后移动到第一个或第三个到第一个,所以不,他不会看到旧的颜色,尝试实现它……我的意思是,每次他访问片段时都会调用这个方法,不管他是否完成滚动,或者他在哪里(关于标签位置,比如第二个第三个),你是对的。但是他不应该把代码放在if(isVisibleToUser)中,只有当片段可见时才会调用它。你说得对!这是一个很好的观点,但是他可以做一个简单的检查,比如如果颜色改变了,那么刷新,如果没有,那么就不要做任何事情。好的,我试过了,但还没有成功。我创建了一个公共静态最终字符串PAGE\u 1\u FRAGMENT=“PAGE\u 1\u FRAGMENT”;在我的页面_1.java中添加了公共void setUserVisibleHint(布尔值isVisibleToUser)。然后我添加了您的“如何刷新片段”代码,并通过第1页的片段更改了“YourFragmentTag”。我还尝试了“页面片段”,但没有成功。我一启动应用程序就崩溃了。谢谢。我假设在这种情况下,我应该将onResume添加到Page_2.java的内部?因此,每次我关闭Acitivity 2(并自动返回到第2页),它都会执行onResume方法。我用:Log.wtf(“test”,“Fragment2-loaded”);是的,它成功了。但是,我不知道我必须在这个方法中发布什么。你在onActivityCreated中写的内容,就像@Vishal Thakkar说的那样
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Page_4_Colors extends Activity
{
int pos;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.page4_colors);
SharedPreferences prefs = getSharedPreferences("bgColor", MODE_MULTI_PROCESS); // Load saved shared file
pos = prefs.getInt("position", pos); // Load saved position (int)
Log.wtf("Position", String.valueOf(pos)); // Show pos value in Log
Button buttonWhite = findViewById(R.id.button_white);
buttonWhite.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.white), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonYellow = findViewById(R.id.button_yellow);
buttonYellow.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.yellow), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonOrange = findViewById(R.id.button_orange);
buttonOrange.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.orange), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonRed = findViewById(R.id.button_red);
buttonRed.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.red), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonGreen = findViewById(R.id.button_green);
buttonGreen.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.green), getResources().getColor(R.color.black)); // Load setColor method and send 2 color values
}
});
Button buttonBlue = findViewById(R.id.button_blue);
buttonBlue.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
setColor(getResources().getColor(R.color.blue), getResources().getColor(R.color.white)); // Load setColor method and send 2 color values
}
});
}
public void setColor (int backgroundColor, int textColor) // Write color values into SharedPreferences
{
SharedPreferences prefs = getSharedPreferences("bgColor", MODE_MULTI_PROCESS); // Create new SharedPreferences instance
SharedPreferences.Editor editor = prefs.edit(); // Assign variable to editor function
if (pos == 1)
{
editor.putInt("backgroundColorLeft", backgroundColor); // Write background color (int) inside of editor
editor.putInt("textColorLeft", textColor); // Write text color (int) inside of editor
}
if (pos == 2)
{
editor.putInt("backgroundColorRight", backgroundColor); // Write background color (int) inside of editor
editor.putInt("textColorRight", textColor); // Write text color (int) inside of editor
}
editor.apply(); // Save values, close process
this.finish(); // Close this activity
}
}
EventBus.getDefault().unregister(this)
EventBus.getDefault().register(this)
EventBus.getDefault().post(new MessageEvent("Change Color"));
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
//change the color here
//add this function in Page_1
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
//you can check if the color is changed then refresh the fragment if not then don't do anything
//here you should refresh your fragment , this will called every time you
//view this fragment in all cases even if you didn't move to the
//third tab
}
}
Fragment currentFragment = getFragmentManager().findFragmentByTag("YourFragmentTag");
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.detach(currentFragment);
fragmentTransaction.attach(currentFragment);
fragmentTransaction.commit();