universum.studios.android.dialog.widget.DialogListView.java Source code

Java tutorial

Introduction

Here is the source code for universum.studios.android.dialog.widget.DialogListView.java

Source

/*
 * =================================================================================================
 *                             Copyright (C) 2017 Universum Studios
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License
 * you may obtain at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * You can redistribute, modify or publish any part of the code written within this file but as it
 * is described in the License, the software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package universum.studios.android.dialog.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.widget.ListView;

import universum.studios.android.dialog.view.DialogView;

/**
 * A {@link ListView} simple implementation that tints its over-scroll effect using the accent color
 * presented within the current dialog theme. This extended list view also draws a shadow at its
 * both vertical (top and bottom) edges.
 *
 * @author Martin Albedinsky
 */
public class DialogListView extends ListView implements DialogView {

    /**
     * Constants ===================================================================================
     */

    /**
     * Log TAG.
     */
    // private static final String TAG = "DialogListView";

    /*
     * Interface ===================================================================================
     */

    /**
     * Static members ==============================================================================
     */

    /**
     * Members =====================================================================================
     */

    /**
     * Helper used to draw vertical edge shadows for this view.
     */
    private VerticalEdgeShadowHelper mEdgeShadowHelper;

    /*
     * Constructors ================================================================================
     */

    /**
     * Same as {@link #DialogListView(Context, AttributeSet)} without attributes.
     */
    public DialogListView(@NonNull Context context) {
        this(context, null);
    }

    /**
     * Same as {@link #DialogListView(Context, AttributeSet, int)} with
     * {@link android.R.attr#listViewStyle android:listViewStyle} as attribute for default style.
     */
    public DialogListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, android.R.attr.listViewStyle);
    }

    /**
     * Same as {@link #DialogListView(Context, AttributeSet, int, int)}  with {@code 0} as default style.
     */
    public DialogListView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init(context, attrs, defStyleAttr, 0);
        OverScrollTintManager.applyOverScrollTint(context);
    }

    /**
     * Creates a new instance of DialogListView for the given <var>context</var>.
     *
     * @param context      Context in which will be the new view presented.
     * @param attrs        Set of Xml attributes used to configure the new instance of this view.
     * @param defStyleAttr An attribute which contains a reference to a default style resource for
     *                     this view within a theme of the given context.
     * @param defStyleRes  Resource id of the default style for the new view.
     */
    @SuppressWarnings("unused")
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public DialogListView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr,
            @StyleRes int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.init(context, attrs, defStyleAttr, defStyleRes);
    }

    /*
     * Methods =====================================================================================
     */

    /**
     * Called from one of constructors of this view to perform its initialization.
     * <p>
     * Initialization is done via parsing of the specified <var>attrs</var> set and obtaining for
     * this view specific data from it that can be used to configure this new view instance. The
     * specified <var>defStyleAttr</var> and <var>defStyleRes</var> are used to obtain default data
     * from the current theme provided by the specified <var>context</var>.
     */
    private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this.mEdgeShadowHelper = new VerticalEdgeShadowHelper(this);
    }

    /**
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mEdgeShadowHelper.onViewSizeChanged(w, h);
    }

    /**
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        final boolean shadowsVisible = ViewCompat.canScrollVertically(this, -1)
                || ViewCompat.canScrollVertically(this, 1);
        mEdgeShadowHelper.setTopShadowVisible(shadowsVisible);
        mEdgeShadowHelper.setBottomShadowVisible(shadowsVisible);
    }

    /**
     */
    @Override
    public boolean verifyDrawable(@NonNull Drawable drawable) {
        return mEdgeShadowHelper.verifyShadowDrawable(drawable) || super.verifyDrawable(drawable);
    }

    /**
      */
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        mEdgeShadowHelper.drawShadows(canvas);
    }

    /*
     * Inner classes ===============================================================================
     */
}