Android Open Source - 101AndroidApps Element Adapter






From Project

Back to project page 101AndroidApps.

License

The source code is released under:

Licenced under the Creative Commons Attribution 4.0 licence. For full text see http://creativecommons.org/licenses/by/4.0/

If you think the Android project 101AndroidApps 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.hulzenga.ioi.android.app_005;
//from w w  w  . j av  a  2  s .  c om
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.hulzenga.ioi.android.app_005.ElementAdapter.ElementChangeObserver.ChangeType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

class ElementAdapter extends ArrayAdapter<Element> {


  private Context       mContext;
  private List<Element> mElements;
  private Map<Element, Integer> mElementIdMap   = new HashMap<>();
  private int                   mIdCount        = 0;
  private int                   mDraggedElement = -1;
  private ElementChangeObserver mElementChangeObserver;

  public ElementAdapter(Context context, int layoutViewId, List<Element> elements) {

    super(context, layoutViewId, elements);

    mContext = context;
    mElements = elements;

    allocateIds();
  }

  private void allocateIds() {
    mIdCount = mElements.size();

    for (int id = 0; id < mIdCount; id++) {
      mElementIdMap.put(mElements.get(id), id);
    }
  }

  public void registerElementChangeObserver(ElementChangeObserver observer) {
    mElementChangeObserver = observer;
  }

  @Override
  public void add(Element element) {
    mElements.add(0, element);
    mElementIdMap.put(element, mIdCount++);
    notifyElementChange(ChangeType.ELEMENT_ADDED);
  }

  /**
   * notifyElementChange notifies the ElementChangeObserver of changes in the
   * data.
   */
  public void notifyElementChange(ChangeType type, int... args) {
    if (mElementChangeObserver != null) {
      mElementChangeObserver.onElementChange(type, args);
    } else {
      throw new IllegalStateException("No ElementChangeObserver is registered!");
    }
  }

  @Override
  public Element getItem(int position) {
    return mElements.get(position);
  }

  @Override
  public long getItemId(int position) {
    return mElementIdMap.get(mElements.get(position));
  }

  public List<Element> getElements() {
    return mElements;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    ElementView elementView;
    if (convertView != null) {
      elementView = (ElementView) convertView;
      elementView.setPosition(position);
    } else {
      elementView = new ElementView(mContext, position);
    }

    if (position != mDraggedElement) {
      elementView.setImageBitmap(mElements.get(position).getIcon());
    } else {
      elementView.setImageBitmap(Element.getmEmptyIcon());
    }

    return elementView;
  }

  public void stopDragging() {
    mDraggedElement = -1;
    notifyElementChange(ChangeType.STOPPED_DRAGGING);
  }

  public void startDragging(int position) {
    mDraggedElement = position;
    notifyElementChange(ChangeType.STARTED_DRAGGING, position);
  }

  public void swap(int position1, int position2) {
    mDraggedElement = -1;

    Element e1 = getItem(position1);
    Element e2 = getItem(position2);

    mElements.set(position1, e2);
    mElements.set(position2, e1);

    notifyElementChange(ChangeType.ELEMENTS_SWAPPED, position1, position2);
  }

  public void removeItem(int position) {
    mElementIdMap.remove(mElements.get(position));
    mElements.remove(position);
    notifyElementChange(ChangeType.ELEMENT_REMOVED, position);
  }

  @Override
  public boolean hasStableIds() {
    return true;
  }

  // more practical observer for changes in the underlying dataset
  public interface ElementChangeObserver {
    public void onElementChange(ChangeType type, int... args);

    enum ChangeType {
      ELEMENT_ADDED, ELEMENT_REMOVED, STARTED_DRAGGING, STOPPED_DRAGGING, ELEMENTS_SWAPPED
    }
  }

}




Java Source Code List

