Android Open Source - GuiLib Multi Fragment Pager Adapter






From Project

Back to project page GuiLib.

License

The source code is released under:

Apache License

If you think the Android project GuiLib 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) 2010 Gandulf Kohlweiss/*from  w w w.  j  a  v a  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.gandulf.guilib.view.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.gandulf.guilib.util.Debug;

/**
 * @author Ganymede
 * 
 */
public abstract class MultiFragmentPagerAdapter extends PagerAdapter {

  protected List<Fragment> mCurrentPrimaryItems = new ArrayList<Fragment>();

  protected FragmentManager mFragmentManager;
  protected FragmentTransaction mCurTransaction;

  protected Context mContext;

  public MultiFragmentPagerAdapter(Context context, FragmentManager fm) {
    super();
    this.mContext = context;
    this.mFragmentManager = fm;
  }

  public abstract Fragment getItem(int position, int tab);

  public abstract int getCount();

  public abstract int getCount(int position);

  @Override
  public Object instantiateItem(View container, int position) {
    if (mCurTransaction == null) {
      mCurTransaction = mFragmentManager.beginTransaction();
    }

    LinearLayout v = new LinearLayout(mContext);
    v.setOrientation(LinearLayout.HORIZONTAL);
    v.setWeightSum(getCount(position));

    int startIndex = getStartIndex(position);

    int containerId = 0;
    final int size = getCount(position);
    for (int i = 0; i < size; i++) {

      containerId = startIndex + i;

      String name = makeFragmentName(container.getId(), containerId);
      Fragment fragment = mFragmentManager.findFragmentByTag(name);

      FrameLayout fragmentView = new FrameLayout(mContext);
      LayoutParams layoutParams = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1);
      fragmentView.setLayoutParams(layoutParams);
      fragmentView.setId(containerId);
      v.addView(fragmentView, i);

      if (fragment != null) {
        Debug.verbose("Attaching item #" + position + ": f=" + fragment + " id:" + containerId);
        mCurTransaction.attach(fragment);
      } else {
        // index is 1 based remove 1 to get a 0-based
        fragment = getItem(position, i);
        if (fragment != null) {
          Debug.verbose("Adding item #" + position + ": f=" + fragment + " id:" + containerId);
          mCurTransaction.add(containerId, fragment, name);
        }
      }
      if (fragment != null && !mCurrentPrimaryItems.contains(fragment)) {
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);
      }
    }

    mCurTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

    ((ViewPager) container).addView(v, 0);

    return v;
  }

  @Override
  public void destroyItem(View container, int position, Object object) {
    if (mCurTransaction == null) {
      mCurTransaction = mFragmentManager.beginTransaction();
    }
    int containerId = 0;
    final int startIndex = getStartIndex(position);
    final int size = getCount(position);
    for (int i = 0; i < size; i++) {
      containerId = startIndex + i;
      String name = makeFragmentName(container.getId(), containerId);
      Fragment fragment = mFragmentManager.findFragmentByTag(name);
      if (fragment != null) {
        Debug.verbose("Detaching item #" + position + ": f=" + object + " v=" + fragment.getView());
        mCurTransaction.detach(fragment);
      }
    }

    ((ViewPager) container).removeView((LinearLayout) object);
  }

  @Override
  public boolean isViewFromObject(View view, Object object) {
    return view == ((LinearLayout) object);
  }

  @Override
  public void setPrimaryItem(View container, int position, Object object) {

    List<Fragment> newPrimaryItems = new ArrayList<Fragment>(getCount(position));
    int containerId = 0;
    final int startIndex = getStartIndex(position);
    final int size = getCount(position);
    for (int i = 0; i < size; i++) {

      containerId = startIndex + i;
      String name = makeFragmentName(container.getId(), containerId);
      Fragment fragment = mFragmentManager.findFragmentByTag(name);
      if (fragment != null) {
        newPrimaryItems.add(fragment);
      }

    }

    mCurrentPrimaryItems.removeAll(newPrimaryItems);
    for (Fragment frag : mCurrentPrimaryItems) {
      frag.setMenuVisibility(false);
      frag.setUserVisibleHint(false);
    }

    for (Fragment frag : newPrimaryItems) {
      frag.setMenuVisibility(true);
      frag.setUserVisibleHint(true);
    }

    mCurrentPrimaryItems = newPrimaryItems;

  }

  @Override
  public void finishUpdate(View container) {
    if (mCurTransaction != null) {
      mCurTransaction.commitAllowingStateLoss();
      mCurTransaction = null;
      mFragmentManager.executePendingTransactions();
    }
  }

  @Override
  public Parcelable saveState() {
    return null;
  }

  @Override
  public void startUpdate(View arg0) {
  }

  @Override
  public void restoreState(Parcelable arg0, ClassLoader arg1) {
  }

  protected String makeFragmentName(int viewId, int position) {
    return "android:switcher:" + viewId + ":" + position;
  }

  protected int getStartIndex(int position) {
    int index = 1;
    for (int i = 0; i < position; i++) {
      index += getCount(i);
    }
    return index;

  }

}




