Android Open Source - Resonos-Android-Framework Tab View Pager Adapter






From Project

Back to project page Resonos-Android-Framework.

License

The source code is released under:

Apache License

If you think the Android project Resonos-Android-Framework 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

package com.resonos.apps.library.tabviewpager;
//  w w  w.  ja v  a 2s .  com
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;

/**
 * This is a simple base version of a PagerAdapter with Views
 */
public abstract class TabViewPagerAdapter extends PagerAdapter
          implements OnPageChangeListener, TitleProvider {
  
  // constants
  private static final String STATE_LPAGE = "lastPage";
  
  // data
  private final String[] mContent;
  private final Drawable[] mIcons;
  private final boolean[] mVis;
  private final int[] mapping;
  private final boolean icons;
  private final int[] viewCols;
  private final int col;
  private final Map<Integer, View> mViews = new HashMap<Integer, View>();
  
  // state
  private int mLastPage = -1;
  private int mRestorePage = -1;
  private Bundle mBundle = null;

  // objects
  private final CustomViewPager mParentView;
  private final TitleProvider mTitleProvider = new TitleProvider() {
    @Override
    public int getCount() {
      return mContent.length;
    }

    @Override
    public String getTitle(int position) {
      return mContent[position].toUpperCase(Locale.US);
    }

    @Override
    public Drawable getIcon(int position) {
      return icons ? mIcons[position] : null;
    }

    @Override
    public boolean isVisible(int position) {
      return mVis == null ? true : mVis[position];
    }
  };

  /**
   * Create the adapter
   * @param parentView : the {@link CustomViewPager} this adapter is for
   * @param pageTitlesRes : an array of string resources representing the
   *  page titles
   * @param iconsRes : an array if drawable resources representing the icons,
   *  or null for no icons
   * @param tabsVis : an array of booleans representing whether each page has
   *  a visible tab, or null for assuming they all do
   * @param column : the column this particular adapter is for when in landscape
   *  tablet two-column mode, or -1 for no particular column
   * @param viewColumns : an array of integers representing the column number of
   *  each page in landscape tablet two-column mode (currently only 0 or 1
   *   allowed), or null to disallow the two-column layout
   */
  public TabViewPagerAdapter(CustomViewPager parentView, int[] pageTitlesRes,
      int[] iconsRes, boolean[] tabsVis, int column, int[] viewColumns) {
    super();
    this.mParentView = parentView;
    col = column;
    viewCols = viewColumns;
    icons = iconsRes != null;
    
    // count the filtered pages for this adapter
    int count = 0;
    if (viewCols != null) {
      for (int i = 0; i < pageTitlesRes.length; i++)
        if (col == -1 || viewCols[i] == col)
          count++;
    } else
      count = pageTitlesRes.length;

    // create the filtered arrays for this adapter
    mContent = new String[count];
    mVis = new boolean[count];
    mIcons = icons ? new Drawable[count] : null;
    mapping = new int[count];
    int n = 0;
    for (int i = 0; i < pageTitlesRes.length; i++) {
      if (col == -1 || viewCols == null || viewCols[i] == col) {
        mContent[n] = parentView.getContext().getString(pageTitlesRes[i]);
        mVis[n] = tabsVis == null ? true : tabsVis[i];
        if (icons)
          mIcons[n] = parentView.getResources().getDrawable(iconsRes[i]);
        mapping[n] = i;
        n++;
      }
    }
  }

  /**
   * Search for the View corresponding to a position in the unified pager system
   * @param position : the view's index
   * @return The View, if it still exists in any pager
   */
  public View findRealView(int realPos) {
    int pos = getFunctionalPage(realPos);
    if (pos == -1)
      return null;
    else
      return findView(pos);
  }

  /**
   * Convert index from unified page system to this adaper's page list
   * @param realPos : the global index
   * @return The index in this pager, or -1 if not in this pager
   */
  public int getFunctionalPage(int realPos) {
    if (realPos == -1)
      return -1;
    for (int i = 0; i < mapping.length; i++)
      if (mapping[i] == realPos)
        return i;
    return -1;
  }

  /**
   * Convert index from this adaper's page list to unified page system
   * @param position : index in this adapter
   * @return The real page number
   */
  public int getRealPage(int position) {
    if (position == -1)
      return -1;
    return mapping[position];
  }

  /**
   * Call this when the hosting fragment/activity's onStart has been called
   */
  public void onStart() {
    if (mLastPage != -1) {
      View v = findView(mLastPage);
      if (v != null)
        onShowView(mLastPage, v);
    }
  }

  /**
   * Call this when the hosting fragment/activity's onStop has been called
   */
  public void onStop() {
    if (mLastPage != -1) {
      View v = findView(mLastPage);
      if (v != null)
        onHideView(mLastPage, v);
    }
  }

  /**
   * Called when the hosting fragment/activity's onSaveInstanceState has been called
   */
  @Override
  public Parcelable saveState() {
    Bundle b = new Bundle();
    b.putInt(STATE_LPAGE, mLastPage == -1 ? -1 : getRealPage(mLastPage));
    if (mLastPage != -1) {
      View v = findView(mLastPage);
      if (v != null)
        onSaveInstanceState(mLastPage, v, b);
    }
    return b;
  }

  /**
   * Called when the hosting fragment/activity's state has been restored
   */
  @Override
  public void restoreState(Parcelable state, ClassLoader loader) {
    if (state != null && state instanceof Bundle) {
      Bundle b = (Bundle)state;
      mRestorePage = getFunctionalPage(b.getInt(STATE_LPAGE, -1));
      if (mRestorePage != -1)
        mBundle = b;
    }
  }

  /**
   * Override this to find out when a page is shown
   * @param position : the page index
   * @param view : the View representing that page
   */
  public abstract void onShowView(int position, Object view);

  /**
   * Override this to find out when a page is hidden
   * @param position : the page index
   * @param view : the View representing that page
   */
  public abstract void onHideView(int position, Object view);

  /**
   * Override this to save state of a page
   * @param position : the page index
   * @param view : the View representing that page
   * @param outState : the Bundle to put the state in
   */
  public abstract void onSaveInstanceState(int position, Object view, Bundle outState);

  /**
   * Override this to restore state of a page
   * @param position : the page index
   * @param view : the View representing that page
   * @param inState : the Bundle to read the state from
   */
  public abstract void onRestoreInstanceState(int position, Object view, Bundle inState);

  /**
   * Override this to provide pages to the adapter
   * @param position : the page index
   * @return A view representing the given page
   */
  public abstract View getView(int position);

  @Override
  public Object instantiateItem(View collection, final int position) {
    View layout = getView(position);
    mViews.put(position, layout);
    if (layout.getParent() != null)
      ((ViewGroup)layout.getParent()).removeView(layout);
    ((CustomViewPager) collection).addView(layout);
    if (mLastPage == -1 && position == mParentView.getCurrentItem()) // so we can observe the first view being shown
      onPageSelected(position);
    if (mRestorePage == position) {
      onRestoreInstanceState(position, layout, mBundle);
      mRestorePage = -1;
      mBundle = null;
    }
    return layout;
  }

  @Override
  public void onPageSelected(int pos) {
    View v;
    if (mLastPage != -1) {
      v = findView(mLastPage);
      if (v != null)
        onHideView(mLastPage, v);
    }
    mLastPage = pos;
    v = findView(pos);
    if (v != null)
      onShowView(pos, v);
    else
      mLastPage = -1; // so instantiateitem calls onstart
  }

  @Override
  public void onPageScrollStateChanged(int arg0) {
    //
  }

  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {
    //
  }

  @Override
  public void destroyItem(View collection, int position, Object view) {
    onHideView(position, view);
    mViews.remove(position);
    ((CustomViewPager) collection).removeView((View) view);
  }

  /**
   * Search for the View corresponding to a position in the pager
   * @param position : the view's index
   * @return The View, if it still exists in the pager
   */
  public View findView(int position) {
    View object = mViews.get(position);
    if (object != null) {
      for (int i = 0; i < mParentView.getChildCount(); i++) {
        View view = mParentView.getChildAt(i);
        if (isViewFromObject(view, object)) {
          return view;
        }
      }
    }
    return null;
  }
  
  @Override
  public int getCount() {
    return mContent.length;
  }

  @Override
  public int getItemPosition(Object item) {
    for (int i = 0; i < getCount(); i++) {
      if (mViews.get(i) == item)
        return i;
    }
    return -1;
  }

  @Override
  public boolean isViewFromObject(View view, Object item) {
    return view.equals(item);
  }
  
  /** intermediary to adapter's title provider **/
  public TitleProvider getTitleProvider() {
    return mTitleProvider;
  }
  
  @Override
  public String getTitle(int position) {
    return getTitleProvider().getTitle(position);
  }

  @Override
  public Drawable getIcon(int position) {
    return getTitleProvider().getIcon(position);
  }

  @Override
  public boolean isVisible(int position) {
    return getTitleProvider().isVisible(position);
  }
}




