Skip to content

Commit

Permalink
Add setEmptyString method
Browse files Browse the repository at this point in the history
  • Loading branch information
koji-1009 committed Jan 11, 2019
1 parent 8880a85 commit 641e3a2
Showing 1 changed file with 111 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.recyclerview.widget.RecyclerView;

public class EmptyRecyclerView extends RecyclerView {
Expand All @@ -30,8 +35,8 @@ public EmptyRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs,

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EmptyRecyclerView, 0, 0);
int resId = a.getResourceId(R.styleable.EmptyRecyclerView_emptyView, 0);
setEmptyView(resId);
a.recycle();
setEmptyView(resId);
}

@Override
Expand Down Expand Up @@ -66,7 +71,7 @@ public void onDraw(Canvas c) {
}

/**
* Sets the view to show if the adapter is empty
* Set a view to show if the adapter is empty
*
* @param emptyView {@link View}
*/
Expand All @@ -75,7 +80,7 @@ public void setEmptyView(@Nullable View emptyView) {
}

/**
* Sets the view to show if the adapter is empty
* Set a view to show if the adapter is empty
*
* @param resId empty view's layout id
*/
Expand All @@ -86,4 +91,107 @@ public void setEmptyView(@LayoutRes int resId) {

mEmptyView = LayoutInflater.from(getContext()).inflate(resId, null);
}

/**
* Create TextView from a string resource and Gravity,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessageResId resource id of message to show
* @param gravity {@link android.view.Gravity}
*/
public void setEmptyString(@StringRes int emptyMessageResId, int gravity) {
setEmptyString(emptyMessageResId, gravity, null);
}

/**
* Create TextView from a string resource and Gravity and LayoutParams,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessageResId resource id of message to show
* @param gravity {@link android.view.Gravity}
* @param params {@link ViewGroup.LayoutParams}
*/
public void setEmptyString(@StringRes int emptyMessageResId, int gravity, @Nullable ViewGroup.LayoutParams params) {
setEmptyString(emptyMessageResId, gravity, params, 0);
}

/**
* Create TextView from a string resource and Gravity and Style resource,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessageResId resource id of message to show
* @param gravity {@link android.view.Gravity}
* @param styleRes resource id of view style
*/
public void setEmptyString(@StringRes int emptyMessageResId, int gravity, @StyleRes int styleRes) {
setEmptyString(emptyMessageResId, gravity, null, styleRes);
}

/**
* Create TextView from a string resource, Gravity, LayoutParams and Style resource,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessageResId resource id of message to show
* @param gravity {@link android.view.Gravity}
* @param params {@link ViewGroup.LayoutParams}
* @param styleRes resource id of view style
*/
public void setEmptyString(@StringRes int emptyMessageResId, int gravity, @Nullable ViewGroup.LayoutParams params, @StyleRes int styleRes) {
setEmptyString(getContext().getString(emptyMessageResId), gravity, params, styleRes);
}

/**
* Create TextView from a string and Gravity,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessage message to show
* @param gravity {@link android.view.Gravity}
*/
public void setEmptyString(@Nullable String emptyMessage, int gravity) {
setEmptyString(emptyMessage, gravity, null);
}

/**
* Create TextView from a string and Gravity and LayoutParams,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessage message to show
* @param gravity {@link android.view.Gravity}
* @param params {@link ViewGroup.LayoutParams}
*/
public void setEmptyString(@Nullable String emptyMessage, int gravity, @Nullable ViewGroup.LayoutParams params) {
setEmptyString(emptyMessage, gravity, params, 0);
}

/**
* Create TextView from a string and Gravity and Style resource,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessage message to show
* @param gravity {@link android.view.Gravity}
* @param styleRes resource id of view style
*/
public void setEmptyString(@Nullable String emptyMessage, int gravity, @StyleRes int styleRes) {
setEmptyString(emptyMessage, gravity, null, styleRes);
}

/**
* Create TextView from a string, Gravity, LayoutParams and Style resource,
* add to EmptyRecyclerView to show if the adapter is empty.
*
* @param emptyMessage message to show
* @param gravity {@link android.view.Gravity}
* @param params {@link ViewGroup.LayoutParams}
* @param styleRes resource id of view style
*/
public void setEmptyString(@Nullable String emptyMessage, int gravity, @Nullable ViewGroup.LayoutParams params, @StyleRes int styleRes) {
TextView textView = new TextView(new ContextThemeWrapper(getContext(), styleRes));
textView.setText(emptyMessage);
textView.setGravity(gravity);
if (params != null) {
textView.setLayoutParams(params);
}

mEmptyView = textView;
}
}

0 comments on commit 641e3a2

Please sign in to comment.