Skip to content

Instantly share code, notes, and snippets.

@Guilherme-HRamos
Last active August 23, 2018 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guilherme-HRamos/3dc75e794081c975ca0b88d7cf8715df to your computer and use it in GitHub Desktop.
Save Guilherme-HRamos/3dc75e794081c975ca0b88d7cf8715df to your computer and use it in GitHub Desktop.
Inflando um ProgressView por ViewStub
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_progress_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/md_white"
android:gravity="center"
android:clickable="true"
android:focusable="true"
app:layout_behavior="@string/scrooling_behavior"
android:orientation="vertical">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:layout_marginBottom="16dp"/>
<TextView
android:id="@+id/progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Carregando..."
android:textColor="#000000"
android:textSize="16sp"/>
</LinearLayout>
import android.support.annotation.LayoutRes;
import android.view.View;
/**
* Classe criada por Guilherme Ramos em 2018
*/
public interface ProgressHelper {
void showProgress();
void showProgress(String message);
void showProgress(@LayoutRes int layoutToInflate);
void hideProgress();
boolean isVisible();
View getViewInflated();
}
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.TextView;
/**
* Classe criada por Guilherme Ramos em 2018
*/
public class ProgressInflater implements ProgressHelper {
private boolean isVisible = false;
private Context context;
private final ViewGroup mViewGroup;
private ViewStub mViewStub;
private View mProgressView;
/*
caso o viewgroup seja a activity;
public View getContentView() {
return getWindow().getDecorView().findViewById(android.R.id.content) != null ?
getWindow().getDecorView().findViewById(android.R.id.content) :
findViewById(android.R.id.content).getRootView();
}
/*
public ProgressInflater(final Context context, ViewGroup viewGroup) {
this.context = context;
mViewGroup = viewGroup;
}
@Override
public void showProgress() {
if (isVisible || context == null)
return;
mViewStub = new ViewStub(context);
mViewStub.setLayoutResource(R.layout.layout_progress);
mViewGroup.addView(mViewStub);
mProgressView = mViewStub.inflate();
isVisible = true;
}
@Override
public void showProgress(final String message) {
if (context == null)
return;
if (!isVisible) {
mViewStub = new ViewStub(context);
mViewStub.setLayoutResource(R.layout.layout_progress);
mViewGroup.addView(mViewStub);
mProgressView = mViewStub.inflate();
System.out.println("Inflando progress view");
}
if (mProgressView.findViewById(R.id.progress_text) != null)
((TextView) mProgressView.findViewById(R.id.progress_text)).setText(message);
isVisible = false;
}
@Override
public void showProgress(final int layoutToInflate) {
if (context == null)
return;
if (!isVisible) {
mViewStub = new ViewStub(context);
mViewStub.setLayoutResource(layoutToInflate);
mViewGroup.addView(mViewStub);
mProgressView = mViewStub.inflate();
}
isVisible = false;
}
@Override
public void hideProgress() {
if (mViewStub == null)
return;
mViewStub.setVisibility(View.GONE);
isVisible = false;
}
@Override
public boolean isVisible() {
return isVisible;
}
@Override
public View getViewInflated() {
return mProgressView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment