Java Code Examples for android.support.v4.app.Fragment#setMenuVisibility()

The following examples show how to use android.support.v4.app.Fragment#setMenuVisibility() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FragmentsAdapter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    Fragment fragment = (Fragment)object;
    if (fragment != mCurrentPrimaryItem) {
        if (mCurrentPrimaryItem != null) {
            mCurrentPrimaryItem.setMenuVisibility(false);
            mCurrentPrimaryItem.setUserVisibleHint(false);
        }
        if (fragment != null) {
            fragment.setMenuVisibility(true);
            if(fragment.getFragmentManager() == null) {
                Log.v(TAG, "fragment manager == null item #"+position+": f="+object);
            }
            fragment.setUserVisibleHint(true);
        }
        mCurrentPrimaryItem = fragment;
    }
}
 
Example 2
Source File: TaggedFragmentPagerAdapter.java    From Storm with Apache License 2.0 6 votes vote down vote up
@Override
public Object instantiateItem(ViewGroup container, int position) {
    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    final long itemId = getItemId(position);

    // Do we already have this fragment?
    String name = createFragmentTag(position);
    Fragment fragment = mFragmentManager.findFragmentByTag(name);
    if (fragment != null) {
        mCurTransaction.attach(fragment);
    } else {
        fragment = getItem(position);
        mCurTransaction.add(container.getId(), fragment,
                createFragmentTag(position));
    }
    if (fragment != mCurrentPrimaryItem) {
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);
    }

    return fragment;
}
 
Example 3
Source File: MainActivity.java    From something.apk with MIT License 6 votes vote down vote up
private void updateActionbar(){
    setDisplayUp(!isMenuShowing() || canPopMenuStack());
    List<Fragment> frags = getSupportFragmentManager().getFragments();
    if(frags != null){
        for(Fragment frag : frags){
            if(frag instanceof SomeFragment && frag.isAdded()){
                if(isFragmentFocused(frag)){
                    CharSequence title = ((SomeFragment) frag).getTitle();
                    if(title != null && title.length() > 0){
                        setTitle(title);
                    }
                    frag.setMenuVisibility(true);
                }else{
                    frag.setMenuVisibility(false);
                }
            }
        }
    }
}
 
Example 4
Source File: FragmentPagerAdapter.java    From fragmentviewpager with Apache License 2.0 6 votes vote down vote up
@Override
public Object instantiateItem(ViewGroup container, int position) {
    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    final long itemId = getItemId(position);

    Fragment fragment = getFragment(position);
    if (fragment != null) {
        if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
        mCurTransaction.attach(fragment);
    } else {
        fragment = instantiateFragment(position);
        if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
        mCurTransaction.add(container.getId(), fragment,
            makeFragmentName(this, itemId));
    }
    if (fragment != mCurrentPrimaryItem) {
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);
    }

    return fragment;
}
 
Example 5
Source File: AppFragmentPagerAdapter.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object instantiateItem(ViewGroup container, int position) {
    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    final long itemId = getItemId(position);

    // Do we already have this fragment?
    // String name = makeFragmentName(container.getId(), itemId);
    String name = getTag(position);
    Fragment fragment = mFragmentManager.findFragmentByTag(name);
    if (fragment != null) {
        if (DEBUG)
            Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
        mCurTransaction.attach(fragment);
    } else {
        fragment = getItem(position);
        if (DEBUG)
            Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
        // mCurTransaction.add(container.getId(), fragment,
        // makeFragmentName(container.getId(), itemId));
        if (!fragment.isAdded())
            mCurTransaction.add(container.getId(), fragment, getTag(position));
        else
            mCurTransaction.show(fragment);
    }
    if (fragment != mCurrentPrimaryItem) {
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);
    }

    return fragment;
}
 
Example 6
Source File: ArrayPagerAdapter.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public Object instantiateItem(ViewGroup container, int position) {
    if (currTransaction == null) {
        currTransaction = fm.beginTransaction();
    }

    Fragment fragment = getExistingFragment(position);

    if (fragment != null) {
        if (fragment.getId() == container.getId()) {
            retentionStrategy.attach(fragment, currTransaction);
        } else {
            fm.beginTransaction().remove(fragment).commit();
            fm.executePendingTransactions();

            currTransaction.add(container.getId(), fragment,
                    getFragmentTag(position));
        }
    } else {
        fragment = createFragment(entries.get(position).getDescriptor());
        currTransaction.add(container.getId(), fragment,
                getFragmentTag(position));
    }

    if (fragment != currPrimaryItem) {
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);
    }

    return fragment;
}
 