com.hulzenga.ioi.android.AppActivity.java
com.hulzenga.ioi.android.AppDetailsDialog.java
com.hulzenga.ioi.android.App.java
com.hulzenga.ioi.android.TableOfContentsActivity.java
com.hulzenga.ioi.android.app_001.HelloWorldActivity.java
com.hulzenga.ioi.android.app_002.Ball.java
com.hulzenga.ioi.android.app_002.BouncyBallsActivity.java
com.hulzenga.ioi.android.app_002.BouncyBallsView.java
com.hulzenga.ioi.android.app_003.MonsterDatabaseActivity.java
com.hulzenga.ioi.android.app_003.MonsterEditDialog.java
com.hulzenga.ioi.android.app_003.MonsterGenerator.java
com.hulzenga.ioi.android.app_003.MonsterListAdapter.java
com.hulzenga.ioi.android.app_003.database.MonsterContract.java
com.hulzenga.ioi.android.app_003.database.MonsterDatabaseHelper.java
com.hulzenga.ioi.android.app_003.database.MonsterProvider.java
com.hulzenga.ioi.android.app_004.BouncyBall3dActivity.java
com.hulzenga.ioi.android.app_004.BouncyBall3dGLSurfaceView.java
com.hulzenga.ioi.android.app_004.BouncyBall3dRenderer.java
com.hulzenga.ioi.android.app_004.BouncyBallGame.java
com.hulzenga.ioi.android.app_005.ElementActivity.java
com.hulzenga.ioi.android.app_005.ElementAdapter.java
com.hulzenga.ioi.android.app_005.ElementAnimator.java
com.hulzenga.ioi.android.app_005.ElementSnakeView.java
com.hulzenga.ioi.android.app_005.ElementView.java
com.hulzenga.ioi.android.app_005.Element.java
com.hulzenga.ioi.android.app_006.EmptyFragment.java
com.hulzenga.ioi.android.app_006.ExposureFragment.java
com.hulzenga.ioi.android.app_006.SettingChangeListener.java
com.hulzenga.ioi.android.app_006.SettingIconFragment.java
com.hulzenga.ioi.android.app_006.SettingMenuFragment.java
com.hulzenga.ioi.android.app_006.SettingMenuLvl2Fragment.java
com.hulzenga.ioi.android.app_006.Setting.java
com.hulzenga.ioi.android.app_006.SimpleCameraActivity.java
com.hulzenga.ioi.android.app_006.SimpleCameraPreview.java
com.hulzenga.ioi.android.app_007.ButtonsFragment.java
com.hulzenga.ioi.android.app_007.HighScores.java
com.hulzenga.ioi.android.app_007.LinksFragment.java
com.hulzenga.ioi.android.app_007.Menu.java
com.hulzenga.ioi.android.app_007.Review.java
com.hulzenga.ioi.android.app_007.Score.java
com.hulzenga.ioi.android.app_007.StatusFragment.java
com.hulzenga.ioi.android.app_007.WikiGameActivity.java
com.hulzenga.ioi.android.app_007.Wiki.java
com.hulzenga.ioi.android.app_008.OverheidActivity.java
com.hulzenga.ioi.android.app_008.OverheidFeedAdapter.java
com.hulzenga.ioi.android.app_008.OverheidFeedFragment.java
com.hulzenga.ioi.android.app_008.OverheidFeedItem.java
com.hulzenga.ioi.android.app_008.OverheidFeed.java
com.hulzenga.ioi.android.app_008.OverheidPagerAdapter.java
com.hulzenga.ioi.android.app_009.ThumbsUpActivity.java
com.hulzenga.ioi.android.app_009.ThumbsUpServer.java
com.hulzenga.ioi.android.app_009.ThumbsUpService.java
com.hulzenga.ioi.android.app_010.CompassActivity.java
com.hulzenga.ioi.android.app_010.CompassDetailsView.java
com.hulzenga.ioi.android.app_010.CompassView.java
com.hulzenga.ioi.android.util.Constrain.java
com.hulzenga.ioi.android.util.DeveloperTools.java
com.hulzenga.ioi.android.util.FileManager.java
com.hulzenga.ioi.android.util.ScreenMetrics.java
com.hulzenga.ioi.android.util.open_gl.ColorFunctionFactory.java
com.hulzenga.ioi.android.util.open_gl.ColorFunction.java
com.hulzenga.ioi.android.util.open_gl.ShaderTools.java
com.hulzenga.ioi.android.util.open_gl.engine.NodeController.java
com.hulzenga.ioi.android.util.open_gl.engine.SceneGraph.java
com.hulzenga.ioi.android.util.open_gl.engine.SceneNode.java
com.hulzenga.ioi.android.util.open_gl.geometry.Box.java
com.hulzenga.ioi.android.util.open_gl.geometry.Cylinder.java
com.hulzenga.ioi.android.util.open_gl.geometry.Geometry.java
com.hulzenga.ioi.android.util.open_gl.geometry.Grid.java
com.hulzenga.ioi.android.util.open_gl.geometry.Sphere.java
com.hulzenga.ioi.android.util.open_gl.geometry.Transform.java
com.hulzenga.ioi.android.util.open_gl.vector.Vec3.java
com.hulzenga.ioi.android.util.open_gl.vector.Vec4.java
fi.iki.elonen.NanoHTTPD.java