Android Open Source - GridListViewAdapters Position Calculator






From Project

Back to project page GridListViewAdapters.

License

The source code is released under:

Apache License

If you think the Android project GridListViewAdapters 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 2014-present Biraj Patel/*w ww  .j ava  2 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 com.birin.gridlistviewadapters.utils;

import android.content.res.Configuration;

/**
 * <p>
 * Often we want to support different number of cards in Portrait & Landscape
 * screen mode.For example 3 cards in Row for Portrait mode & 4 cards in
 * Landscape mode, The challenging part is since number of card is different the
 * first visible card's row will also vary. For Example for Portrait
 * mode(3-card-row) Card#25 is present in 9th Row, but same card#25 is present
 * in 7th row in Landscape mode (4-card-row).
 * </p>
 * 
 * <p>
 * This class enables you to calculate the new row position for a card(which was
 * visible at first in old orientation.)
 * </p>
 * 
 * @see library usage demos for usage of this class.
 * 
 */
public class PositionCalculator {

  private final MaxCardsInfo maxCardsInfo;

  public PositionCalculator(MaxCardsInfo info) {
    this.maxCardsInfo = info;
  }

  /**
   * Calculate correct row position after orientation change.
   * 
   * @param oldRowPosition
   *            row position The visible row position in old orientation
   * @param oldOrientation
   *            orientation value for the old orientation
   *            {@link Configuration#ORIENTATION_LANDSCAPE} or
   *            {@link Configuration#ORIENTATION_PORTRAIT}
   * @param newOrientation
   *            orientation value for the new orientation
   *            {@link Configuration#ORIENTATION_PORTRAIT} or
   *            {@link Configuration#ORIENTATION_LANDSCAPE}
   * @return the new row position to be selected after orientation change.
   * @see JavaDocs for this class & library usage samples.
   */
  public int calculatePositionInNewOrientation(
      final int VISIBLE_POSITION_IN_OLD_ORIENTATION,
      final int OLD_ORIENTATION, final int NEW_ORIENTATION) {
    int positionInNewOrientation = VISIBLE_POSITION_IN_OLD_ORIENTATION;
    if (OLD_ORIENTATION != NEW_ORIENTATION) {
      validateOrientation(OLD_ORIENTATION);
      validateOrientation(NEW_ORIENTATION);
      int maxCardsInOldOrientation = getMaxCardsFor(OLD_ORIENTATION);
      int maxCardsInNewOrientation = getMaxCardsFor(NEW_ORIENTATION);
      int firstVisibleCardIndexInOldOrientation = (VISIBLE_POSITION_IN_OLD_ORIENTATION * maxCardsInOldOrientation);
      firstVisibleCardIndexInOldOrientation += 1; // 1 based index
      positionInNewOrientation = calculateBucket(
          firstVisibleCardIndexInOldOrientation,
          maxCardsInNewOrientation);

    }
    return positionInNewOrientation;
  }

  private int calculateBucket(int firstVisibleCardIndexInOldOrientation,
      int maxCardsInNewOrientation) {
    int quotient = (firstVisibleCardIndexInOldOrientation / maxCardsInNewOrientation);
    int remainder = (firstVisibleCardIndexInOldOrientation % maxCardsInNewOrientation);
    quotient -= 1; // 0 based rows
    if (remainder == 0) {
      // First visible card is already present in quotient row.
    } else {
      // First visible card is present in next row of quotient row.
      quotient += 1;
    }
    return quotient;
  }

  /**
   * Determine the max supported cards in current orientation.
   * 
   * @param orientation
   *            . the current orientation
   * @param maxCardsInfo
   *            the max cards info which holds max supported cards info in
   *            both orientation.
   * @return the max cards for given orientation mode.
   * @see MaxCardsInfo
   */
  public static int getMaxCardsFor(final int ORIENTATION,
      MaxCardsInfo maxCardsInfo) {
    int maxCards = 0;
    if (ORIENTATION == Configuration.ORIENTATION_PORTRAIT) {
      maxCards = maxCardsInfo.getPotraitMaxCards();
    } else if (ORIENTATION == Configuration.ORIENTATION_LANDSCAPE) {
      maxCards = maxCardsInfo.getLandscapeMaxCards();
    }
    return maxCards;
  }

  private int getMaxCardsFor(final int ORIENTATION) {
    return getMaxCardsFor(ORIENTATION, maxCardsInfo);
  }

