下面列出了怎么用android.graphics.drawable.InsetDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Drawable parse(ParseRequest request) {
final Context ctx = request.context();
final AttributeSet attrs = request.attrs();
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.Folivora_Inset);
int insetAll = a.getDimensionPixelSize(R.styleable.Folivora_Inset_insetAll, 0);
int[] ints = {
R.styleable.Folivora_Inset_insetLeft,
R.styleable.Folivora_Inset_insetTop,
R.styleable.Folivora_Inset_insetRight,
R.styleable.Folivora_Inset_insetBottom
};
for (int i = 0; i < ints.length; i++) {
ints[i] = a.getDimensionPixelSize(ints[i], insetAll);
}
final Drawable child = getDrawable(ctx, a, attrs, R.styleable.Folivora_Inset_insetDrawable);
a.recycle();
return new InsetDrawable(child, ints[0], ints[1], ints[2], ints[3]);
}
private Drawable makeDefaultThumbDrawable() {
StateListDrawable stateListDrawable = new StateListDrawable();
GradientDrawable pressedDrawable = new GradientDrawable();
pressedDrawable.setColor(thumbPressedColor);
final float radius = width / 2f;
final int inset = dp2px(FASTSCROLLER_THUMB_INSET_TOP_BOTTOM_RIGHT);// inset
final int insetLeft = width - inset - dp2px(FASTSCROLLER_THUMB_WIDTH);
pressedDrawable.setCornerRadius(radius);
stateListDrawable.addState(DRAWABLE_STATE_PRESSED, new InsetDrawable(pressedDrawable, insetLeft, inset,
inset, inset));
GradientDrawable normalDrawable = new GradientDrawable();
normalDrawable.setColor(thumbNormalColor);
normalDrawable.setCornerRadius(radius);
stateListDrawable.addState(DRAWABLE_STATE_DEFAULT, new InsetDrawable(normalDrawable, insetLeft, inset,
inset, inset));
return stateListDrawable;
}
public static boolean containsNinePatch(Drawable drawable) {
drawable = getWrapperDrawable(drawable);
if (drawable instanceof NinePatchDrawable || drawable instanceof InsetDrawable || drawable instanceof LayerDrawable) {
return true;
} else if (drawable instanceof StateListDrawable) {
final DrawableContainer.DrawableContainerState containerState = ((DrawableContainer.DrawableContainerState) drawable.getConstantState());
//can't get containState from drawable which is containing DrawableWrapperDonut
//https://code.google.com/p/android/issues/detail?id=169920
if (containerState == null) {
return true;
}
for (Drawable dr : containerState.getChildren()) {
dr = getWrapperDrawable(dr);
if (dr instanceof NinePatchDrawable || dr instanceof InsetDrawable || dr instanceof LayerDrawable) {
return true;
}
}
}
return false;
}
@Override
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowmanager = window.getWindowManager();
windowmanager.getDefaultDisplay().getMetrics(displayMetrics);
window.setLayout(
Math.min(DimenUtils.dpToPx(displayMetrics.widthPixels > displayMetrics.heightPixels ? 800 : 500),
(int) (displayMetrics.widthPixels * 0.9f)),
WindowManager.LayoutParams.WRAP_CONTENT
);
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(ColorUtils.fromAttr(new ContextThemeWrapper(getContext(), getTheme()),
android.R.attr.colorBackground, Color.WHITE));
drawable.setCornerRadius(cornerRadius);
window.setBackgroundDrawable(new InsetDrawable(drawable, DimenUtils.dpToPx(12)));
}
public static boolean containsNinePatch(Drawable drawable) {
drawable = getWrapperDrawable(drawable);
if (drawable instanceof NinePatchDrawable
|| drawable instanceof InsetDrawable
|| drawable instanceof LayerDrawable) {
return true;
} else if (drawable instanceof StateListDrawable) {
final DrawableContainer.DrawableContainerState containerState = ((DrawableContainer.DrawableContainerState) drawable.getConstantState());
//can't get containState from drawable which is containing DrawableWrapperDonut
//https://code.google.com/p/android/issues/detail?id=169920
if (containerState == null) {
return true;
}
for (Drawable dr : containerState.getChildren()) {
dr = getWrapperDrawable(dr);
if (dr instanceof NinePatchDrawable
|| dr instanceof InsetDrawable
|| dr instanceof LayerDrawable) {
return true;
}
}
}
return false;
}
private static boolean shouldMutateBackground(Drawable drawable) {
if (Build.VERSION.SDK_INT >= 16) {
// For SDK 16+, we should be fine mutating the drawable
return true;
}
if (drawable instanceof LayerDrawable) {
return Build.VERSION.SDK_INT >= 16;
} else if (drawable instanceof InsetDrawable) {
return Build.VERSION.SDK_INT >= 14;
} else if (drawable instanceof DrawableContainer) {
// If we have a DrawableContainer, let's traverse it's child array
final Drawable.ConstantState state = drawable.getConstantState();
if (state instanceof DrawableContainer.DrawableContainerState) {
final DrawableContainer.DrawableContainerState containerState =
(DrawableContainer.DrawableContainerState) state;
for (Drawable child : containerState.getChildren()) {
if (!shouldMutateBackground(child)) {
return false;
}
}
}
}
return true;
}
/**
* Creates a {@link MaterialShapeDrawable} to use as the {@code itemBackground} and wraps it in an
* {@link InsetDrawable} for margins.
*
* @param a The TintTypedArray containing the resolved NavigationView style attributes.
*/
@NonNull
private final Drawable createDefaultItemBackground(@NonNull TintTypedArray a) {
int shapeAppearanceResId = a.getResourceId(R.styleable.NavigationView_itemShapeAppearance, 0);
int shapeAppearanceOverlayResId =
a.getResourceId(R.styleable.NavigationView_itemShapeAppearanceOverlay, 0);
MaterialShapeDrawable materialShapeDrawable =
new MaterialShapeDrawable(
ShapeAppearanceModel.builder(
getContext(), shapeAppearanceResId, shapeAppearanceOverlayResId)
.build());
materialShapeDrawable.setFillColor(
MaterialResources.getColorStateList(
getContext(), a, R.styleable.NavigationView_itemShapeFillColor));
int insetLeft = a.getDimensionPixelSize(R.styleable.NavigationView_itemShapeInsetStart, 0);
int insetTop = a.getDimensionPixelSize(R.styleable.NavigationView_itemShapeInsetTop, 0);
int insetRight = a.getDimensionPixelSize(R.styleable.NavigationView_itemShapeInsetEnd, 0);
int insetBottom = a.getDimensionPixelSize(R.styleable.NavigationView_itemShapeInsetBottom, 0);
return new InsetDrawable(materialShapeDrawable, insetLeft, insetTop, insetRight, insetBottom);
}
@Nullable
private MaterialShapeDrawable getMaterialShapeDrawable(boolean getSurfaceColorStrokeDrawable) {
if (rippleDrawable != null && rippleDrawable.getNumberOfLayers() > 0) {
if (IS_LOLLIPOP) {
InsetDrawable insetDrawable = (InsetDrawable) rippleDrawable.getDrawable(0);
LayerDrawable layerDrawable = (LayerDrawable) insetDrawable.getDrawable();
return (MaterialShapeDrawable)
layerDrawable.getDrawable(getSurfaceColorStrokeDrawable ? 0 : 1);
} else {
return (MaterialShapeDrawable)
rippleDrawable.getDrawable(getSurfaceColorStrokeDrawable ? 0 : 1);
}
}
return null;
}
@Override
public void onStart() {
super.onStart();
Window window = requireDialog().getWindow();
// Dialogs use a background with an InsetDrawable by default, so we have to replace it.
if (fullscreen) {
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
window.setBackgroundDrawable(background);
} else {
window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int inset =
getResources().getDimensionPixelOffset(R.dimen.mtrl_calendar_dialog_background_inset);
Rect insets = new Rect(inset, inset, inset, inset);
window.setBackgroundDrawable(new InsetDrawable(background, inset, inset, inset, inset));
window
.getDecorView()
.setOnTouchListener(new InsetDialogOnTouchListener(requireDialog(), insets));
}
startPickerFragment();
}
private static Drawable createButtonShape(Context context, int color) {
// Translation of Lollipop's xml button-bg definition to Java
int paddingH = context.getResources()
.getDimensionPixelSize(R.dimen.button_padding_horizontal_material);
int paddingV = context.getResources()
.getDimensionPixelSize(R.dimen.button_padding_vertical_material);
int insetH = context.getResources()
.getDimensionPixelSize(R.dimen.button_inset_horizontal_material);
int insetV = context.getResources()
.getDimensionPixelSize(R.dimen.button_inset_vertical_material);
float[] outerRadii = new float[8];
Arrays.fill(outerRadii, CORNER_RADIUS);
RoundRectShape r = new RoundRectShape(outerRadii, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(r);
shapeDrawable.getPaint().setColor(color);
shapeDrawable.setPadding(paddingH, paddingV, paddingH, paddingV);
return new InsetDrawable(shapeDrawable,
insetH, insetV, insetH, insetV);
}
private void init(Context context) {
setBackground(new InsetDrawable(new Triangle(context.getResources()
.getColor(R.color.vote_state_empty)),
(int) Views.convertDpToPixel(12, context),
(int) Views.convertDpToPixel(4, context),
(int) Views.convertDpToPixel(12, context),
(int) Views.convertDpToPixel(4, context)));
voteState = Vote.VoteState.EMPTY;
setOnClickListener(v -> {
if (onVoteClickListener != null) {
Vote.VoteState from = this.voteState;
Vote.VoteState to = (this.voteState == Vote.VoteState.EMPTY
? Vote.VoteState.UP
: Vote.VoteState.EMPTY);
onVoteClickListener.onVoteClicked(from, to);
}
});
}
public static Drawable tintDrawable(Drawable drawable, int fillColor, int strokeColor, int strokePixels)
{
Drawable d = null;
try {
d = tintDrawable((InsetDrawable)drawable, fillColor, strokeColor, strokePixels);
} catch (ClassCastException e) {
try {
//noinspection ConstantConditions
d = tintDrawable((LayerDrawable)drawable, fillColor, strokeColor, strokePixels);
} catch (ClassCastException e2) {
try {
//noinspection ConstantConditions
d = tintDrawable((GradientDrawable)drawable, fillColor, strokeColor, strokePixels);
} catch (ClassCastException e3) {
Log.e("tintDrawable", "");
}
}
}
return d;
}
/**
* @param drawable a ShapeDrawable
* @param fillColor the fill color
* @param strokeColor the stroke color
* @return a GradientDrawable with the given fill and stroke
*/
public static Drawable tintDrawable(InsetDrawable drawable, int fillColor, int strokeColor, int strokePixels)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
try {
GradientDrawable gradient = (GradientDrawable)drawable.getDrawable();
if (gradient != null)
{
SuntimesUtils.tintDrawable(gradient, fillColor, strokeColor, strokePixels);
return drawable;
} else {
Log.w("tintDrawable", "failed to apply color! Null inset drawable.");
return drawable;
}
} catch (ClassCastException e) {
Log.w("tintDrawable", "failed to apply color! " + e);
return drawable;
}
} else {
Log.w("tintDrawable", "failed to apply color! InsetDrawable.getDrawable requires api 19+");
return drawable; // not supported
}
}
public Drawable getRectWithCustomSize(Context context, String leftText, String rightText, boolean selected) {
TextDrawable.IBuilder builder = TextDrawable.builder()
.beginConfig()
.width(toPx(context, 29))
.withBorder(toPx(context, 2))
.textColor(selected ? getTextColorSelected() : getTextColorUnselected())
.endConfig()
.rect();
TextDrawable left = builder
.build(leftText, mGenerator.getColor(leftText));
TextDrawable right = builder
.build(rightText, mGenerator.getColor(rightText));
Drawable[] layerList = {
new InsetDrawable(left, 0, 0, toPx(context, 31), 0),
new InsetDrawable(right, toPx(context, 31), 0, 0, 0)
};
return new LayerDrawable(layerList);
}
private void updateHandleColorsAndInset() {
StateListDrawable drawable = new StateListDrawable();
if (!RecyclerFastScrollerUtils.isRTL(getContext())) {
drawable.addState(View.PRESSED_ENABLED_STATE_SET,
new InsetDrawable(new ColorDrawable(mHandlePressedColor), mBarInset, 0, 0, 0));
drawable.addState(View.EMPTY_STATE_SET,
new InsetDrawable(new ColorDrawable(mHandleNormalColor), mBarInset, 0, 0, 0));
} else {
drawable.addState(View.PRESSED_ENABLED_STATE_SET,
new InsetDrawable(new ColorDrawable(mHandlePressedColor), 0, 0, mBarInset, 0));
drawable.addState(View.EMPTY_STATE_SET,
new InsetDrawable(new ColorDrawable(mHandleNormalColor), 0, 0, mBarInset, 0));
}
RecyclerFastScrollerUtils.setViewBackground(mHandle, drawable);
}
@Override
public View provideHandleView(ViewGroup container) {
handle = new View(getContext());
int verticalInset = getScroller().isVertical() ? 0 : getContext().getResources().getDimensionPixelSize(R.dimen.fastscroll__handle_inset);
int horizontalInset = !getScroller().isVertical() ? 0 : getContext().getResources().getDimensionPixelSize(R.dimen.fastscroll__handle_inset);
InsetDrawable handleBg = new InsetDrawable(ContextCompat.getDrawable(getContext(), R.drawable.fastscroll__default_handle), horizontalInset, verticalInset, horizontalInset, verticalInset);
Utils.setBackground(handle, handleBg);
int handleWidth = getContext().getResources().getDimensionPixelSize(getScroller().isVertical() ? R.dimen.fastscroll__handle_clickable_width : R.dimen.fastscroll__handle_height);
int handleHeight = getContext().getResources().getDimensionPixelSize(getScroller().isVertical() ? R.dimen.fastscroll__handle_height : R.dimen.fastscroll__handle_clickable_width);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(handleWidth, handleHeight);
handle.setLayoutParams(params);
return handle;
}
public Drawable getRectWithCustomSize() {
String leftText = "I";
String rightText = "J";
TextDrawable.IBuilder builder = TextDrawable.builder()
.beginConfig()
.width(toPx(29))
.withBorder(toPx(2))
.endConfig()
.rect();
TextDrawable left = builder
.build(leftText, mGenerator.getColor(leftText));
TextDrawable right = builder
.build(rightText, mGenerator.getColor(rightText));
Drawable[] layerList = {
new InsetDrawable(left, 0, 0, toPx(31), 0),
new InsetDrawable(right, toPx(31), 0, 0, 0)
};
return new LayerDrawable(layerList);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
layoutManager = new LinearLayoutManager(this.getContext());
chatList.setLayoutManager(layoutManager);
chatList.setAdapter(adapter);
Drawable divider = ContextCompat.getDrawable(chatList.getContext(), R.drawable.line_divider);
int avatarWidth = getResources().getDimensionPixelSize(R.dimen.list_item_avatar_size);
int itemPadding = getResources().getDimensionPixelSize(R.dimen.list_item_chat_padding);
InsetDrawable insetDivider = new InsetDrawable(divider, avatarWidth + (itemPadding * 2), 0, 0, 0);
DividerItemDecoration itemDecoration = new DividerItemDecoration(chatList.getContext(), layoutManager.getOrientation());
itemDecoration.setDrawable(insetDivider);
chatList.addItemDecoration(itemDecoration);
adapter.setListener(this);
chatList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0 && addChatFab.getVisibility() == View.VISIBLE) {
addChatFab.hide();
} else if (dy < 0 && addChatFab.getVisibility() != View.VISIBLE) {
addChatFab.show();
}
}
});
addChatFab.setOnClickListener(v -> presenter.onClickCreateNewChatButton(buildRevealSettings()));
return view;
}
public void mount(T drawable, DrawableMatrix matrix) {
if (mDrawable == drawable) {
return;
}
if (mDrawable != null) {
setDrawableVisibilitySafe(false, false);
mDrawable.setCallback(null);
}
mDrawable = drawable;
if (mDrawable != null) {
setDrawableVisibilitySafe(isVisible(), false);
mDrawable.setCallback(this);
}
mMatrix = matrix;
// We should clip rect if either the transformation matrix needs so or
// if a ColorDrawable in Gingerbread is being drawn because it doesn't
// respect its bounds.
mShouldClipRect =
(mMatrix != null && mMatrix.shouldClipRect())
|| (Build.VERSION.SDK_INT < HONEYCOMB && mDrawable instanceof ColorDrawable)
|| (mDrawable instanceof InsetDrawable);
invalidateSelf();
}
/**
* Update the background for the reveal view and content view based on the background padding.
*/
protected void updateBackground(int paddingLeft, int paddingTop,
int paddingRight, int paddingBottom) {
mRevealView.setBackground(new InsetDrawable(mBaseDrawable,
paddingLeft, paddingTop, paddingRight, paddingBottom));
mContent.setBackground(new InsetDrawable(mBaseDrawable,
paddingLeft, paddingTop, paddingRight, paddingBottom));
}
@Override
protected void updateBackground(
int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) {
if (mLauncher.getDeviceProfile().isVerticalBarLayout()) {
getRevealView().setBackground(new InsetDrawable(mBaseDrawable,
paddingLeft, paddingTop, paddingRight, paddingBottom));
getContentView().setBackground(
new InsetDrawable(new ColorDrawable(Color.TRANSPARENT),
paddingLeft, paddingTop, paddingRight, paddingBottom));
} else {
getRevealView().setBackground(mBaseDrawable);
}
}
private void addDividerItemDecoration(@ColorInt int color, int orientation,
int paddingLeft, int paddingTop,
int paddingRight, int paddingBottom) {
DividerItemDecoration decor = new DividerItemDecoration(getContext(), orientation);
if (color != 0) {
ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
shapeDrawable.setIntrinsicHeight(Utils.dpToPx(getContext(), 1));
shapeDrawable.setIntrinsicWidth(Utils.dpToPx(getContext(), 1));
shapeDrawable.getPaint().setColor(color);
InsetDrawable insetDrawable = new InsetDrawable(shapeDrawable, paddingLeft, paddingTop, paddingRight, paddingBottom);
decor.setDrawable(insetDrawable);
}
decor.setShowLastDivider(showLastDivider);
addItemDecoration(decor);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final Context context = inflater.getContext();
final Resources res = context.getResources();
final View view = inflater.inflate(R.layout.fragment_directory, container, false);
mProgressBar = (MaterialProgressBar) view.findViewById(R.id.progressBar);
mEmptyView = (CompatTextView)view.findViewById(android.R.id.empty);
mListView = (ListView) view.findViewById(R.id.list);
mListView.setOnItemClickListener(mItemListener);
mListView.setMultiChoiceModeListener(mMultiListener);
mListView.setRecyclerListener(mRecycleListener);
// Indent our list divider to align with text
final Drawable divider = mListView.getDivider();
final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
if (insetLeft) {
mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
} else {
mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
}
mGridView = (GridView) view.findViewById(R.id.grid);
mGridView.setOnItemClickListener(mItemListener);
mGridView.setMultiChoiceModeListener(mMultiListener);
mGridView.setRecyclerListener(mRecycleListener);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
final Resources res = getActivity().getResources();
fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(this);
if(isTelevision()){
fab.setVisibility(View.GONE);
}
mProgressBar = (MaterialProgressBar) view.findViewById(R.id.progressBar);
mEmptyView = (CompatTextView)view.findViewById(android.R.id.empty);
mListView = (ListView) view.findViewById(R.id.list);
mListView.setOnItemClickListener(mItemListener);
if(isTelevision()) {
mListView.setOnItemLongClickListener(mItemLongClickListener);
}
fab.attachToListView(mListView);
// Indent our list divider to align with text
final Drawable divider = mListView.getDivider();
final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
if (insetLeft) {
mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
} else {
mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final Context context = inflater.getContext();
final Resources res = context.getResources();
final View view = inflater.inflate(R.layout.fragment_directory, container, false);
mProgressBar = (MaterialProgressBar) view.findViewById(R.id.progressBar);
mEmptyView = (CompatTextView)view.findViewById(android.R.id.empty);
mListView = (ListView) view.findViewById(R.id.list);
mListView.setOnItemClickListener(mItemListener);
mListView.setMultiChoiceModeListener(mMultiListener);
mListView.setRecyclerListener(mRecycleListener);
// Indent our list divider to align with text
final Drawable divider = mListView.getDivider();
final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
if (insetLeft) {
mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
} else {
mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
}
mGridView = (GridView) view.findViewById(R.id.grid);
mGridView.setOnItemClickListener(mItemListener);
mGridView.setMultiChoiceModeListener(mMultiListener);
mGridView.setRecyclerListener(mRecycleListener);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
final Resources res = getActivity().getResources();
fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(this);
if(isTelevision()){
fab.setVisibility(View.GONE);
}
mProgressBar = (MaterialProgressBar) view.findViewById(R.id.progressBar);
mEmptyView = (CompatTextView)view.findViewById(android.R.id.empty);
mListView = (ListView) view.findViewById(R.id.list);
mListView.setOnItemClickListener(mItemListener);
if(isTelevision()) {
mListView.setOnItemLongClickListener(mItemLongClickListener);
}
fab.attachToListView(mListView);
// Indent our list divider to align with text
final Drawable divider = mListView.getDivider();
final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
if (insetLeft) {
mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
} else {
mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final Context context = inflater.getContext();
final Resources res = context.getResources();
final View view = inflater.inflate(R.layout.fragment_directory, container, false);
mProgressBar = (MaterialProgressBar) view.findViewById(R.id.progressBar);
mEmptyView = (CompatTextView)view.findViewById(android.R.id.empty);
mListView = (ListView) view.findViewById(R.id.list);
mListView.setOnItemClickListener(mItemListener);
mListView.setMultiChoiceModeListener(mMultiListener);
mListView.setRecyclerListener(mRecycleListener);
// Indent our list divider to align with text
final Drawable divider = mListView.getDivider();
final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
if (insetLeft) {
mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
} else {
mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
}
mGridView = (GridView) view.findViewById(R.id.grid);
mGridView.setOnItemClickListener(mItemListener);
mGridView.setMultiChoiceModeListener(mMultiListener);
mGridView.setRecyclerListener(mRecycleListener);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
final Resources res = getActivity().getResources();
fab = (FloatingActionButton)view.findViewById(R.id.fab);
fab.setOnClickListener(this);
if(isTelevision()){
fab.setVisibility(View.GONE);
}
mProgressBar = (MaterialProgressBar) view.findViewById(R.id.progressBar);
mEmptyView = (CompatTextView)view.findViewById(android.R.id.empty);
mListView = (ListView) view.findViewById(R.id.list);
mListView.setOnItemClickListener(mItemListener);
if(isTelevision()) {
mListView.setOnItemLongClickListener(mItemLongClickListener);
}
fab.attachToListView(mListView);
// Indent our list divider to align with text
final Drawable divider = mListView.getDivider();
final boolean insetLeft = res.getBoolean(R.bool.list_divider_inset_left);
final int insetSize = res.getDimensionPixelSize(R.dimen.list_divider_inset);
if (insetLeft) {
mListView.setDivider(new InsetDrawable(divider, insetSize, 0, 0, 0));
} else {
mListView.setDivider(new InsetDrawable(divider, 0, 0, insetSize, 0));
}
}
/**
* Some drawable implementations have problems with mutation. This method returns false if
* there is a known issue in the given drawable's implementation.
*/
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
return false;
} else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
// GradientDrawable has a bug pre-ICS which results in mutate() resulting
// in loss of color
return false;
} else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
return false;
}
if (drawable instanceof DrawableContainer) {
// If we have a DrawableContainer, let's traverse it's child array
final Drawable.ConstantState state = drawable.getConstantState();
if (state instanceof DrawableContainer.DrawableContainerState) {
final DrawableContainer.DrawableContainerState containerState =
(DrawableContainer.DrawableContainerState) state;
for (final Drawable child : containerState.getChildren()) {
if (!canSafelyMutateDrawable(child)) {
return false;
}
}
}
} else if (SkinCompatVersionUtils.isV4DrawableWrapper(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4DrawableWrapperWrappedDrawable(drawable));
} else if (SkinCompatVersionUtils.isV4WrappedDrawable(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4WrappedDrawableWrappedDrawable(drawable));
} else if (SkinCompatVersionUtils.isV7DrawableWrapper(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV7DrawableWrapperWrappedDrawable(drawable));
} else if (drawable instanceof ScaleDrawable) {
Drawable scaleDrawable = ((ScaleDrawable) drawable).getDrawable();
if (scaleDrawable != null) {
return canSafelyMutateDrawable(scaleDrawable);
}
}
return true;
}
/**
* Some drawable implementations have problems with mutation. This method returns false if
* there is a known issue in the given drawable's implementation.
*/
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
return false;
} else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
// GradientDrawable has a bug pre-ICS which results in mutate() resulting
// in loss of color
return false;
} else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
return false;
}
if (drawable instanceof DrawableContainer) {
// If we have a DrawableContainer, let's traverse it's child array
final Drawable.ConstantState state = drawable.getConstantState();
if (state instanceof DrawableContainer.DrawableContainerState) {
final DrawableContainer.DrawableContainerState containerState =
(DrawableContainer.DrawableContainerState) state;
for (final Drawable child : containerState.getChildren()) {
if (!canSafelyMutateDrawable(child)) {
return false;
}
}
}
} else if (SkinCompatVersionUtils.isV4DrawableWrapper(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4DrawableWrapperWrappedDrawable(drawable));
} else if (SkinCompatVersionUtils.isV4WrappedDrawable(drawable)) {
return canSafelyMutateDrawable(SkinCompatVersionUtils.getV4WrappedDrawableWrappedDrawable(drawable));
} else if (drawable instanceof DrawableWrapper) {
return canSafelyMutateDrawable(((DrawableWrapper) drawable).getWrappedDrawable());
} else if (drawable instanceof ScaleDrawable) {
return canSafelyMutateDrawable(((ScaleDrawable) drawable).getDrawable());
}
return true;
}