Example 7
Source File: FragmentStatePagerAdapterFix.java    From UMS-Interface with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("LongLogTag")
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
    if (state != null) {
        Bundle bundle = (Bundle)state;
        bundle.setClassLoader(loader);
        Parcelable[] fss = bundle.getParcelableArray("states");
        mSavedState.clear();
        mFragments.clear();
        if (fss != null) {
            for (int i=0; i<fss.length; i++) {
                mSavedState.add((Fragment.SavedState)fss[i]);
            }
        }
        Iterable<String> keys = bundle.keySet();
        for (String key: keys) {
            if (key.startsWith("f")) {
                int index = Integer.parseInt(key.substring(1));
                Fragment f = mFragmentManager.getFragment(bundle, key);
                if (f != null) {
                    while (mFragments.size() <= index) {
                        mFragments.add(null);
                    }
                    f.setMenuVisibility(false);
                    mFragments.set(index, f);
                } else {
                    Log.w(TAG, "Bad fragment at key " + key);
                }
            }
        }
    }
}
 
Example 8
Source File: FragmentStatePagerAdapterFix.java    From UMS-Interface with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    Fragment fragment = (Fragment)object;
    if (fragment != mCurrentPrimaryItem) {
        if (mCurrentPrimaryItem != null) {
            mCurrentPrimaryItem.setMenuVisibility(false);
            mCurrentPrimaryItem.setUserVisibleHint(false);
        }
        if (fragment != null) {
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
        }
        mCurrentPrimaryItem = fragment;
    }
}
 
Example 9
Source File: FragmentListPageAdapter.java    From SprintNBA with Apache License 2.0 5 votes vote down vote up
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
	Fragment fragment = (Fragment) object;
	if (fragment != mCurrentPrimaryItem) {
		if (mCurrentPrimaryItem != null) {
			mCurrentPrimaryItem.setMenuVisibility(false);
			mCurrentPrimaryItem.setUserVisibleHint(false);
		}
		if (fragment != null) {
			fragment.setMenuVisibility(true);
			fragment.setUserVisibleHint(true);
		}
		mCurrentPrimaryItem = fragment;
	}
}
 
Example 10
Source File: TaggedFragmentStatePagerAdapter.java    From yahnac with Apache License 2.0 5 votes vote down vote up
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
    if (state != null) {
        Bundle bundle = (Bundle) state;
        bundle.setClassLoader(loader);
        Parcelable[] fss = bundle.getParcelableArray("states");
        mSavedState.clear();
        mFragments.clear();
        if (fss != null) {
            for (int i = 0; i < fss.length; i++) {
                mSavedState.add((Fragment.SavedState) fss[i]);
            }
        }
        Iterable<String> keys = bundle.keySet();
        for (String key : keys) {
            if (key.startsWith("f")) {
                int index = Integer.parseInt(key.substring(1));
                Fragment f = mFragmentManager.getFragment(bundle, key);
                if (f != null) {
                    while (mFragments.size() <= index) {
                        mFragments.add(null);
                    }
                    f.setMenuVisibility(false);
                    mFragments.set(index, f);
                } else {
                    Log.w(TAG, "Bad fragment at key " + key);
                }
            }
        }
    }
}
 
Example 11
Source File: AppFragmentPagerAdapter.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    Fragment fragment = (Fragment) object;
    if (fragment != mCurrentPrimaryItem) {
        if (mCurrentPrimaryItem != null) {
            mCurrentPrimaryItem.setMenuVisibility(false);
            mCurrentPrimaryItem.setUserVisibleHint(false);
        }
        if (fragment != null) {
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
        }
        mCurrentPrimaryItem = fragment;
    }
}
 
