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






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  ww .j  a  v a  2s .  c o  m*/
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.resonos.app.library.R;
import com.resonos.apps.library.BaseFragment;
import com.resonos.apps.library.FragmentBaseActivity;

/**
 * This fragment shows multiple views by using a tabbed view pager.
 * In landscape mode on tablets, it supports two side-by-side viewpagers
 *  using the same set of pages.  It is quite powerful if one takes advantage
 *  of all of the public API available here.
 * @author Chris
 */
public abstract class TabViewPagerFragment extends BaseFragment {

  // objects
  private TabViewPagerAdapter mAdapter;
  private CustomViewPager mPager;
  private TabPageIndicator mIndicator;
  
  // optional second set of objects
  private TabViewPagerAdapter mAdapterTwo;
  private CustomViewPager mPagerTwo;
  private TabPageIndicator mIndicatorTwo;

  @Override
  public View onCreateView(LayoutInflater l, ViewGroup container,
      Bundle icicle) {
    View root = l.inflate(getLayoutID(), container, false);
    mPager = (CustomViewPager)root.findViewById(R.id.pager);
    mPagerTwo = (CustomViewPager)root.findViewById(R.id.pager_two);
    
    // two different reasons to disallow column 2
    if (getColumnData() == null || mPagerTwo == null) {
      View c2 = root.findViewById(R.id.container_two);
      if (c2 != null)
        c2.setVisibility(View.GONE);
      mPagerTwo = null;
    }
    
    mAdapter = createAdapter(mPager, mPagerTwo == null ? -1 : 0);
    mPager.setAdapter(mAdapter);
    mIndicator = (TabPageIndicator)root.findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
    mIndicator.setOnPageChangeListener(mAdapter);
    mIndicator.setVisibility((mAdapter.getCount() == 1) ? View.GONE : View.VISIBLE);

    if (mPagerTwo != null) {
      mAdapterTwo = createAdapter(mPagerTwo, 1);
      mPagerTwo.setAdapter(mAdapterTwo);
      mIndicatorTwo = (TabPageIndicator)root.findViewById(R.id.indicator_two);
      mIndicatorTwo.setViewPager(mPagerTwo);
      mIndicatorTwo.setOnPageChangeListener(mAdapterTwo);
      mIndicatorTwo.setVisibility((mAdapterTwo.getCount() == 1) ? View.GONE : View.VISIBLE);
    }
    
    // on phone landscape screens, the tab indicator is best thrown into the actionbar
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE && getColumnCount() == 1) {
      ((ViewGroup)getDominantIndicator().getParent()).removeView(getDominantIndicator());
      ((FragmentBaseActivity)getActivity()).getSupportActionBar().setDisplayShowCustomEnabled(true);
    }
    return root;
  }
  
  /**
   * Return the number of columns in use.
   * @return
   */
  public int getColumnCount() {
    if (mPagerTwo != null)
      return 2;
    else
      return 1;
  }

  /**
   * Creates a CustomViewPager adapter
   * @param col : the column number (-1 for no column system)
   * @return the adapter
   */
  protected TabViewPagerAdapter createAdapter(CustomViewPager pager, int col) {
    // this will allow subclasses of TabViewPagerFragment to override methods that originate in the adapter
    return new TabViewPagerAdapter(pager, getData(), getIconData(), getVisibleData(), col, getColumnData()) {
      @Override
      public void onShowView(int position, Object view) {
        TabViewPagerFragment.this.onShowView(this.getRealPage(position), view);
      }
      @Override
      public void onHideView(int position, Object view) {
        TabViewPagerFragment.this.onHideView(this.getRealPage(position), view);
      }
      @Override
      public View getView(int position) {
        return TabViewPagerFragment.this.getView(this.getRealPage(position));
      }
      @Override
      public void onSaveInstanceState(int position, Object view, Bundle outState) {
        TabViewPagerFragment.this.onSaveInstanceState(this.getRealPage(position), view, outState);
      }
      @Override
      public void onRestoreInstanceState(int position, Object view, Bundle inState) {
        TabViewPagerFragment.this.onRestoreInstanceState(this.getRealPage(position), view, inState);
      }
    };
  }

  /**
   * Override this to provide a custom layout for this fragment.
   * @return the layout resource
   */
  protected int getLayoutID() {
    return R.layout.fragment_tabviewpager;
  }

  @Override
  public void onStop() {
    super.onStop();
    mAdapter.onStop();
    if (mAdapterTwo != null)
      mAdapterTwo.onStop();
  }

  @Override
  public void onPause() {
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE && getColumnCount() == 1) {
      // in this case, the dominant indicator can only ever be the first
      View v = ((FragmentBaseActivity)getActivity()).getSupportActionBar().getCustomView();
      if (v == getDominantIndicator())
        ((FragmentBaseActivity)getActivity()).getSupportActionBar().setCustomView(null);
    }
    super.onPause();
  }

  @Override
  public void onResume() {
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE && getColumnCount() == 1) {
      // in this case, the dominant indicator can only ever be the first
      ViewParent vp = getDominantIndicator().getParent();
      if (vp != null && vp instanceof ViewGroup) {
        ((ViewGroup)vp).removeView(getDominantIndicator());
        vp = null;
      }
      if (vp == null)
        ((FragmentBaseActivity)getActivity()).getSupportActionBar().setCustomView(getDominantIndicator());
    }
    super.onResume();
  }

  /**
   * Return the tabindicatorview that should be prioritized
   * @return
   */
  private View getDominantIndicator() {
    if (mIndicatorTwo != null && mPagerTwo.getAdapter().getCount() > 1)
      return mIndicatorTwo;
    else
      return mIndicator;
  }

  @Override
  public void onStart() {
    super.onStart();
    mAdapter.onStart();
    if (mAdapterTwo != null)
      mAdapterTwo.onStart();
  }
  
  /**
   * Get access to the CustomViewPager object
   * @param : the column (tablets only)
   * @return a {@link CustomViewPager}
   */
  public CustomViewPager getPager(int col) {
    return (col > 0 && mPagerTwo != null) ? mPagerTwo : mPager;
  }
  
  /**
   * Get access to the TabPageIndicator object
   * @param : the column (tablets only)
   * @return a {@link TabPageIndicator}
   */
  public TabPageIndicator getTabPageIndicator(int col) {
    return (col > 0 && mIndicatorTwo != null) ? mIndicatorTwo : mIndicator;
  }

  /**
   * 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) {
    if (mAdapter == null)
      return null;
    View v = mAdapter.findRealView(position);
    if (v == null && mAdapterTwo != null)
      v = mAdapterTwo.findRealView(position);
    return v;
  }

  /**
   * Search for the View corresponding to a column and index in the pager
   * @param position : the view's index
   * @return The View, if it still exists in the pager
   */
  public View findView(int col, int colIndex) {
    if (mAdapter == null)
      return null;
    if (col <= 0) {
      return mAdapter.findView(colIndex);
    } else {
      if (mAdapterTwo == null)
        return null;
      else
        return mAdapterTwo.findView(colIndex);
    }
  }
  
  /**
   * Override this to provide the data for this CustomViewPager
   * @return An array of integers pointing to the resource Strings for titles of the pages
   */
  protected abstract int[] getData();

  /**
   * Override this to provide the icon data for this CustomViewPager
   * This will override displaying titles
   * @return An array of integers pointing to the resource drawables for tabs of the pages
   */
  protected abstract int[] getIconData();

  /**
   * Override this to provide the visibility data for this CustomViewPager
   * This will allow you to have "hidden" tabs, making the appearance of multi-page tabs
   * @return An array of booleans describing tab visibility, or null to assume all are visible
   */
  protected abstract boolean[] getVisibleData();

  /**
   * Override this to provide the data for which views are in the second column (tablet layouts only)
   * @return An array of ints, 0 for the first column, 1 for the second.
   *   Return null to disallow the second column
   */
  protected abstract int[] getColumnData();
  
  /**
   * Override this to provide the views for this CustomViewPager
   * @param position : the index of the view needed
   * @return The View prepared for this slot in the CustomViewPager
   */
  protected abstract View getView(int position);

  /**
   * Want to implement an onResume/onPause style system for views in the CustomViewPager?
   * Override this method to find out when a view in the CustomViewPager is shown.
   * Your implementation must check to make sure the view was not already shown!
   * @param position : index of the view
   * @param view : the actual view
   */
  public void onShowView(int position, Object view) {
    //
  }

  /**
   * Want to implement an onResume/onPause style system for views in the CustomViewPager?
   * Override this method to find out when a view in the CustomViewPager is hidden.
   * Your implementation must check to make sure the view was not already paused!
   * @param position : index of the view
   * @param view : the actual view
   */
  public void onHideView(int position, Object view) {
    //
  }

  /**
   * Override this method to restore state of views in the pager
   * @param position : index of the view
   * @param view : the actual view
   * @param inState : the state to restore
   */
  public void onRestoreInstanceState(int position, Object view, Bundle inState) {
    //
  }

  /**
   * Override this method to save state of views in the pager
   * @param position : index of the view
   * @param view : the actual view
   * @param outState : the bundle to save state to
   */
  public void onSaveInstanceState(int position, Object view, Bundle outState) {
    //
  }
}




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