  private void validateOrientation(final int ORIENTATION) {
    boolean isValid = (ORIENTATION == Configuration.ORIENTATION_LANDSCAPE || ORIENTATION == Configuration.ORIENTATION_PORTRAIT);
    if (isValid == false) {
      throw new UnsupportedOperationException(
          "Unsupported Orientation: "
              + ORIENTATION
              + ", Valid ones are Configuration.ORIENTATION_LANDSCAPE, Configuration.ORIENTATION_PORTRAIT");
    }
  }

}




Java Source Code List

com.birin.cursorgridadapter.base.BaseCursorGridActivity.java
com.birin.cursorgridadapter.base.BaseEmployeeCursorGridAdapter.java
com.birin.cursorgridadapter.datasetup.CursorRetainingFragment.java
com.birin.cursorgridadapter.datasetup.TestContentProviderSqlHelper.java
com.birin.cursorgridadapter.datasetup.TestContentProvider.java
com.birin.cursorgridadapter.demo1.FixedCursorItems.java
com.birin.cursorgridadapter.demo2.CardClickHandlingEmployeeCursorGridAdapter.java
com.birin.cursorgridadapter.demo2.CardClickHandlingFixedCursorItems.java
com.birin.cursorgridadapter.demo3.ChildAndCardClickHandlingEmployeeCursorGridAdapter.java
com.birin.cursorgridadapter.demo3.ChildAndCardClickHandlingFixedCursorItems.java
com.birin.cursorgridadapter.demo4.FixedCursorItemsRotationSupport.java
com.birin.cursorgridadapter.demo5.UnlimitedCursorItemsRotationSupportAutoLoadMore.java
com.birin.cursorgridadapter.demo6.UnlimitedCursorItemsRotationClickToLoadMore.java
com.birin.gridlistviewadapters.BaseGridAdapter.java
com.birin.gridlistviewadapters.Card.java
com.birin.gridlistviewadapters.CursorFilter.java
com.birin.gridlistviewadapters.CursorGridAdapter.java
com.birin.gridlistviewadapters.ListGridAdapter.java
com.birin.gridlistviewadapters.RowViewHolder.java
com.birin.gridlistviewadapters.dataholders.CardDataHolder.java
com.birin.gridlistviewadapters.dataholders.CardPositionInfo.java
com.birin.gridlistviewadapters.dataholders.RowDataHolder.java
com.birin.gridlistviewadapters.utils.ChildViewsClickHandler.java
com.birin.gridlistviewadapters.utils.GridDataStructure.java
com.birin.gridlistviewadapters.utils.MaxCardsInfo.java
com.birin.gridlistviewadapters.utils.OnLoadMoreRequestListener.java
com.birin.gridlistviewadapters.utils.PositionCalculator.java
com.birin.gridlistviewadapters.utils.ViewHolderPositionTagger.java
com.birin.gridlistviewadaptersdemo.BaseDemoMenuList.java
com.birin.gridlistviewadaptersdemo.CursorDataDemos.java
com.birin.gridlistviewadaptersdemo.JavaUtilListDataDemos.java
com.birin.gridlistviewadaptersdemo.ParentDemoMenuList.java
com.birin.gridlistviewadaptersdemo.common.CharacterDrawable.java
com.birin.gridlistviewadaptersdemo.common.Constants.java
com.birin.gridlistviewadaptersdemo.common.EmployeeCardViewHolder.java
com.birin.gridlistviewadaptersdemo.common.RandomInfoGenerator.java
com.birin.listgridadapter.base.BaseEmployeeListGridAdapter.java
com.birin.listgridadapter.base.BaseListGridActivity.java
com.birin.listgridadapter.datasetup.Employee.java
com.birin.listgridadapter.datasetup.RetainedDataFragment.java
com.birin.listgridadapter.demo1.SimplestListGridAdapterUsageDemoActivity.java
com.birin.listgridadapter.demo2.FixedListItems.java
com.birin.listgridadapter.demo3.CardClickHandlingEmployeeListGridAdapter.java
com.birin.listgridadapter.demo3.CardClickHandlingFixedListItems.java
com.birin.listgridadapter.demo4.ChildAndCardClickHandlingEmployeeListGridAdapter.java
com.birin.listgridadapter.demo4.ChildAndCardClickHandlingFixedListItems.java
com.birin.listgridadapter.demo5.FixedListItemsRotationSupport.java
com.birin.listgridadapter.demo6.UnlimitedListItemsRotationSupportAutoLoadMore.java
com.birin.listgridadapter.demo7.UnlimitedListItemsRotationSupportAutoLoadMoreMax100Items.java
com.birin.listgridadapter.demo8.UnlimitedListItemsRotationSupportClickToLoadMore.java