Android Open Source - example Card Header






From Project

Back to project page example.

License

The source code is released under:

Apache License

If you think the Android project example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * ******************************************************************************
 *   Copyright (c) 2013 Gabriele Mariotti.
 *//www . j  a  v a2  s  .  c o  m
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.library.internal;

import android.content.Context;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import it.gmariotti.cardslib.library.R;
import it.gmariotti.cardslib.library.internal.base.BaseCard;

/**
 * Card Header Model.
 * </p>
 * You can customize this component. See https://github.com/gabrielemariotti/cardslib/tree/master/doc/HEADER.md for more information.
 * </p>
 * You can fully customize this component with:
 * <ul>
 * <li>using your innerLayout in constructor</li>
 * <li>setting your elements with {@link #setupInnerViewElements(android.view.ViewGroup, android.view.View)} method.</li>
 *</ul>
 * </p>
 * <b>Usage:</b>
 * <pre><code>
 *       //Create a Card
 *       Card card = new Card(getContext());
 *
 *       //Create a CardHeader
 *       CardHeader header = new CardHeader(getContext());
 *
 *       //Add header to card
 *       card.addCardHeader(header);
 *
 * </code></pre>
 *
 * </p>
 * You can customize buttons behaviour with {@link #setButtonOverflowVisible(boolean)} ,{@link #setButtonExpandVisible(boolean)}, {@link #setOtherButtonVisible(boolean)}
 * </p>
 * You can attach a Popup Menu to overflowMenu with {@link #setPopupMenu(int, CardHeader.OnClickCardHeaderPopupMenuListener)}
 * <pre><code>
 * header.setPopupMenu(R.menu.popupmain, new CardHeader.OnClickCardHeaderPopupMenuListener(){
 *
 *       public void onMenuItemClick(BaseCard card, MenuItem item) {
 *                 mPopupMenuListener.doSomething(...);
 *       }
 *  });
 * </code></pre>
 * </p>
 * You can attach a {@link CardHeader.OnClickCardHeaderOtherButtonListener} to button to listen any callback when
 * other button is clicked.
 * <pre><code>
 *     header.setOtherButtonClickListener(new CardHeader.OnClickCardHeaderOtherButtonListener() {
 *
 *         public void onButtonItemClick(Card card, View view) {
 *                //do something...
 *         }
 *      });
 * </code></pre>
 * </p>
 * Use your style and drawable files to customize buttons style.
 * </p>
 * Also you can use a different header layout using a custom attr in main card layout.
 * <pre><code>
 *
 *  <it.gmariotti.cardslib.library.view.component.CardHeaderView
 *       android:id="@+id/card_header_layout"
 *       android:layout_width="match_parent"
 *       android:layout_height="wrap_content"
 *       card:card_header_layout_resourceID="@layout/my_header_layout" />
 * </code></pre>
 * </p>
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class CardHeader extends BaseCard {

    /**
     * Indicates to make visible the Button to expand/collapse
     */
    protected boolean mIsButtonExpandVisible=false;

    /**
     * Indicates to make visible the Overflow Button to open a popup menu
     */
    protected boolean mIsButtonOverflowVisible=false;

    /**
     * Indicates to make visible another Button
     */
    protected boolean mIsOtherButtonVisible=false;

    /**
     *  Resource ID for PopMenu
     */
    protected int mPopupMenu=NO_POPUP_MENU;

    /**
     * Listener invoked when a item in PopupMenu is clicked
     */
    protected OnClickCardHeaderPopupMenuListener mPopupMenuListener;


    /**
     * Listener invoked when Other Button is clicked
     */
    protected OnClickCardHeaderOtherButtonListener mOtherButtonClickListener;


    /**
     * Drawable used by otherButton.
     * </p>
     * Otherwise you can use this your xml style.
     * <pre>
     * <code>
     *  <style name="card.header_button_base.other">
     *      <item name="android:background">@drawable/card_menu_button_other</item>
     *  </style>
     *  </code>
     *  </pre>
     */
    protected int mOtherButtonDrawable=0;

    /**
     * Use this value to remove popup on overflow button
     */
    public static int NO_POPUP_MENU = -1;


    /**
     * Interface for custom overflow animation
     */
    public interface CustomOverflowAnimation {

        /**
         * @return the bitmap from custom source
         */
       void doAnimation(Card card,View imageOverflow);
    }

    protected CustomOverflowAnimation mCustomOverflowAnimation = null;

    /**
     * Indicates if overflow icon is selected
     */
    protected boolean mIsOverflowSelected=false;

    // -------------------------------------------------------------
    // Constructors
    // -------------------------------------------------------------

    /**
     * Constructor with a base inner layout defined by R.layout.inner_base_header
     *
     * @param context  context
     */
    public CardHeader(Context context) {
        this(context,R.layout.inner_base_header);
    }

    /**
     * Constructor with a custom inner layout.
     *
     * @param context context
     * @param innerLayout   layout resource ID
     */
    public CardHeader(Context context,int innerLayout) {
        super(context);
        mInnerLayout= innerLayout;
    }


    // -------------------------------------------------------------
    // Interfaces
    // -------------------------------------------------------------

    /**
     * Interface to handle any callback when a item in popup menu is clicked
     */
    public interface OnClickCardHeaderPopupMenuListener {
        public void onMenuItemClick(BaseCard card, MenuItem item);
    }

    /**
     * Interface to handle callbacks when ExpandButton is clicked
     * Currently is never used
     */
    public interface OnClickExpandListener {
        public void onButtonExpandClick(BaseCard card, MenuItem item);
    }

    /**
     * Interface to handle callbacks when Other Button is clicked
     */
    public interface OnClickCardHeaderOtherButtonListener {
        public void onButtonItemClick(Card card, View view);
    }


    // -------------------------------------------------------------
    // Popup Menu
    // -------------------------------------------------------------

    /**
     * Sets a popup menu for overflow button.
     * <p/>
     * Setting the menu resource to {@linl NO_POPUP_MENU} disables the menu for this card.
     *
     * @param menuRes  The menu resource ID to use for the card's popup menu.
     * @param listener A listener invoked when an option in the popup menu is tapped by the user.
     */
    public void setPopupMenu(int menuRes, OnClickCardHeaderPopupMenuListener listener) {
        mPopupMenu = menuRes;
        mPopupMenuListener = listener;

        if (menuRes==NO_POPUP_MENU){
            mIsButtonOverflowVisible=false;
            listener=null;
        }else{
            mIsButtonOverflowVisible=true;
        }
    }

    // -------------------------------------------------------------
    // Other Button
    // -------------------------------------------------------------



    // -------------------------------------------------------------
    // CustomOverflowAnimator
    // -------------------------------------------------------------


    /**
     * Returns the custom animation on Overflow icon
     *
     * @return the listener
     */
    public CustomOverflowAnimation getCustomOverflowAnimation() {
        return  mCustomOverflowAnimation;
    }

    /**
     * Sets the listener for a custom animation on Overflow icon
     *
     * @param customAnimation
     */
    public void setCustomOverflowAnimation(CustomOverflowAnimation customAnimation) {
        this.mCustomOverflowAnimation = customAnimation;

        if (mCustomOverflowAnimation==null){
            mIsButtonOverflowVisible=false;
        }else{
            mIsButtonOverflowVisible=true;
        }
    }


    public boolean isOverflowSelected() {
        return mIsOverflowSelected;
    }

    public void setOverflowSelected(boolean isOverflowSelected) {
        mIsOverflowSelected = isOverflowSelected;
    }



    // -------------------------------------------------------------
    // Inner View
    // -------------------------------------------------------------

    /**
     * Inflates the inner layout and adds to parent layout.
     * Then calls {@link #setupInnerViewElements(android.view.ViewGroup, android.view.View)} method
     * to setup all values.
     * </p>
     * You can provide your custom layout.
     * You can use a xml layout with {@link CardHeader#setInnerLayout(int)} or with constructor
     * {@link CardHeader#CardHeader(android.content.Context, int)}.
     *
     * Then customize #setupInnerViewElements to set your values.
     *
     * @param context context
     * @param parent  Inner Layout
     * @return view
     */
    @Override
    public View getInnerView(Context context, ViewGroup parent) {

        View view= super.getInnerView(context, parent);

        //This provide a simple implementation with a single title
        if (view!=null){
            //Add inner view to parent
            parent.addView(view);

            //Setup value
            if (mInnerLayout>-1 ){
                setupInnerViewElements(parent,view);
            }
        }
        return view;
    }

    // -------------------------------------------------------------
    // Setup Inner view Elements
    // -------------------------------------------------------------

    /**
     * This method sets values to header elements and customizes view.
     *
     * Override this method to set your elements inside InnerView.
     *
     * @param parent  parent view (Inner Frame)
     * @param view   Inner View
     */
    @Override
    public void setupInnerViewElements(ViewGroup parent,View view){

        //Add simple title to header
        if (view!=null){
            TextView mTitleView=(TextView) view.findViewById(R.id.card_header_inner_simple_title);
            if (mTitleView!=null)
                mTitleView.setText(mTitle);
        }

    }

    // -------------------------------------------------------------
    //  Getters and Setters
    // -------------------------------------------------------------

    /**
     * Return the popup listener invoked when a item in PopupMenu is clicked
     *
     * @return  popup listener
     */
    public OnClickCardHeaderPopupMenuListener getPopupMenuListener() {
        return mPopupMenuListener;
    }

    /**
     * Sets the popup listener invoked when a item in PopupMenu is clicked
     *
     * @param popupMenuListener  popup listener
     */
    public void setPopupMenuListener(OnClickCardHeaderPopupMenuListener popupMenuListener) {
        mPopupMenuListener = popupMenuListener;
    }

    /**
     * Indicates if expand/collapse button is visible
     *
     * @return <code>true</code> if the button is visible
     */
    public boolean isButtonExpandVisible() {
        return mIsButtonExpandVisible;
    }

    /**
     * Sets visibility to expand/collapse button
     *
     * @param buttonExpandVisible
     */
    public void setButtonExpandVisible(boolean buttonExpandVisible) {
        mIsButtonExpandVisible = buttonExpandVisible;
    }

    /**
     * Indicates if overflow button is visible.
     * If the Popup Menu is =-1 return  in any case <code>false</code>
     *
     * @return <code>true</code> if the button is visible and Popup Menu is assigned.
     */
    public boolean isButtonOverflowVisible() {
        //Without a PopupMenu, the button is not visible
        if (mPopupMenu==NO_POPUP_MENU && mCustomOverflowAnimation==null){
            if (mIsButtonOverflowVisible)
                Log.w("CardHeader","You set visible=true to overflow menu, but you don't add any Popup Menu or a CustomOverflowAnimator");
            return false;
        }
        return mIsButtonOverflowVisible;
    }

    /**
     * Sets visibility to overflow button
     *
     * @param buttonOverflowVisible
     */
    public void setButtonOverflowVisible(boolean buttonOverflowVisible) {
        mIsButtonOverflowVisible = buttonOverflowVisible;
    }

    /**
     * Returns Resource ID for Popup menu
     *
     * @return resource ID for Popup Menu
     */
    public int getPopupMenu() {
        return mPopupMenu;
    }

    /**
     * Indicates if other button is visible.
     * If any {@link #mOtherButtonClickListener} is assigned return in any case <code>false</code>
     *
     * @return <code>true</code> if other button is visible and {@link #mOtherButtonClickListener} is assigned
     */
    public boolean isOtherButtonVisible() {
        if (mOtherButtonClickListener == null) {
            if (mIsOtherButtonVisible)
                Log.w("CardHeader", "You set visible=true to other button menu, but you don't add any listener");
            return false;
        }
        return mIsOtherButtonVisible;
    }

    /**
     * Sets visibility to other button
     *
     * @param isOtherButtonVisible
     */
    public void setOtherButtonVisible(boolean isOtherButtonVisible) {
        mIsOtherButtonVisible = isOtherButtonVisible;
    }

    /**
     * Returns listener invoked when Other Button is clicked
     *
     * @return listener
     */
    public OnClickCardHeaderOtherButtonListener getOtherButtonClickListener() {
        return mOtherButtonClickListener;
    }

    /**
     * Sets listener invoked when Other Button is clicked
     *
     * @param otherButtonClickListener listener
     */
    public void setOtherButtonClickListener(OnClickCardHeaderOtherButtonListener otherButtonClickListener) {
        mOtherButtonClickListener = otherButtonClickListener;
    }

    /**
     * Returns resource drawable ID for Other Button
     *
     * @return resource ID
     */
    public int getOtherButtonDrawable() {
        return mOtherButtonDrawable;
    }

    /**
     * Set resource drawable ID for other Button
     * You can customize this button also with your styles.xml file using:
     * <pre><code>
     *     <style name="card.header_button_base.other">
     *      <item name="android:background">@drawable/card_menu_button_other</item>
     *     </style>
     * </code></pre>
     *
     * @param otherButtonDrawable
     */
    public void setOtherButtonDrawable(int otherButtonDrawable) {
        mOtherButtonDrawable = otherButtonDrawable;
    }


}




