2016-06-03 9 views
2

Ich verwende diese Methode ziemlich oft, um Snackbar-Nachrichten mit den in der App verwendeten Farben anzuzeigen. Aus diesem Grund wollte ich die Methode statisch machen und sie in eine letzte Klasse von Dienstprogrammen einfügen, damit ich sie überall verwenden und nicht jedes Mal wiederholen kann.Platzieren Sie die Snackbar in eine Utility-Klasse

Allerdings hat dies das Problem, dass findViewById und getResources().getColor etc .. brauchen eine Aktivität zu "leben in" und kann auch nicht statisch sein.

Jeder elegante Weg, dies zu tun? Ich habe in den anderen Posts nachgesehen, aber keiner von ihnen hat dieses Problem recht reflektiert. Danke !!

protected void displaySnackbar (String s) 
{ 
    Snackbar snack = Snackbar.make(findViewById(android.R.id.content), s, Snackbar.LENGTH_LONG); 
    View sbview = snack.getView(); 
    sbview.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent)); 
    TextView textView = (TextView) sbview.findViewById(android.support.design.R.id.snackbar_text); 
    textView.setTextColor(getResources().getColor(R.color.primary_light)); 
    snack.show(); 
} 
+0

Die Wurzel der jeweiligen Parameter in dieser Klasse übergeben Blick auf die Snackbar kann sich von "android.R.id.content" unterscheiden? –

Antwort

0

Vielen Dank allen für Ihre Inspiration .. Dieser Code funktioniert jetzt:

protected static void sacaSnackbar (Context context, View view, String s) 
{ 
    Snackbar snack = Snackbar.make(view, s, Snackbar.LENGTH_LONG); 
    View sbview = snack.getView(); 
    sbview.setBackgroundColor(ContextCompat.getColor(contexto, R.color.colorAccent)); 
    TextView textView = (TextView) sbview.findViewById(android.support.design.R.id.snackbar_text); 
    textView.setTextColor(ContextCompat.getColor(context, R.color.primary_light)); 
    snack.show(); 
} 
0

Sie können Kontext und Darstellung als Parameter übergeben

protected static void displaySnackbar (View view, Context context,String s) { 
Snackbar snack = Snackbar.make(view.findViewById(android.R.id.content), s, Snackbar.LENGTH_LONG); 
    View sbview = snack.getView(); 
    sbview.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent)); 
    TextView textView = (TextView) sbview.findViewById(android.support.design.R.id.snackbar_text); 
    textView.setTextColor(context.getResources().getColor(R.color.primary_light)); 
    snack.show(); 
} 
1

Sie die Ansicht und Kontext als Parameter für die Methode übergeben kann

protected void displaySnackbar (Context context, View view, String s) 
{ 
    Snackbar snack = Snackbar.make(view, s, Snackbar.LENGTH_LONG); 
    View sbview = snack.getView(); 
    sbview.setBackgroundColor(context.getColor(this, R.color.colorAccent)); 
    TextView textView = (TextView) sbview.findViewById(android.support.design.R.id.snackbar_text); 
    textView.setTextColor(context.getResources().getColor(R.color.primary_light)); 
    snack.show(); 
} 
+0

Problem damit: "dies" muss in "Kontext" geändert werden, also ok dort, und die andere Sache ist, dass, wenn wir Kontext zur Zeile hinzufügen "context.getResources(). GetColor (R.color.primary_light)" , getColor() erscheint als veraltet – Javi

+0

Ich antworte mir selbst, ContextCompat.getColor muss dann verwendet werden ... – Javi

+0

@JaviMar vielleicht sollten Sie ContextCompat als zusätzlichen Parameter an die Funktion übergeben, da die getColor-Methode nach Kontext fragt – SaravInfern

1

Sie können diesen Code verwenden gist

Buchungscode für den Fall, dass der Link

public class UtilSnackbar { 
    /************************************ ShowSnackbar with message, KeepItDisplayedOnScreen for few seconds*****************************/ 
    public static void showSnakbarTypeOne(View rootView, String mMessage) { 
     Snackbar.make(rootView, mMessage, Snackbar.LENGTH_LONG) 
       .setAction("Action", null) 
       .show(); 
    } 

    /************************************ ShowSnackbar with message, KeepItDisplayedOnScreen*****************************/ 
    public static void showSnakbarTypeTwo(View rootView, String mMessage) { 

     Snackbar.make(rootView, mMessage, Snackbar.LENGTH_LONG) 
       .make(rootView, mMessage, Snackbar.LENGTH_INDEFINITE) 
       .setAction("Action", null) 
       .show(); 

    } 

    /************************************ ShowSnackbar without message, KeepItDisplayedOnScreen, OnClickOfOk restrat the activity*****************************/ 
    public static void showSnakbarTypeThree(View rootView, final Activity activity) { 

     Snackbar 
       .make(rootView, "NoInternetConnectivity", Snackbar.LENGTH_INDEFINITE) 
       .setAction("TryAgain", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Intent intent = activity.getIntent(); 
         activity.finish(); 
         activity.startActivity(intent); 
        } 
       }) 
       .setActionTextColor(Color.CYAN) 
       .setCallback(new Snackbar.Callback() { 
        @Override 
        public void onDismissed(Snackbar snackbar, int event) { 
         super.onDismissed(snackbar, event); 
        } 

        @Override 
        public void onShown(Snackbar snackbar) { 
         super.onShown(snackbar); 
        } 
       }) 
       .show(); 

    } 

    /************************************ ShowSnackbar with message, KeepItDisplayedOnScreen, OnClickOfOk restrat the activity*****************************/ 
    public static void showSnakbarTypeFour(View rootView, final Activity activity, String mMessage) { 

     Snackbar 
       .make(rootView, mMessage, Snackbar.LENGTH_INDEFINITE) 
       .setAction("TryAgain", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Intent intent = activity.getIntent(); 
         activity.finish(); 
         activity.startActivity(intent); 
        } 
       }) 
       .setActionTextColor(Color.CYAN) 
       .setCallback(new Snackbar.Callback() { 
        @Override 
        public void onDismissed(Snackbar snackbar, int event) { 
         super.onDismissed(snackbar, event); 
        } 

        @Override 
        public void onShown(Snackbar snackbar) { 
         super.onShown(snackbar); 
        } 
       }) 
       .show(); 

    } 
} 
1

diese Methode Setzen Sie, wo Sie die snackbar zeigen wollen und

protected void displaySnackbar (View view,String s) 
    { 
     Snackbar snack = Snackbar.make(view, s, Snackbar.LENGTH_LONG); 
     View sbview = snack.getView(); 
     sbview.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent)); 
     TextView textView = (TextView) sbview.findViewById(android.support.design.R.id.snackbar_text); 
     textView.setTextColor(getResources().getColor(R.color.primary_light)); 
     snack.show(); 
    }