Java Source Code List

au.com.bytecode.opencsv.CSVReader.java
com.ecloud.pulltozoomview.PullToZoomScrollView.java
com.gandulf.guilib.data.OpenArrayAdapter.java
com.gandulf.guilib.data.OpenFilter.java
com.gandulf.guilib.download.AbstractDownloader.java
com.gandulf.guilib.download.DownloadBroadcastReceiver.java
com.gandulf.guilib.download.DownloaderGinger.java
com.gandulf.guilib.download.DownloaderWrapper.java
com.gandulf.guilib.download.Downloader.java
com.gandulf.guilib.download.MediaScannerWrapper.java
com.gandulf.guilib.download.UnzipIntentService.java
com.gandulf.guilib.listener.CheckableListenable.java
com.gandulf.guilib.listener.OnCheckedChangeListener.java
com.gandulf.guilib.util.ColorUtil.java
com.gandulf.guilib.util.Debug.java
com.gandulf.guilib.util.DefaultTextWatcher.java
com.gandulf.guilib.util.DirectoryFileFilter.java
com.gandulf.guilib.util.FileFileFilter.java
com.gandulf.guilib.util.ListViewCompat.java
com.gandulf.guilib.util.MathUtil.java
com.gandulf.guilib.util.ResUtil.java
com.gandulf.guilib.view.ColorPickerDialog.java
com.gandulf.guilib.view.DynamicListViewEx.java
com.gandulf.guilib.view.HackeyDrawerLayout.java
com.gandulf.guilib.view.SeekBarEx.java
com.gandulf.guilib.view.VersionInfoDialog.java
com.gandulf.guilib.view.ViewScroller.java
com.gandulf.guilib.view.adapter.MultiFragmentPagerAdapter.java
com.getbase.floatingactionbutton.AddFloatingActionButton.java
com.getbase.floatingactionbutton.FloatingActionButton.java
com.getbase.floatingactionbutton.FloatingActionsMenu.java
com.github.amlcurran.showcaseview.AnimationFactory.java
com.github.amlcurran.showcaseview.AnimatorAnimationFactory.java
com.github.amlcurran.showcaseview.ApiUtils.java
com.github.amlcurran.showcaseview.Calculator.java
com.github.amlcurran.showcaseview.NewShowcaseDrawer.java
com.github.amlcurran.showcaseview.OnShowcaseEventListener.java
com.github.amlcurran.showcaseview.ShotStateStore.java
com.github.amlcurran.showcaseview.ShowcaseAreaCalculator.java
com.github.amlcurran.showcaseview.ShowcaseDrawer.java
com.github.amlcurran.showcaseview.ShowcaseView.java
com.github.amlcurran.showcaseview.StandardShowcaseDrawer.java
com.github.amlcurran.showcaseview.TextDrawer.java
com.github.amlcurran.showcaseview.targets.ActionBarReflector.java
com.github.amlcurran.showcaseview.targets.ActionBarViewWrapper.java
com.github.amlcurran.showcaseview.targets.ActionItemTarget.java
com.github.amlcurran.showcaseview.targets.ActionViewTarget.java
com.github.amlcurran.showcaseview.targets.AppCompatReflector.java
com.github.amlcurran.showcaseview.targets.PointTarget.java
com.github.amlcurran.showcaseview.targets.ReflectorFactory.java
com.github.amlcurran.showcaseview.targets.Reflector.java
com.github.amlcurran.showcaseview.targets.SherlockReflector.java
com.github.amlcurran.showcaseview.targets.Target.java
com.github.amlcurran.showcaseview.targets.ViewTarget.java
com.sothree.slidinguppanel.SlidingUpPanelLayout.java
com.sothree.slidinguppanel.ViewDragHelper.java
com.thebnich.floatinghintedittext.FloatingHintEditText.java
com.thebnich.floatinghintedittext.FloatingHintTextView.java
com.wefika.flowlayout.FlowLayout.java
de.hdodenhof.circleimageview.CircleImageView.java
uk.co.senab.photoview.Compat.java
uk.co.senab.photoview.PhotoViewAttacher.java
uk.co.senab.photoview.PhotoView.java
uk.co.senab.photoview.SDK16.java
uk.co.senab.photoview.ScrollerProxy.java
uk.co.senab.photoview.VersionedGestureDetector.java
uk.me.lewisdeane.ldialogs.BaseDialog.java
uk.me.lewisdeane.ldialogs.CustomDialog.java
uk.me.lewisdeane.ldialogs.CustomListAdapter.java