Example 12
Source File: CustomFragmentStatePagerAdapter.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
    if (state != null) {
        Bundle bundle = (Bundle) state;
        bundle.setClassLoader(loader);
        Parcelable[] fss = bundle.getParcelableArray("states");
        mSavedState.clear();
        mFragments.clear();
        if (fss != null) {
            for (int i = 0; i < fss.length; i++) {
                mSavedState.add((Fragment.SavedState) fss[i]);
            }
        }
        Iterable<String> keys = bundle.keySet();
        for (String key : keys) {
            if (key.startsWith("f")) {
                int index = Integer.parseInt(key.substring(1));
                Fragment f = mFragmentManager.getFragment(bundle, key);
                if (f != null) {
                    while (mFragments.size() <= index) {
                        mFragments.add(null);
                    }
                    f.setMenuVisibility(false);
                    mFragments.set(index, f);
                } else {
                    Log.w(TAG, "Bad fragment at key " + key);
                }
            }
        }
    }
}
 
Example 13
Source File: CustomFragmentStatePagerAdapter.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    Fragment fragment = (Fragment) object;
    if (fragment != mCurrentPrimaryItem) {
        if (mCurrentPrimaryItem != null) {
            mCurrentPrimaryItem.setMenuVisibility(false);
            mCurrentPrimaryItem.setUserVisibleHint(false);
        }
        if (fragment != null) {
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
        }
        mCurrentPrimaryItem = fragment;
    }
}
 
Example 14
Source File: UpdatableFragmentPagerAdapter.java    From UpdatableFragmentStatePagerAdapter with Apache License 2.0 5 votes vote down vote up
@Override
public void restoreState(@Nullable Parcelable state, ClassLoader loader) {
    if (state != null) {
        Bundle bundle = (Bundle) state;
        bundle.setClassLoader(loader);
        long[] fss = bundle.getLongArray("states");
        mSavedStates.clear();
        mFragments.clear();
        if (fss != null) {
            for (long fs : fss) {
                mSavedStates.put(fs, (Fragment.SavedState) bundle.getParcelable(Long.toString(fs)));
            }
        }
        Iterable<String> keys = bundle.keySet();
        for (String key : keys) {
            if (key.startsWith("f")) {
                Fragment f = mFragmentManager.getFragment(bundle, key);
                if (f != null) {
                    f.setMenuVisibility(false);
                    mFragments.put(Long.parseLong(key.substring(1)), f);
                } else {
                    Log.w(TAG, "Bad fragment at key " + key);
                }
            }
        }
    }
}
 
Example 15
Source File: FragmentPageSnapAdapter.java    From RecyclerPager with Apache License 2.0 5 votes vote down vote up
/**
 * Attach Fragment,
 */
@Override public void onViewAttachedToWindow(@NonNull FragmentViewHolder holder) {
    String name = makeFragmentName(holder.itemView.getId(), holder.getAdapterPosition());
    Fragment fragment = mFragmentManager.findFragmentByTag(name);

    if (fragment != null && fragment == holder.currentFragment) {
        // Nothing Changed
    } else {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

        if (fragment != null) {
            fragmentTransaction.attach(fragment);
        } else {
            fragment = getItem(holder.getAdapterPosition());
            fragmentTransaction.add(holder.itemView.getId(), fragment, name);
        }
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);

        fragmentTransaction.commitNowAllowingStateLoss();

        if (holder.currentFragment != null && holder.currentFragment.getUserVisibleHint()) {
            holder.currentFragment.setMenuVisibility(false);
            holder.currentFragment.setUserVisibleHint(false);
        }

        holder.currentFragment = fragment;
    }
}
 
Example 16
Source File: MainTabsPagerAdapter.java    From BadgeNumberTree with Apache License 2.0 5 votes vote down vote up
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    Fragment fragment = (Fragment) object;
    if (fragment != mCurrentPrimaryItem) {
        if (mCurrentPrimaryItem != null) {
            mCurrentPrimaryItem.setMenuVisibility(false);
            mCurrentPrimaryItem.setUserVisibleHint(false);
        }
        if (fragment != null) {
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
        }
        if (!fragmentsAccess[position]) {
            if (DEBUG) {
                Log.v(LOG_TAG, "Set Primary Item to " + position + " for the first time");
            }
            fragmentsAccess[position] = true;
            viewPager.post(new Runnable() {
                @Override
                public void run() {
                    notifyDataSetChanged();
                }
            });
        }
        mCurrentPrimaryItem = fragment;
    }
}
 