Java Source Code List

com.resonos.apps.library.Action.java
com.resonos.apps.library.AlertFragment.java
com.resonos.apps.library.App.java
com.resonos.apps.library.BaseFragment.java
com.resonos.apps.library.FragmentBaseActivity.java
com.resonos.apps.library.file.AltAndroidFileHandle.java
com.resonos.apps.library.file.AltAndroidFiles.java
com.resonos.apps.library.file.AltFileHandle.java
com.resonos.apps.library.file.FileCache.java
com.resonos.apps.library.media.AudioVisualizer.java
com.resonos.apps.library.media.BitmapMemoryCache.java
com.resonos.apps.library.media.HueColorFilter.java
com.resonos.apps.library.media.ImageLoader.java
com.resonos.apps.library.media.MediaScannerNotifier.java
com.resonos.apps.library.model.Coord.java
com.resonos.apps.library.model.ImmutableCoord.java
com.resonos.apps.library.tabviewpager.CustomViewPager.java
com.resonos.apps.library.tabviewpager.PageIndicator.java
com.resonos.apps.library.tabviewpager.TabPageIndicator.java
com.resonos.apps.library.tabviewpager.TabViewPagerAdapter.java
com.resonos.apps.library.tabviewpager.TabViewPagerFragment.java
com.resonos.apps.library.tabviewpager.TitleProvider.java
com.resonos.apps.library.util.AppUtils.java
com.resonos.apps.library.util.ErrorReporter.java
com.resonos.apps.library.util.LifecycleTaskQueue.java
com.resonos.apps.library.util.M.java
com.resonos.apps.library.util.NetworkClient.java
com.resonos.apps.library.util.NetworkRequest.java
com.resonos.apps.library.util.ParameterList.java
com.resonos.apps.library.util.SensorReader.java
com.resonos.apps.library.util.TouchViewWorker.java
com.resonos.apps.library.util.ViewServer.java
com.resonos.apps.library.widget.DashboardLayout.java
com.resonos.apps.library.widget.FormBuilder.java
com.resonos.apps.library.widget.FormElement.java
com.resonos.apps.library.widget.ListFormBuilder.java
com.resonos.apps.library.widget.PopupWindows3D.java
com.resonos.apps.library.widget.QuickAction3D.java
com.resonos.apps.library.widget.RangeSeekBar.java
com.resonos.apps.library.widget.SeekBar.java
com.resonos.apps.library.widget.ToolBarButton.java
com.resonos.apps.library.widget.ToolBar.java