Java Source Code List

com.pratamawijaya.example_gson.ApplicationTest.java
com.pratamawijaya.example_gson.MainActivity.java
com.pratamawijaya.examplebuildflavor.ApplicationTest.java
com.pratamawijaya.examplebuildflavor.MainActivity.java
com.pratamawijaya.exampledynamicedittext.ApplicationTest.java
com.pratamawijaya.exampledynamicedittext.MainActivity.java
com.pratamawijaya.examplefragment.ApplicationTest.java
com.pratamawijaya.examplefragment.MyActivity.java
com.pratamawijaya.examplefragment.MyFragment.java
com.pratamawijaya.examplefusedlocation.ApplicationTest.java
com.pratamawijaya.examplefusedlocation.MainActivity.java
com.pratamawijaya.exampleinstagram.ApplicationTest.java
com.pratamawijaya.exampleinstagram.BaseActivity.java
com.pratamawijaya.exampleinstagram.BaseApplication.java
com.pratamawijaya.exampleinstagram.MainActivity.java
com.pratamawijaya.exampleinstagram.authentication.InstagramApp.java
com.pratamawijaya.exampleinstagram.authentication.InstagramWebviewAuthActivity.java
com.pratamawijaya.examplepallete.ApplicationTest.java
com.pratamawijaya.examplepallete.MainActivity.java
com.pratamawijaya.examplepallete.adapter.CoverAdapter.java
com.pratamawijaya.examplepallete.model.CoverItem.java
com.pratamawijaya.exampleparse.ApplicationTest.java
com.pratamawijaya.exampleparse.BaseApplication.java
com.pratamawijaya.exampleparse.MainActivity.java
com.pratamawijaya.examplerealmio.ApplicationTest.java
com.pratamawijaya.examplerealmio.InsertDataActivity.java
com.pratamawijaya.examplerealmio.MainActivity.java
com.pratamawijaya.examplerealmio.ReadDataActivity.java
com.pratamawijaya.examplerealmio.User.java
com.pratamawijaya.exampleretrofit.ApplicationTest.java
com.pratamawijaya.exampleretrofit.MyActivity.java
id.pratama.example.cardslib.MainActivity.java
id.pratama.example.facebooklogin.MainActivity.java
id.pratama.example.streamingaudio.CobaSaja.java
id.pratama.example.streamingaudio.MainActivity.java
id.pratama.example.streamingaudio.Test.java
id.pratama.example.streamingaudio.service.StreamService.java
id.pratama.example.streamingaudio.utils.Utils.java
id.pratama.example.volley.MainActivity.java
it.gmariotti.cardslib.library.Constants.java
it.gmariotti.cardslib.library.internal.CardArrayAdapter.java
it.gmariotti.cardslib.library.internal.CardArrayMultiChoiceAdapter.java
it.gmariotti.cardslib.library.internal.CardCursorAdapter.java
it.gmariotti.cardslib.library.internal.CardExpand.java
it.gmariotti.cardslib.library.internal.CardGridArrayAdapter.java
it.gmariotti.cardslib.library.internal.CardGridCursorAdapter.java
it.gmariotti.cardslib.library.internal.CardHeader.java
it.gmariotti.cardslib.library.internal.CardThumbnail.java
it.gmariotti.cardslib.library.internal.Card.java
it.gmariotti.cardslib.library.internal.ViewToClickToExpand.java
it.gmariotti.cardslib.library.internal.base.BaseCardArrayAdapter.java
it.gmariotti.cardslib.library.internal.base.BaseCardCursorAdapter.java
it.gmariotti.cardslib.library.internal.base.BaseCard.java
it.gmariotti.cardslib.library.internal.base.CardUIInferface.java
it.gmariotti.cardslib.library.internal.multichoice.DefaultOptionMultiChoice.java
it.gmariotti.cardslib.library.internal.multichoice.MultiChoiceAdapterHelperBase.java
it.gmariotti.cardslib.library.internal.multichoice.MultiChoiceAdapter.java
it.gmariotti.cardslib.library.internal.multichoice.OptionMultiChoice.java
it.gmariotti.cardslib.library.internal.overflowanimation.BaseCardOverlayAnimation.java
it.gmariotti.cardslib.library.internal.overflowanimation.BaseOverflowAnimation.java
it.gmariotti.cardslib.library.internal.overflowanimation.TwoCardOverlayAnimation.java
it.gmariotti.cardslib.library.utils.BitmapUtils.java
it.gmariotti.cardslib.library.utils.CacheUtil.java
it.gmariotti.cardslib.library.view.BaseCardView.java
it.gmariotti.cardslib.library.view.CardGridView.java
it.gmariotti.cardslib.library.view.CardListView.java
it.gmariotti.cardslib.library.view.CardView.java
it.gmariotti.cardslib.library.view.base.CardViewInterface.java
it.gmariotti.cardslib.library.view.component.CardHeaderView.java
it.gmariotti.cardslib.library.view.component.CardShadowView.java
it.gmariotti.cardslib.library.view.component.CardThumbnailView.java
it.gmariotti.cardslib.library.view.listener.SwipeDismissListViewTouchListener.java
it.gmariotti.cardslib.library.view.listener.SwipeDismissViewTouchListener.java
it.gmariotti.cardslib.library.view.listener.UndoBarController.java
it.gmariotti.cardslib.library.view.listener.UndoCard.java
me.pratama.examplebetterpickers.ApplicationTest.java
me.pratama.examplebetterpickers.MainActivity.java
me.pratama.examplelayoutforandroiddev.ApplicationTest.java
me.pratama.examplelayoutforandroiddev.MainActivity.java
me.pratama.examplelayoutforandroiddev.SoalActivity.java
me.pratama.examplelayoutforandroiddev.entity.Soal.java
me.pratama.examplematerial.ApplicationTest.java
me.pratama.examplematerial.BaseActivity.java
me.pratama.examplematerial.MainActivity.java
me.pratama.examplematerial.interpolator.CubicBezierInterpolator.java
me.pratama.examplematerial.model.Singer.java
me.pratama.examplematerial.ui.FabActivity.java
me.pratama.examplematerial.ui.RecyclerViewActivity.java
me.pratama.examplematerial.ui.adapter.RecyclerAdapter.java