Example 17
Source File: TaggedFragmentPagerAdapter.java    From Storm with Apache License 2.0 5 votes vote down vote up
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    Fragment fragment = (Fragment)object;
    if (fragment != mCurrentPrimaryItem) {
        if (mCurrentPrimaryItem != null) {
            mCurrentPrimaryItem.setMenuVisibility(false);
            mCurrentPrimaryItem.setUserVisibleHint(false);
        }
        if (fragment != null) {
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
        }
        mCurrentPrimaryItem = fragment;
    }
}
 
Example 18
Source File: UpdatableFragmentPagerAdapter.java    From UpdatableFragmentStatePagerAdapter with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public Object instantiateItem(ViewGroup container, int position) {
    long tag = getItemId(position);
    Fragment fragment = mFragments.get(tag);
    // If we already have this item instantiated, there is nothing
    // to do.  This can happen when we are restoring the entire pager
    // from its saved state, where the fragment manager has already
    // taken care of restoring the fragments we previously had instantiated.
    if (fragment != null) {
        return fragment;
    }

    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    fragment = getItem(position);
    // restore state
    final Fragment.SavedState savedState = mSavedStates.get(tag);
    if (savedState != null) {
        fragment.setInitialSavedState(savedState);
    }
    fragment.setMenuVisibility(false);
    fragment.setUserVisibleHint(false);
    mFragments.put(tag, fragment);
    mCurTransaction.add(container.getId(), fragment, "f" + tag);

    return fragment;
}
 
Example 19
Source File: FragmentStatePagerAdapterFix.java    From UMS-Interface with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("LongLogTag")
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
    if (state != null) {
        Bundle bundle = (Bundle)state;
        bundle.setClassLoader(loader);
        Parcelable[] fss = bundle.getParcelableArray("states");
        mSavedState.clear();
        mFragments.clear();
        if (fss != null) {
            for (int i=0; i<fss.length; i++) {
                mSavedState.add((Fragment.SavedState)fss[i]);
            }
        }
        Iterable<String> keys = bundle.keySet();
        for (String key: keys) {
            if (key.startsWith("f")) {
                int index = Integer.parseInt(key.substring(1));
                Fragment f = mFragmentManager.getFragment(bundle, key);
                if (f != null) {
                    while (mFragments.size() <= index) {
                        mFragments.add(null);
                    }
                    f.setMenuVisibility(false);
                    mFragments.set(index, f);
                } else {
                    Log.w(TAG, "Bad fragment at key " + key);
                }
            }
        }
    }
}
 
Example 20
Source File: OpenPagerAdapter.java    From OpenPagerAdapter with Apache License 2.0 4 votes vote down vote up
@Override
public Object instantiateItem(ViewGroup container, int position) {
    // If we already have this item instantiated, there is nothing
    // to do.  This can happen when we are restoring the entire pager
    // from its saved state, where the fragment manager has already
    // taken care of restoring the fragments we previously had instantiated.
    if (mItemInfos.size() > position) {
        ItemInfo ii = mItemInfos.get(position);
        if (ii != null) {
            //判断位置是否相等,如果不相等说明新数据有增加或删除(导致了ViewPager那边有空位),
            // 而这时notifyDataSetChanged方法还没有完成,ViewPager会先调用instantiateItem来获取新的页面
            //所以为了不取错页面,我们需要对缓存进行检查和调整位置:checkProcessCacheChanged
            if (ii.position == position) {
                return ii;
            } else {
                checkProcessCacheChanged();
            }
        }
    }

    if (mCurTransaction == null) {
        mCurTransaction = mFragmentManager.beginTransaction();
    }

    Fragment fragment = getItem(position);
    if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
    if (mSavedState.size() > position) {
        Fragment.SavedState fss = mSavedState.get(position);
        if (fss != null) {
            fragment.setInitialSavedState(fss);
        }
    }
    while (mItemInfos.size() <= position) {
        mItemInfos.add(null);
    }
    fragment.setMenuVisibility(false);
    fragment.setUserVisibleHint(false);
    ItemInfo<T> iiNew = new ItemInfo<>(fragment, getItemData(position), position);
    mItemInfos.set(position, iiNew);
    mCurTransaction.add(container.getId(), fragment);

    return iiNew;
}