我想改变工具栏,线性布局和按钮放置在一个活动的各种片段和活动中的背景颜色。我尝试在每个颜色的共享首选项中保存一个值,并根据每个片段和活动中的值更改颜色。但结果是片段中的颜色不同,因为共享偏好中的值首先在活动中发生变化,然后调用片段。那么,有没有更好的方法来改变不同布局的片段和活动中不同按钮的颜色来自一个活动?就像使用localbroadcast或其他东西一样?
public interface ColorInterface{
void myAction(String value) ;
}
//片段
@Override
public void myAction(String value) {
if(TextUtils.isEmpty(value)){
if (getActivity()!=null)
toolbar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
}else{
if(value.equals("1")){
if (getActivity()!=null)
toolbar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.mantis));
}else if(value.equals("2")){
if (getActivity()!=null)
toolbar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.sandybrown));
}else if(value.equals("3")){
if (getActivity()!=null)
toolbar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.bostonblue));
}else if(value.equals("4")){
if (getActivity()!=null)
toolbar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
}
}
}
//活动
public class HomeActivity extends AppCompatActivity{
private TabLayout tabs;
private SharedPref pref;
private ColorInterface listener ;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tabs = (TabLayout)findViewById(R.id.tabs);
pref = new SharedPref(HomeActivity.this);
bindWidgetsWithAnEvent();
setupTabLayout();
if (findViewById(R.id.frame) != null) {
if (savedInstanceState != null) {
return;
}
HomeFragment frag1 = new HomeFragment();
getSupportFragmentManager().beginTransaction().add(R.id.frame, frag1).commit();
}
changeColor();
} // onCreate ends
private void bindWidgetsWithAnEvent(){
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
setCurrentTabFragment(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupTabLayout() {
tabs.addTab(tabs.newTab().setIcon(R.drawable.home_white),true);
tabs.addTab(tabs.newTab().setIcon(R.drawable.category_white),true);
tabs.addTab(tabs.newTab().setIcon(R.drawable.upload_white),true);
tabs.addTab(tabs.newTab().setIcon(R.drawable.listing_white),true);
tabs.addTab(tabs.newTab().setIcon(R.drawable.user_white),true);
TabLayout.Tab tab = tabs.getTabAt(0);
tab.select();
}
private void setCurrentTabFragment(int tabPosition){
switch (tabPosition){
case 0 :
HomeFragment frag1 = new HomeFragment();
setListener(frag1);
replaceFragment(frag1);
break;
case 1 :
CategoryFragment frag2 = new CategoryFragment();
replaceFragment(frag2);
break;
case 2 :
AddProdFragment1 frag3 = new AddProdFragment1();
replaceFragment(frag3);
break;
case 3 :
MyProdFragment frag4 = new MyProdFragment();
replaceFragment(frag4);
break;
case 4 :
MyProfileFragment frag5 = new MyProfileFragment();
replaceFragment(frag5);
break;
}
}
public void replaceFragment(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
public void replaceFragments(Fragment fragment) {
FragmentManager fm = getSupportFragmentManager();
Fragment currentFragment = fm.findFragmentById(R.id.frame);
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame, fragment);
if(!currentFragment.getClass().equals(fragment.getClass()))
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
private void updateMyLoc(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, Utility.UPDATEMYLOC,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
if(jObj.getString("ACK").equals("1")){
System.out.println("sammy_loc_updated");
}
} catch (JSONException e) {
// JSON error
System.out.println("sammy_JSONError "+e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError) {
Toast.makeText(HomeActivity.this,getString(R.string.tooslow),Toast.LENGTH_LONG).show();
}else if (error instanceof NoConnectionError){
Toast.makeText(HomeActivity.this,getString(R.string.nointernet),Toast.LENGTH_LONG).show();
}else if (error instanceof AuthFailureError) {
System.out.println("sammy_AuthFailureError "+error);
} else if (error instanceof ServerError) {
System.out.println("sammy_ServerError "+error);
} else if (error instanceof NetworkError) {
System.out.println("sammy_NetworkError "+error);
} else if (error instanceof ParseError) {
System.out.println("sammy_ParseError "+error);
}
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", pref.getString(Utility.USERID));
params.put("lat", String.valueOf(GPSTracker.currLat));
params.put("long", String.valueOf(GPSTracker.currLong));
params.put("device_token_id", "1234567890");
params.put("device_type", "android");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(HomeActivity.this);
requestQueue.add(stringRequest);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
public void setListener(ColorInterface listener){
this.listener = listener ;
}
private void changeColor(){
if(TextUtils.isEmpty(pref.getString(Utility.COLOR))){
tabs.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
listener.myAction("");
pref.setString(Utility.COLOR, "1");
}else{
if(pref.getString(Utility.COLOR).equals("1")){
tabs.setBackgroundColor(ContextCompat.getColor(this, R.color.mantis));
listener.myAction("1");
pref.setString(Utility.COLOR, "2");
}else if(pref.getString(Utility.COLOR).equals("2")){
tabs.setBackgroundColor(ContextCompat.getColor(this, R.color.sandybrown));
listener.myAction("2");
pref.setString(Utility.COLOR, "3");
}else if(pref.getString(Utility.COLOR).equals("3")){
tabs.setBackgroundColor(ContextCompat.getColor(this, R.color.bostonblue));
listener.myAction("3");
pref.setString(Utility.COLOR, "4");
}else if(pref.getString(Utility.COLOR).equals("4")){
tabs.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
listener.myAction("4");
pref.setString(Utility.COLOR, "1");
}
}
}
}